Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / utils / wxNamingViewer / wxNamingTree.cpp
blobe900f66dc37ea4c0308a60579c2e1c9bd72e84d3
1 // @file wxNamingTree.cpp
2 //
3 // @author Charlie Frasch <cfrasch@atdesk.com>
4 #include "pch.h"
5 #include "wxNamingTree.h"
7 #include "wx/clipbrd.h"
8 #include "wxAutoDialog.h"
9 #include "wxBindNewContext.h"
10 #include "wxNamingObject.h"
11 #include "wxBindDialog.h"
12 #include "wxViewIORDialog.h"
15 BEGIN_EVENT_TABLE( WxNamingTree, wxTreeCtrl)
16 EVT_MENU(
17 contextPopupBindContext,
18 WxNamingTree::onContextPopupBindContext)
19 EVT_MENU(
20 contextPopupBindNewContext,
21 WxNamingTree::onContextPopupBindNewContext)
22 EVT_MENU(
23 contextPopupBindObject,
24 WxNamingTree::onContextPopupBindObject)
25 EVT_MENU(
26 contextPopupDestroy,
27 WxNamingTree::onContextPopupDestroy)
28 EVT_MENU(
29 contextPopupRefresh,
30 WxNamingTree::onContextPopupRefresh)
31 EVT_MENU(
32 contextPopupUnbind,
33 WxNamingTree::onContextPopupUnbind)
34 EVT_MENU(
35 contextPopupViewReference,
36 WxNamingTree::onPopupViewReference)
37 EVT_MENU(
38 objectPopupUnbind,
39 WxNamingTree::onObjectPopupUnbind)
40 EVT_MENU(
41 objectPopupViewReference,
42 WxNamingTree::onPopupViewReference)
43 EVT_TREE_ITEM_EXPANDING(
44 WxNamingTree::treeCtrl,
45 WxNamingTree::onItemExpanding)
46 // For some reason RIGHT_UP doesn't work
47 EVT_RIGHT_DOWN( WxNamingTree::onRMouseUClick)
48 EVT_LEFT_DCLICK( WxNamingTree::onLeftDClick)
49 END_EVENT_TABLE()
52 WxNamingTree::WxNamingTree(
53 wxWindow* parent,
54 const wxWindowID id):
55 wxTreeCtrl(
56 parent,
57 id,
58 wxDefaultPosition,
59 wxSize( 234, 149)),
60 orb( 0)
62 contextPopup = new wxMenu();
63 contextPopup->Append( contextPopupBindContext, "Bind context");
64 contextPopup->Append( contextPopupBindNewContext, "Bind new context");
65 contextPopup->Append( contextPopupBindObject, "Bind object");
66 contextPopup->Append( contextPopupUnbind, "Unbind");
67 contextPopup->Append( contextPopupDestroy, "Destroy");
68 contextPopup->Append( contextPopupViewReference, "View reference");
69 contextPopup->Append( contextPopupRefresh, "Refresh");
71 objectPopup = new wxMenu();
72 objectPopup->Append( objectPopupUnbind, "Unbind");
73 objectPopup->Append( objectPopupViewReference, "View reference");
77 WxNamingTree::~WxNamingTree()
79 clearChildren();
82 void WxNamingTree::clearChildren( void)
84 wxTreeItemId item = GetRootItem();
85 if (item) {
87 clearChildren( item);
88 Delete( item);
93 void WxNamingTree::clearChildren( wxTreeItemId& item)
95 long cookie;
96 wxTreeItemId child = GetFirstChild( item, cookie);
97 while( child) {
99 clearChildren( child);
100 Delete( child);
101 child = GetFirstChild( item, cookie);
107 void WxNamingTree::copySelectedToClipboard()
109 WxNamingObject* object = getTreeObject();
110 try {
112 wxString ior = orb->object_to_string( object->Object());
113 if (wxTheClipboard->Open()) {
115 wxTheClipboard->SetData( new wxTextDataObject( ior));
116 wxTheClipboard->Close();
120 } catch( CORBA::Exception& ex) {
122 wxMessageBox( ex._rep_id(), "CORBA::Exception");
128 WxNamingObject* WxNamingTree::getTreeObject( void) const
130 wxTreeItemId item = GetSelection();
131 if (item == wxTreeItemId( (wxGenericTreeItem*)0)) {
132 return 0;
134 return getTreeObject (item);
137 WxNamingObject* WxNamingTree::getTreeObject( wxTreeItemId& item) const
139 WxNamingObject* object = static_cast<WxNamingObject*>(
140 GetItemData( item));
141 return object;
145 void WxNamingTree::listBindingList(
146 wxTreeItemId& item,
147 CosNaming::NamingContext_ptr context,
148 CosNaming::BindingList_var& bl)
150 try {
152 for( unsigned int i=0; i < bl->length(); i++) {
154 // Add each entry into the tree control
155 CORBA::Object_var object = context->resolve( bl[i].binding_name);
156 bool isContext =(bl[i].binding_type == CosNaming::ncontext);
157 WxNamingObject* newObject = new WxNamingObject(
158 bl[i].binding_name,
159 object.in(),
160 isContext);
161 wxString name = static_cast<const char*>( (bl[i].binding_name[0]).id);
162 const wxString kind = static_cast<const char*>( (bl[i].binding_name[0]).kind);
163 if (!kind.IsNull()) {
165 name << " | " << kind;
168 wxTreeItemId contextItem = AppendItem( item, name);
169 SetItemData( contextItem, newObject);
170 switch( bl[i].binding_type) {
172 case CosNaming::ncontext: {
174 // TODO: set a different icon for contexts
175 // TODO: set only if there are children
176 SetItemHasChildren( contextItem);
179 break;
181 case CosNaming::nobject:
182 break;
188 } catch( CORBA::Exception& ex) {
190 wxMessageBox( ex._rep_id(), "CORBA::Exception");
196 void WxNamingTree::listContext( wxTreeItemId& item)
198 // TODO: use hourglass
199 // SetCursor( *wxHOURGLASS_CURSOR);
200 try {
202 // Get the item's object and make sure we have a context
203 WxNamingObject* namingObject = getTreeObject( item);
204 CosNaming::NamingContext_var context = namingObject->NamingContext();
205 if (CORBA::is_nil( context.in ())) {
207 return;
211 // List the context's entries
212 CosNaming::BindingList_var bl;
213 CosNaming::BindingIterator_var bi;
214 context->list( listQuantum, bl, bi);
215 listBindingList( item, context.in(), bl);
216 if (!CORBA::is_nil( bi.in ())) {
218 while( bl->length()) {
220 wxString text;
221 text << "This context contains more than " << listQuantum <<
222 " entries, list the next " << listQuantum << "?";
223 if (wxMessageBox(
224 text,
225 "Question",
226 wxYES_NO | wxICON_QUESTION) == wxYES) {
228 bi->next_n( listQuantum, bl);
229 listBindingList( item, context.in(), bl);
234 bi->destroy();
238 } catch( CORBA::Exception& ex) {
240 wxMessageBox( ex._rep_id(), "CORBA::Exception");
247 void WxNamingTree::onContextPopupBindContext( wxCommandEvent& event)
249 WxAutoDialog<WxBindDialog> dialog( new WxBindDialog(
250 true,
251 orb,
252 this));
253 if (dialog->ShowModal() != wxID_OK) {
255 return;
258 try {
260 WxNamingObject* object = getTreeObject();
261 CosNaming::NamingContext_var context = object->NamingContext();
262 if (CORBA::is_nil( context.in ())) {
264 return;
267 CosNaming::NamingContext_var newContext =
268 CosNaming::NamingContext::_narrow( dialog->getObject());
269 if (CORBA::is_nil( newContext.in ())) {
271 wxMessageBox(
272 "Object is not a CosNaming::NamingContext",
273 "Error",
274 wxOK | wxICON_EXCLAMATION,
275 this);
276 return;
279 context->bind_context(
280 dialog->getName(),
281 newContext.in ());
282 onContextPopupRefresh( event);
284 } catch( CORBA::Exception& ex) {
286 wxMessageBox(
287 ex._rep_id(),
288 "CORBA::Exception");
294 void WxNamingTree::onContextPopupBindObject( wxCommandEvent& event)
296 WxAutoDialog<WxBindDialog> dialog( new WxBindDialog(
297 false,
298 orb,
299 this));
300 if (dialog->ShowModal() != wxID_OK) {
302 return;
305 try {
307 wxTreeItemId item = GetSelection();
308 WxNamingObject* object = getTreeObject( item);
309 CosNaming::NamingContext_var context = object->NamingContext();
310 if (CORBA::is_nil( context.in ())) {
312 return;
315 context->bind( dialog->getName(), dialog->getObject());
316 onContextPopupRefresh( event);
318 } catch( CORBA::Exception& ex) {
320 wxMessageBox(
321 ex._rep_id(),
322 "CORBA::Exception");
328 void WxNamingTree::onContextPopupBindNewContext( wxCommandEvent& event)
330 wxTreeItemId item = GetSelection();
331 WxNamingObject* object = getTreeObject( item);
332 CosNaming::NamingContext_var context = object->NamingContext();
333 if (CORBA::is_nil( context.in ())) {
335 return;
338 WxAutoDialog<WxBindNewContext> dialog( new WxBindNewContext( this));
339 if (dialog->ShowModal() != wxID_OK) {
341 return;
344 try {
346 CosNaming::NamingContext_var newContext = context->new_context();
347 context->bind_context( dialog->getName(), newContext.in ());
348 onContextPopupRefresh( event);
350 } catch( CORBA::Exception& ex) {
352 wxMessageBox(
353 ex._rep_id(),
354 "CORBA::Exception");
360 void WxNamingTree::onContextPopupDestroy( wxCommandEvent&)
362 if (wxMessageBox(
363 "Are you sure you want to destroy this object?",
364 "Confirm",
365 wxYES_NO | wxICON_QUESTION) != wxYES) {
367 return;
370 wxTreeItemId item = GetSelection();
371 wxTreeItemId parentItem = GetParent( item);
372 if (parentItem == 0) {
374 return;
377 WxNamingObject* object = getTreeObject( item);
378 WxNamingObject* parentObject = getTreeObject( parentItem);
379 CosNaming::NamingContext_var parentNaming = parentObject->NamingContext();
380 try {
382 // First try to destroy, it will raise an exception if it's not empty
383 CosNaming::NamingContext_var context = object->NamingContext();
384 context->destroy();
385 // OK it's destroyed, cleanup any children we might have lying aroung
386 clearChildren( item);
387 Delete( item);
388 // Do the unbind
389 parentNaming->unbind( object->Name());
391 } catch( CORBA::Exception& ex) {
393 wxMessageBox( ex._rep_id(), "CORBA::Exception");
399 void WxNamingTree::onContextPopupRefresh( wxCommandEvent&)
401 wxTreeItemId item = GetSelection();
402 clearChildren( item);
403 listContext( item);
407 void WxNamingTree::onContextPopupUnbind( wxCommandEvent&)
409 if (wxMessageBox(
410 "Are you sure you want to unbind this context?",
411 "Confirm",
412 wxYES_NO | wxICON_QUESTION) != wxYES) {
414 return;
417 wxTreeItemId item = GetSelection();
418 wxTreeItemId parentItem = GetParent( item);
419 if (parentItem == 0) {
421 return;
424 WxNamingObject* object = getTreeObject( item);
425 WxNamingObject* parent = getTreeObject( parentItem);
426 CosNaming::NamingContext_var context = parent->NamingContext();
427 try {
429 context->unbind( object->Name());
430 clearChildren( item);
431 Delete( item);
433 } catch( CORBA::Exception& ex) {
435 wxMessageBox( ex._rep_id(), "CORBA::Exception");
441 void WxNamingTree::onItemExpanding( wxTreeEvent& event)
444 wxTreeItemId item = event.GetItem();
445 // If this item has a child it has already been listed so nothing to do.
446 if (GetLastChild( item) != wxTreeItemId( 0L)) {
448 return;
451 listContext( item);
455 void WxNamingTree::onLeftDClick( wxMouseEvent& event)
457 wxTreeItemId item = HitTest( event.GetPosition());
458 if (!item) {
460 return;
462 WxAutoDialog<WxViewIORDialog> dialog( new WxViewIORDialog(
463 orb,
464 getTreeObject( item)->Object(),
465 this));
467 dialog->ShowModal();
471 void WxNamingTree::onObjectPopupUnbind( wxCommandEvent& )
473 if (wxMessageBox(
474 "Are you sure you want to unbind this object?",
475 "Confirm",
476 wxYES_NO | wxICON_QUESTION) != wxYES) {
478 return;
481 wxTreeItemId item = GetSelection();
483 // Make sure we don't unbind "Root"
484 wxTreeItemId parentItem = GetParent( item);
485 if (parentItem == 0) {
487 return;
490 WxNamingObject* object = getTreeObject( item);
491 WxNamingObject* parent = getTreeObject( parentItem);
492 CosNaming::NamingContext_var context = parent->NamingContext();
493 try {
495 context->unbind( object->Name());
496 clearChildren( item);
497 Delete( item);
499 } catch( CORBA::Exception& ex) {
501 wxMessageBox( ex._rep_id(), "CORBA::Exception");
507 void WxNamingTree::onPopupViewReference( wxCommandEvent&)
509 WxAutoDialog<WxViewIORDialog> dialog( new WxViewIORDialog(
510 orb,
511 getTreeObject()->Object(),
512 this));
514 dialog->ShowModal();
518 void WxNamingTree::onRMouseUClick( wxMouseEvent& event)
520 wxTreeItemId item = HitTest( event.GetPosition());
521 if (!item) {
523 return;
525 // First select the item, then popup the appropriate menu
526 SelectItem( item);
527 WxNamingObject* object = getTreeObject( item);
528 CosNaming::NamingContext_var context = object->NamingContext();
529 if (CORBA::is_nil( context.in ())) {
531 PopupMenu( objectPopup, event.m_x, event.m_y);
533 } else {
535 contextPopup->Enable( contextPopupDestroy, item != GetRootItem());
536 contextPopup->Enable( contextPopupUnbind, item != GetRootItem());
537 PopupMenu( contextPopup, event.m_x, event.m_y);
542 void WxNamingTree::resolve( CosNaming::NamingContext_ptr pRootContext)
544 clearChildren();
545 if (!CORBA::is_nil( pRootContext)) {
547 wxTreeItemId item = AddRoot( "Root");
548 SetItemData( item, new WxNamingObject( pRootContext));
549 listContext( item);
551 } else {
553 AddRoot( "<null>");
558 void WxNamingTree::setOrb( CORBA::ORB_ptr pOrb)
560 // This can only be called once!
561 assert( orb == 0);
562 orb = pOrb;