Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / utils / NamingViewer / NamingTreeCtrl.cpp
blob2a7f27911480883b6c08dab4c97b41f8e25f36e7
1 #include "stdafx.h"
2 #include "NamingViewer.h"
3 #include "NamingTreeCtrl.h"
4 #include "ViewIORDialog.h"
5 #include "NamingObject.h"
6 #include "BindDialog.h"
7 #include "BindNewContext.h"
9 #ifdef _DEBUG
10 #define new DEBUG_NEW
11 #undef THIS_FILE
12 static char THIS_FILE[] = __FILE__;
13 #endif
15 /////////////////////////////////////////////////////////////////////////////
16 // CNamingTreeCtrl
18 CNamingTreeCtrl::CNamingTreeCtrl (void)
20 m_ContextPopup.LoadMenu (IDR_CONTEXT_POPUP);
21 m_ObjectPopup.LoadMenu (IDR_OBJECT_POPUP);
24 CNamingTreeCtrl::~CNamingTreeCtrl (void)
28 BEGIN_MESSAGE_MAP(CNamingTreeCtrl, CTreeCtrl)
29 //{{AFX_MSG_MAP(CNamingTreeCtrl)
30 ON_WM_RBUTTONDOWN()
31 ON_COMMAND(ID_CONTEXT_POPUP_VIEWREFERENCE, OnContextPopupViewreference)
32 ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnItemexpanding)
33 ON_COMMAND(ID_CONTEXT_POPUP_REFRESH, OnContextPopupRefresh)
34 ON_COMMAND(ID_CONTEXT_POPUP_UNBIND, OnContextPopupUnbind)
35 ON_COMMAND(ID_CONTEXT_POPUP_DESTROY, OnContextPopupDestroy)
36 ON_COMMAND(ID_CONTEXT_POPUP_BIND_CONTEXT, OnContextPopupBindContext)
37 ON_COMMAND(ID_CONTEXT_POPUP_BINDOBJECT, OnContextPopupBindobject)
38 ON_WM_DESTROY()
39 ON_COMMAND(ID_CONTEXTPOPUP_BINDNEWCONTEXT, OnContextpopupBindnewcontext)
40 ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
41 ON_COMMAND(ID_OBJECTPOPUP_UNBIND, OnObjectpopupUnbind)
42 ON_COMMAND(ID_OBJECTPOPUP_VIEWREFRENCE, OnObjectpopupViewrefrence)
43 //}}AFX_MSG_MAP
44 END_MESSAGE_MAP()
46 /////////////////////////////////////////////////////////////////////////////
47 // CNamingTreeCtrl message handlers
49 void CNamingTreeCtrl::OnRButtonDown (UINT nFlags, CPoint point)
51 // TODO: Add your message handler code here and/or call default
52 // Special case here - this causes the entry to be selected when the
53 // right button is the first to be hit. strange
54 OnLButtonDown (nFlags, point);
56 // Now find the item were hitting
57 HTREEITEM hItem = HitTest(point);
58 if(!hItem)
60 return;
62 SelectItem(hItem);
64 POINT Point = point;
65 ClientToScreen(&Point);
66 CNamingObject* pObject = GetTreeObject(hItem);
68 // If this is not a context, show the object popup
69 if(!pObject->IsContext())
71 if(!m_ObjectPopup.GetSubMenu(0)->TrackPopupMenu(
72 #if defined (TPM_RIGHTBUTTON)
73 TPM_RIGHTBUTTON |
74 #endif
75 TPM_LEFTALIGN,
76 Point.x, Point.y, this))
78 TRACE0("TrackPopupMenu Failed");
81 else
83 if(!m_ContextPopup.GetSubMenu(0)->TrackPopupMenu(
84 #if defined (TPM_RIGHTBUTTON)
85 TPM_RIGHTBUTTON |
86 #endif
87 TPM_LEFTALIGN,
88 Point.x, Point.y, this))
90 TRACE0("TrackPopupMenu Failed");
95 void CNamingTreeCtrl::OnContextPopupViewreference()
97 // TODO: Add your command handler code here
98 ViewIORDialog Dialog(m_pORB, GetTreeObject()->Object());
99 Dialog.DoModal();
102 void CNamingTreeCtrl::SetpORB(CORBA::ORB_ptr pORB)
104 m_pORB = pORB;
107 CNamingObject* CNamingTreeCtrl::GetTreeObject(HTREEITEM hItem)
109 if(hItem == 0)
111 hItem = GetSelectedItem();
112 if(!hItem)
114 return 0;
117 CNamingObject* pObject = (CNamingObject*)GetItemData(hItem);
118 return pObject;
121 void CNamingTreeCtrl::ClearChildren(HTREEITEM hItem)
123 if(hItem == 0)
125 HTREEITEM hItem = GetRootItem();
126 if(hItem)
129 //CORBA::Object_var Object = (CORBA::Object_ptr)GetItemData(hItem);
130 ClearChildren(hItem);
131 delete GetTreeObject(hItem);
132 DeleteItem(hItem);
134 return;
137 HTREEITEM hChild;
138 while(hChild = GetNextItem(hItem, TVGN_CHILD))
140 // Remove our reference count on the object reference
141 ClearChildren(hChild);
142 delete GetTreeObject(hChild);
143 DeleteItem(hChild);
148 #define LISTQUANTUM 40
150 void CNamingTreeCtrl::ListContext(HTREEITEM hItem)
152 CWaitCursor Waiter;
155 // Get the items object and make sure we have a context
156 CNamingObject* pObject = GetTreeObject(hItem);
157 CosNaming::NamingContext_var Context = pObject->NamingContext();
158 if(CORBA::is_nil(Context))
160 return;
163 // List the contexts entries
164 CosNaming::BindingList_var bl;
165 CosNaming::BindingIterator_var bi;
166 Context->list(LISTQUANTUM, bl, bi);
167 ListBindingList(hItem, Context, bl);
169 if(!CORBA::is_nil(bi))
171 while(bl->length())
173 CString Text;
174 Text.Format(ACE_TEXT ("This context contains more than %d entries, list the next %d?"), LISTQUANTUM, LISTQUANTUM);
175 if(MessageBox(Text, ACE_TEXT ("Question"), MB_YESNO) == IDNO)
177 return;
179 bi->next_n(LISTQUANTUM, bl);
180 ListBindingList(hItem, Context, bl);
182 bi->destroy();
185 catch(CORBA::Exception& ex)
187 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
191 void CNamingTreeCtrl::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult)
193 NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
194 // TODO: Add your control notification handler code here
195 *pResult = 0;
196 // If this item has a child, it has already been listed so nothing to do..
197 if(GetChildItem(pNMTreeView->itemNew.hItem))
199 return;
201 ListContext(pNMTreeView->itemNew.hItem);
204 void CNamingTreeCtrl::OnContextPopupRefresh()
206 // TODO: Add your command handler code here
207 HTREEITEM hItem = GetSelectedItem();
208 ClearChildren(hItem);
209 ListContext(hItem);
212 void CNamingTreeCtrl::OnContextPopupUnbind()
214 // TODO: Add your command handler code here
215 if(MessageBox(ACE_TEXT ("Are you sure you want to unbind this object?"),
216 ACE_TEXT ("Confirm"), MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
218 return;
220 HTREEITEM hItem = GetSelectedItem();
221 HTREEITEM hParent = GetParentItem(hItem);
222 if(!hParent)
224 return;
226 CNamingObject* pObject = GetTreeObject(hItem);
227 CNamingObject* pParent= GetTreeObject(hParent);
228 CosNaming::NamingContext_var Context = pParent->NamingContext();
231 Context->unbind(pObject->Name());
232 ClearChildren(hItem);
233 delete pObject;
234 DeleteItem(hItem);
236 catch(CORBA::Exception& ex)
238 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
242 void CNamingTreeCtrl::Resolve(CosNaming::NamingContext_ptr pRootContext)
244 ClearChildren();
245 if(!CORBA::is_nil(pRootContext))
247 HTREEITEM hItem = InsertItem(ACE_TEXT ("Root"));
248 CosNaming::Name Name;
249 Name.length(1);
250 Name[0].id = CORBA::string_dup("Root");
251 SetItemData(hItem, (DWORD)new CNamingObject(Name, pRootContext, true));
252 ListContext(hItem);
256 void CNamingTreeCtrl::OnContextPopupDestroy()
258 // TODO: Add your command handler code here
259 if(MessageBox(ACE_TEXT ("Are you sure you want to destroy this context?"),
260 ACE_TEXT ("Confirm"), MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
262 return;
264 HTREEITEM hItem = GetSelectedItem();
265 HTREEITEM hParent = GetParentItem(hItem);
266 if(!hParent)
268 return;
270 CNamingObject* pObject = GetTreeObject(hItem);
271 CNamingObject* pParent= GetTreeObject(hParent);
272 CosNaming::NamingContext_var Parent = pParent->NamingContext();
275 // First try to destroy, it will raise exception if its not empty
276 CosNaming::NamingContext_var Context = pObject->NamingContext();
277 Context->destroy();
278 // Ok its destroyed, clean up any children we might have laying around
279 ClearChildren(hItem);
280 DeleteItem(hItem);
281 // do the unbind
282 Parent->unbind(pObject->Name());
283 delete pObject;
285 catch(CORBA::Exception& ex)
287 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
291 void CNamingTreeCtrl::OnContextPopupBindContext()
293 // TODO: Add your command handler code here
294 CBindDialog Dialog(true, m_pORB);
295 if(Dialog.DoModal() != IDOK)
297 return;
301 CNamingObject* pObject = GetTreeObject();
302 CosNaming::NamingContext_var Context = pObject->NamingContext();
303 if(CORBA::is_nil(Context.in ()))
305 return;
307 CosNaming::NamingContext_var NewContext = CosNaming::NamingContext::_narrow(Dialog.GetObject());
308 if(CORBA::is_nil(NewContext.in ()))
310 AfxMessageBox(ACE_TEXT ("Object is not a CosNaming::NamingContext"));
311 return;
313 Context->bind_context(Dialog.GetName(), NewContext);
314 OnContextPopupRefresh();
316 catch(CORBA::Exception& ex)
318 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
322 void CNamingTreeCtrl::OnContextPopupBindobject()
324 // TODO: Add your command handler code here
325 CBindDialog Dialog(false, m_pORB);
326 if(Dialog.DoModal() != IDOK)
328 return;
332 HTREEITEM hItem = GetSelectedItem();
333 CNamingObject* pObject = GetTreeObject(hItem);
334 CosNaming::NamingContext_var Context = pObject->NamingContext();
335 if(CORBA::is_nil(Context.in ()))
337 return;
339 Context->bind(Dialog.GetName(), Dialog.GetObject());
340 OnContextPopupRefresh();
342 catch(CORBA::Exception& ex)
344 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
348 void CNamingTreeCtrl::OnDestroy()
350 CTreeCtrl::OnDestroy();
352 // TODO: Add your message handler code here
353 ClearChildren();
358 void CNamingTreeCtrl::OnContextpopupBindnewcontext()
360 // TODO: Add your command handler code here
361 HTREEITEM hItem = GetSelectedItem();
362 CNamingObject* pObject = GetTreeObject(hItem);
363 CosNaming::NamingContext_var Context = pObject->NamingContext();
364 if(CORBA::is_nil(Context.in ()))
366 return;
368 CBindNewContext Dialog;
369 if(Dialog.DoModal() != IDOK)
371 return;
375 CosNaming::NamingContext_var NewContext = Context->new_context();
376 Context->bind_context(Dialog.GetName(), NewContext);
377 OnContextPopupRefresh();
379 catch(CORBA::Exception& ex)
381 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
385 void CNamingTreeCtrl::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
387 // TODO: Add your control notification handler code here
388 CNamingObject* pObject = GetTreeObject();
389 // Only display non contexts
390 if(!pObject->IsContext())
392 ViewIORDialog Dialog(m_pORB, pObject->Object());
393 Dialog.DoModal();
396 *pResult = 0;
399 void CNamingTreeCtrl::OnCopy()
401 // TODO: Add your command handler code here
402 CNamingObject* pObject = GetTreeObject();
405 CString IOR = m_pORB->object_to_string(pObject->Object());
406 // Copy to the clipboard by using the CEdit control. This is easier
407 // that doing it the right way
408 CEdit Temp;
409 CRect None(0,0, 1, 1);
410 Temp.Create(0, None, this, 0);
411 Temp.SetWindowText(IOR);
412 Temp.SetSel(0, IOR.GetLength());
413 Temp.Copy();
414 Temp.PostMessage(WM_CLOSE);
416 catch(CORBA::Exception& ex)
418 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
422 LRESULT CNamingTreeCtrl::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
424 // TODO: Add your specialized code here and/or call the base class
425 if(message == WM_HOTKEY)
427 // To trap control-c (for copying) we registered a hotkey. For some reason
428 // MFC wasn't calling my OnHotKey() function that I registered so I am forcing
429 // it this way. Anyone know the right way to do this?
430 OnCopy();
432 return CTreeCtrl::WindowProc(message, wParam, lParam);
435 void CNamingTreeCtrl::OnObjectpopupUnbind()
437 // TODO: Add your command handler code here
438 if(MessageBox(ACE_TEXT ("Are you sure you want to unbind this object?"),
439 ACE_TEXT ("Confirm"), MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
441 return;
443 HTREEITEM hItem = GetSelectedItem();
444 HTREEITEM hParent = GetParentItem(hItem);
445 if(!hParent)
447 return;
449 CNamingObject* pObject = GetTreeObject(hItem);
450 CNamingObject* pParent= GetTreeObject(hParent);
451 CosNaming::NamingContext_var Context = pParent->NamingContext();
454 Context->unbind(pObject->Name());
455 ClearChildren(hItem);
456 delete pObject;
457 DeleteItem(hItem);
459 catch(CORBA::Exception& ex)
461 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));
465 void CNamingTreeCtrl::OnObjectpopupViewrefrence()
467 // TODO: Add your command handler code here
468 ViewIORDialog Dialog(m_pORB, GetTreeObject()->Object());
469 Dialog.DoModal();
472 void CNamingTreeCtrl::ListBindingList(HTREEITEM hItem, CosNaming::NamingContext_ptr pContext, CosNaming::BindingList_var& bl)
476 for(unsigned int i=0; i < bl->length(); i++)
478 // Add each entry into the tree control
479 CORBA::Object_var Object = pContext->resolve(bl[i].binding_name);
480 bool Context = (bl[i].binding_type == CosNaming::ncontext);
481 CNamingObject* pNewObject = new CNamingObject(bl[i].binding_name, Object, Context);
482 CString Name;
483 const char* pKind = (bl[i].binding_name[0]).kind;
484 if(*pKind)
486 Name.Format(ACE_TEXT ("%s | %s"), (bl[i].binding_name[0]).id, pKind);
488 else
490 Name.Format(ACE_TEXT ("%s"), (bl[i].binding_name[0]).id);
492 HTREEITEM hContext = InsertItem(Name, hItem);
493 SetItemData(hContext, (DWORD)pNewObject);
494 switch(bl[i].binding_type)
496 case CosNaming::ncontext:
498 // Set the children flag so the + button is displayed
499 TV_ITEM Item;
500 Item.mask = TVIF_CHILDREN | TVIF_HANDLE;
501 Item.cChildren = 1;
502 Item.hItem = hContext;
503 SetItem(&Item);
505 break;
506 case CosNaming::nobject:
507 break;
511 catch(CORBA::Exception& ex)
513 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex._rep_id()), ACE_TEXT ("CORBA::Exception"));