2 #include "NamingViewer.h"
3 #include "NamingTreeCtrl.h"
4 #include "ViewIORDialog.h"
5 #include "NamingObject.h"
6 #include "BindDialog.h"
7 #include "BindNewContext.h"
12 static char THIS_FILE
[] = __FILE__
;
15 /////////////////////////////////////////////////////////////////////////////
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)
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
)
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
)
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
);
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)
76 Point
.x
, Point
.y
, this))
78 TRACE0("TrackPopupMenu Failed");
83 if(!m_ContextPopup
.GetSubMenu(0)->TrackPopupMenu(
84 #if defined (TPM_RIGHTBUTTON)
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());
102 void CNamingTreeCtrl::SetpORB(CORBA::ORB_ptr pORB
)
107 CNamingObject
* CNamingTreeCtrl::GetTreeObject(HTREEITEM hItem
)
111 hItem
= GetSelectedItem();
117 CNamingObject
* pObject
= (CNamingObject
*)GetItemData(hItem
);
121 void CNamingTreeCtrl::ClearChildren(HTREEITEM hItem
)
125 HTREEITEM hItem
= GetRootItem();
129 //CORBA::Object_var Object = (CORBA::Object_ptr)GetItemData(hItem);
130 ClearChildren(hItem
);
131 delete GetTreeObject(hItem
);
138 while(hChild
= GetNextItem(hItem
, TVGN_CHILD
))
140 // Remove our reference count on the object reference
141 ClearChildren(hChild
);
142 delete GetTreeObject(hChild
);
148 #define LISTQUANTUM 40
150 void CNamingTreeCtrl::ListContext(HTREEITEM hItem
)
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
))
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
))
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
)
179 bi
->next_n(LISTQUANTUM
, bl
);
180 ListBindingList(hItem
, Context
, bl
);
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
196 // If this item has a child, it has already been listed so nothing to do..
197 if(GetChildItem(pNMTreeView
->itemNew
.hItem
))
201 ListContext(pNMTreeView
->itemNew
.hItem
);
204 void CNamingTreeCtrl::OnContextPopupRefresh()
206 // TODO: Add your command handler code here
207 HTREEITEM hItem
= GetSelectedItem();
208 ClearChildren(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
)
220 HTREEITEM hItem
= GetSelectedItem();
221 HTREEITEM hParent
= GetParentItem(hItem
);
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
);
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
)
245 if(!CORBA::is_nil(pRootContext
))
247 HTREEITEM hItem
= InsertItem(ACE_TEXT ("Root"));
248 CosNaming::Name Name
;
250 Name
[0].id
= CORBA::string_dup("Root");
251 SetItemData(hItem
, (DWORD
)new CNamingObject(Name
, pRootContext
, true));
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
)
264 HTREEITEM hItem
= GetSelectedItem();
265 HTREEITEM hParent
= GetParentItem(hItem
);
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();
278 // Ok its destroyed, clean up any children we might have laying around
279 ClearChildren(hItem
);
282 Parent
->unbind(pObject
->Name());
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
)
301 CNamingObject
* pObject
= GetTreeObject();
302 CosNaming::NamingContext_var Context
= pObject
->NamingContext();
303 if(CORBA::is_nil(Context
.in ()))
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"));
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
)
332 HTREEITEM hItem
= GetSelectedItem();
333 CNamingObject
* pObject
= GetTreeObject(hItem
);
334 CosNaming::NamingContext_var Context
= pObject
->NamingContext();
335 if(CORBA::is_nil(Context
.in ()))
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
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 ()))
368 CBindNewContext Dialog
;
369 if(Dialog
.DoModal() != IDOK
)
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());
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
409 CRect
None(0,0, 1, 1);
410 Temp
.Create(0, None
, this, 0);
411 Temp
.SetWindowText(IOR
);
412 Temp
.SetSel(0, IOR
.GetLength());
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?
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
)
443 HTREEITEM hItem
= GetSelectedItem();
444 HTREEITEM hParent
= GetParentItem(hItem
);
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
);
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());
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
);
483 const char* pKind
= (bl
[i
].binding_name
[0]).kind
;
486 Name
.Format(ACE_TEXT ("%s | %s"), (bl
[i
].binding_name
[0]).id
, pKind
);
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
500 Item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
502 Item
.hItem
= hContext
;
506 case CosNaming::nobject
:
511 catch(CORBA::Exception
& ex
)
513 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex
._rep_id()), ACE_TEXT ("CORBA::Exception"));