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 ()
20 m_ContextPopup
.LoadMenu (IDR_CONTEXT_POPUP
);
21 m_ObjectPopup
.LoadMenu (IDR_OBJECT_POPUP
);
24 CNamingTreeCtrl::~CNamingTreeCtrl ()
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();
128 //CORBA::Object_var Object = (CORBA::Object_ptr)GetItemData(hItem);
129 ClearChildren(hItem
);
130 delete GetTreeObject(hItem
);
137 while(hChild
= GetNextItem(hItem
, TVGN_CHILD
))
139 // Remove our reference count on the object reference
140 ClearChildren(hChild
);
141 delete GetTreeObject(hChild
);
147 #define LISTQUANTUM 40
149 void CNamingTreeCtrl::ListContext(HTREEITEM hItem
)
154 // Get the items object and make sure we have a context
155 CNamingObject
* pObject
= GetTreeObject(hItem
);
156 CosNaming::NamingContext_var Context
= pObject
->NamingContext();
157 if(CORBA::is_nil(Context
))
162 // List the contexts entries
163 CosNaming::BindingList_var bl
;
164 CosNaming::BindingIterator_var bi
;
165 Context
->list(LISTQUANTUM
, bl
, bi
);
166 ListBindingList(hItem
, Context
, bl
);
168 if(!CORBA::is_nil(bi
))
173 Text
.Format(ACE_TEXT ("This context contains more than %d entries, list the next %d?"), LISTQUANTUM
, LISTQUANTUM
);
174 if(MessageBox(Text
, ACE_TEXT ("Question"), MB_YESNO
) == IDNO
)
178 bi
->next_n(LISTQUANTUM
, bl
);
179 ListBindingList(hItem
, Context
, bl
);
184 catch(CORBA::Exception
& ex
)
186 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex
._rep_id()), ACE_TEXT ("CORBA::Exception"));
190 void CNamingTreeCtrl::OnItemexpanding(NMHDR
* pNMHDR
, LRESULT
* pResult
)
192 NM_TREEVIEW
* pNMTreeView
= (NM_TREEVIEW
*)pNMHDR
;
193 // TODO: Add your control notification handler code here
195 // If this item has a child, it has already been listed so nothing to do..
196 if(GetChildItem(pNMTreeView
->itemNew
.hItem
))
200 ListContext(pNMTreeView
->itemNew
.hItem
);
203 void CNamingTreeCtrl::OnContextPopupRefresh()
205 // TODO: Add your command handler code here
206 HTREEITEM hItem
= GetSelectedItem();
207 ClearChildren(hItem
);
211 void CNamingTreeCtrl::OnContextPopupUnbind()
213 // TODO: Add your command handler code here
214 if(MessageBox(ACE_TEXT ("Are you sure you want to unbind this object?"),
215 ACE_TEXT ("Confirm"), MB_YESNO
| MB_ICONEXCLAMATION
) != IDYES
)
219 HTREEITEM hItem
= GetSelectedItem();
220 HTREEITEM hParent
= GetParentItem(hItem
);
225 CNamingObject
* pObject
= GetTreeObject(hItem
);
226 CNamingObject
* pParent
= GetTreeObject(hParent
);
227 CosNaming::NamingContext_var Context
= pParent
->NamingContext();
230 Context
->unbind(pObject
->Name());
231 ClearChildren(hItem
);
235 catch(CORBA::Exception
& ex
)
237 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex
._rep_id()), ACE_TEXT ("CORBA::Exception"));
241 void CNamingTreeCtrl::Resolve(CosNaming::NamingContext_ptr pRootContext
)
244 if(!CORBA::is_nil(pRootContext
))
246 HTREEITEM hItem
= InsertItem(ACE_TEXT ("Root"));
247 CosNaming::Name Name
;
249 Name
[0].id
= CORBA::string_dup("Root");
250 SetItemData(hItem
, (DWORD
)new CNamingObject(Name
, pRootContext
, true));
255 void CNamingTreeCtrl::OnContextPopupDestroy()
257 // TODO: Add your command handler code here
258 if(MessageBox(ACE_TEXT ("Are you sure you want to destroy this context?"),
259 ACE_TEXT ("Confirm"), MB_YESNO
| MB_ICONEXCLAMATION
) != IDYES
)
263 HTREEITEM hItem
= GetSelectedItem();
264 HTREEITEM hParent
= GetParentItem(hItem
);
269 CNamingObject
* pObject
= GetTreeObject(hItem
);
270 CNamingObject
* pParent
= GetTreeObject(hParent
);
271 CosNaming::NamingContext_var Parent
= pParent
->NamingContext();
274 // First try to destroy, it will raise exception if its not empty
275 CosNaming::NamingContext_var Context
= pObject
->NamingContext();
277 // Ok its destroyed, clean up any children we might have laying around
278 ClearChildren(hItem
);
281 Parent
->unbind(pObject
->Name());
284 catch(CORBA::Exception
& ex
)
286 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex
._rep_id()), ACE_TEXT ("CORBA::Exception"));
290 void CNamingTreeCtrl::OnContextPopupBindContext()
292 // TODO: Add your command handler code here
293 CBindDialog
Dialog(true, m_pORB
);
294 if(Dialog
.DoModal() != IDOK
)
300 CNamingObject
* pObject
= GetTreeObject();
301 CosNaming::NamingContext_var Context
= pObject
->NamingContext();
302 if(CORBA::is_nil(Context
.in ()))
306 CosNaming::NamingContext_var NewContext
= CosNaming::NamingContext::_narrow(Dialog
.GetObject());
307 if(CORBA::is_nil(NewContext
.in ()))
309 AfxMessageBox(ACE_TEXT ("Object is not a CosNaming::NamingContext"));
312 Context
->bind_context(Dialog
.GetName(), NewContext
);
313 OnContextPopupRefresh();
315 catch(CORBA::Exception
& ex
)
317 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex
._rep_id()), ACE_TEXT ("CORBA::Exception"));
321 void CNamingTreeCtrl::OnContextPopupBindobject()
323 // TODO: Add your command handler code here
324 CBindDialog
Dialog(false, m_pORB
);
325 if(Dialog
.DoModal() != IDOK
)
331 HTREEITEM hItem
= GetSelectedItem();
332 CNamingObject
* pObject
= GetTreeObject(hItem
);
333 CosNaming::NamingContext_var Context
= pObject
->NamingContext();
334 if(CORBA::is_nil(Context
.in ()))
338 Context
->bind(Dialog
.GetName(), Dialog
.GetObject());
339 OnContextPopupRefresh();
341 catch(CORBA::Exception
& ex
)
343 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex
._rep_id()), ACE_TEXT ("CORBA::Exception"));
347 void CNamingTreeCtrl::OnDestroy()
349 CTreeCtrl::OnDestroy();
351 // TODO: Add your message handler code here
356 void CNamingTreeCtrl::OnContextpopupBindnewcontext()
358 // TODO: Add your command handler code here
359 HTREEITEM hItem
= GetSelectedItem();
360 CNamingObject
* pObject
= GetTreeObject(hItem
);
361 CosNaming::NamingContext_var Context
= pObject
->NamingContext();
362 if(CORBA::is_nil(Context
.in ()))
366 CBindNewContext Dialog
;
367 if(Dialog
.DoModal() != IDOK
)
373 CosNaming::NamingContext_var NewContext
= Context
->new_context();
374 Context
->bind_context(Dialog
.GetName(), NewContext
);
375 OnContextPopupRefresh();
377 catch(CORBA::Exception
& ex
)
379 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex
._rep_id()), ACE_TEXT ("CORBA::Exception"));
383 void CNamingTreeCtrl::OnDblclk(NMHDR
* pNMHDR
, LRESULT
* pResult
)
385 // TODO: Add your control notification handler code here
386 CNamingObject
* pObject
= GetTreeObject();
387 // Only display non contexts
388 if(!pObject
->IsContext())
390 ViewIORDialog
Dialog(m_pORB
, pObject
->Object());
397 void CNamingTreeCtrl::OnCopy()
399 // TODO: Add your command handler code here
400 CNamingObject
* pObject
= GetTreeObject();
403 CString IOR
= m_pORB
->object_to_string(pObject
->Object());
404 // Copy to the clipboard by using the CEdit control. This is easier
405 // that doing it the right way
407 CRect
None(0,0, 1, 1);
408 Temp
.Create(0, None
, this, 0);
409 Temp
.SetWindowText(IOR
);
410 Temp
.SetSel(0, IOR
.GetLength());
412 Temp
.PostMessage(WM_CLOSE
);
414 catch(CORBA::Exception
& ex
)
416 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex
._rep_id()), ACE_TEXT ("CORBA::Exception"));
420 LRESULT
CNamingTreeCtrl::WindowProc(UINT message
, WPARAM wParam
, LPARAM lParam
)
422 // TODO: Add your specialized code here and/or call the base class
423 if(message
== WM_HOTKEY
)
425 // To trap control-c (for copying) we registered a hotkey. For some reason
426 // MFC wasn't calling my OnHotKey() function that I registered so I am forcing
427 // it this way. Anyone know the right way to do this?
430 return CTreeCtrl::WindowProc(message
, wParam
, lParam
);
433 void CNamingTreeCtrl::OnObjectpopupUnbind()
435 // TODO: Add your command handler code here
436 if(MessageBox(ACE_TEXT ("Are you sure you want to unbind this object?"),
437 ACE_TEXT ("Confirm"), MB_YESNO
| MB_ICONEXCLAMATION
) != IDYES
)
441 HTREEITEM hItem
= GetSelectedItem();
442 HTREEITEM hParent
= GetParentItem(hItem
);
447 CNamingObject
* pObject
= GetTreeObject(hItem
);
448 CNamingObject
* pParent
= GetTreeObject(hParent
);
449 CosNaming::NamingContext_var Context
= pParent
->NamingContext();
452 Context
->unbind(pObject
->Name());
453 ClearChildren(hItem
);
457 catch(CORBA::Exception
& ex
)
459 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex
._rep_id()), ACE_TEXT ("CORBA::Exception"));
463 void CNamingTreeCtrl::OnObjectpopupViewrefrence()
465 // TODO: Add your command handler code here
466 ViewIORDialog
Dialog(m_pORB
, GetTreeObject()->Object());
470 void CNamingTreeCtrl::ListBindingList(HTREEITEM hItem
, CosNaming::NamingContext_ptr pContext
, CosNaming::BindingList_var
& bl
)
474 for(unsigned int i
=0; i
< bl
->length(); i
++)
476 // Add each entry into the tree control
477 CORBA::Object_var Object
= pContext
->resolve(bl
[i
].binding_name
);
478 bool Context
= (bl
[i
].binding_type
== CosNaming::ncontext
);
479 CNamingObject
* pNewObject
= new CNamingObject(bl
[i
].binding_name
, Object
, Context
);
481 const char* pKind
= (bl
[i
].binding_name
[0]).kind
;
484 Name
.Format(ACE_TEXT ("%s | %s"), (bl
[i
].binding_name
[0]).id
, pKind
);
488 Name
.Format(ACE_TEXT ("%s"), (bl
[i
].binding_name
[0]).id
);
490 HTREEITEM hContext
= InsertItem(Name
, hItem
);
491 SetItemData(hContext
, (DWORD
)pNewObject
);
492 switch(bl
[i
].binding_type
)
494 case CosNaming::ncontext
:
496 // Set the children flag so the + button is displayed
498 Item
.mask
= TVIF_CHILDREN
| TVIF_HANDLE
;
500 Item
.hItem
= hContext
;
504 case CosNaming::nobject
:
509 catch(CORBA::Exception
& ex
)
511 MessageBox(ACE_TEXT_CHAR_TO_TCHAR (ex
._rep_id()), ACE_TEXT ("CORBA::Exception"));