1 // @file wxNamingTree.cpp
3 // @author Charlie Frasch <cfrasch@atdesk.com>
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
)
17 contextPopupBindContext
,
18 WxNamingTree::onContextPopupBindContext
)
20 contextPopupBindNewContext
,
21 WxNamingTree::onContextPopupBindNewContext
)
23 contextPopupBindObject
,
24 WxNamingTree::onContextPopupBindObject
)
27 WxNamingTree::onContextPopupDestroy
)
30 WxNamingTree::onContextPopupRefresh
)
33 WxNamingTree::onContextPopupUnbind
)
35 contextPopupViewReference
,
36 WxNamingTree::onPopupViewReference
)
39 WxNamingTree::onObjectPopupUnbind
)
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
)
52 WxNamingTree::WxNamingTree(
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()
82 void WxNamingTree::clearChildren( void)
84 wxTreeItemId item
= GetRootItem();
93 void WxNamingTree::clearChildren( wxTreeItemId
& item
)
96 wxTreeItemId child
= GetFirstChild( item
, cookie
);
99 clearChildren( child
);
101 child
= GetFirstChild( item
, cookie
);
107 void WxNamingTree::copySelectedToClipboard()
109 WxNamingObject
* object
= getTreeObject();
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)) {
134 return getTreeObject (item
);
137 WxNamingObject
* WxNamingTree::getTreeObject( wxTreeItemId
& item
) const
139 WxNamingObject
* object
= static_cast<WxNamingObject
*>(
145 void WxNamingTree::listBindingList(
147 CosNaming::NamingContext_ptr context
,
148 CosNaming::BindingList_var
& bl
)
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(
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
);
181 case CosNaming::nobject
:
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);
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 ())) {
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()) {
221 text
<< "This context contains more than " << listQuantum
<<
222 " entries, list the next " << listQuantum
<< "?";
226 wxYES_NO
| wxICON_QUESTION
) == wxYES
) {
228 bi
->next_n( listQuantum
, bl
);
229 listBindingList( item
, context
.in(), bl
);
238 } catch( CORBA::Exception
& ex
) {
240 wxMessageBox( ex
._rep_id(), "CORBA::Exception");
247 void WxNamingTree::onContextPopupBindContext( wxCommandEvent
& event
)
249 WxAutoDialog
<WxBindDialog
> dialog( new WxBindDialog(
253 if (dialog
->ShowModal() != wxID_OK
) {
260 WxNamingObject
* object
= getTreeObject();
261 CosNaming::NamingContext_var context
= object
->NamingContext();
262 if (CORBA::is_nil( context
.in ())) {
267 CosNaming::NamingContext_var newContext
=
268 CosNaming::NamingContext::_narrow( dialog
->getObject());
269 if (CORBA::is_nil( newContext
.in ())) {
272 "Object is not a CosNaming::NamingContext",
274 wxOK
| wxICON_EXCLAMATION
,
279 context
->bind_context(
282 onContextPopupRefresh( event
);
284 } catch( CORBA::Exception
& ex
) {
294 void WxNamingTree::onContextPopupBindObject( wxCommandEvent
& event
)
296 WxAutoDialog
<WxBindDialog
> dialog( new WxBindDialog(
300 if (dialog
->ShowModal() != wxID_OK
) {
307 wxTreeItemId item
= GetSelection();
308 WxNamingObject
* object
= getTreeObject( item
);
309 CosNaming::NamingContext_var context
= object
->NamingContext();
310 if (CORBA::is_nil( context
.in ())) {
315 context
->bind( dialog
->getName(), dialog
->getObject());
316 onContextPopupRefresh( event
);
318 } catch( CORBA::Exception
& ex
) {
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 ())) {
338 WxAutoDialog
<WxBindNewContext
> dialog( new WxBindNewContext( this));
339 if (dialog
->ShowModal() != wxID_OK
) {
346 CosNaming::NamingContext_var newContext
= context
->new_context();
347 context
->bind_context( dialog
->getName(), newContext
.in ());
348 onContextPopupRefresh( event
);
350 } catch( CORBA::Exception
& ex
) {
360 void WxNamingTree::onContextPopupDestroy( wxCommandEvent
&)
363 "Are you sure you want to destroy this object?",
365 wxYES_NO
| wxICON_QUESTION
) != wxYES
) {
370 wxTreeItemId item
= GetSelection();
371 wxTreeItemId parentItem
= GetParent( item
);
372 if (parentItem
== 0) {
377 WxNamingObject
* object
= getTreeObject( item
);
378 WxNamingObject
* parentObject
= getTreeObject( parentItem
);
379 CosNaming::NamingContext_var parentNaming
= parentObject
->NamingContext();
382 // First try to destroy, it will raise an exception if it's not empty
383 CosNaming::NamingContext_var context
= object
->NamingContext();
385 // OK it's destroyed, cleanup any children we might have lying aroung
386 clearChildren( item
);
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
);
407 void WxNamingTree::onContextPopupUnbind( wxCommandEvent
&)
410 "Are you sure you want to unbind this context?",
412 wxYES_NO
| wxICON_QUESTION
) != wxYES
) {
417 wxTreeItemId item
= GetSelection();
418 wxTreeItemId parentItem
= GetParent( item
);
419 if (parentItem
== 0) {
424 WxNamingObject
* object
= getTreeObject( item
);
425 WxNamingObject
* parent
= getTreeObject( parentItem
);
426 CosNaming::NamingContext_var context
= parent
->NamingContext();
429 context
->unbind( object
->Name());
430 clearChildren( 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)) {
455 void WxNamingTree::onLeftDClick( wxMouseEvent
& event
)
457 wxTreeItemId item
= HitTest( event
.GetPosition());
462 WxAutoDialog
<WxViewIORDialog
> dialog( new WxViewIORDialog(
464 getTreeObject( item
)->Object(),
471 void WxNamingTree::onObjectPopupUnbind( wxCommandEvent
& )
474 "Are you sure you want to unbind this object?",
476 wxYES_NO
| wxICON_QUESTION
) != wxYES
) {
481 wxTreeItemId item
= GetSelection();
483 // Make sure we don't unbind "Root"
484 wxTreeItemId parentItem
= GetParent( item
);
485 if (parentItem
== 0) {
490 WxNamingObject
* object
= getTreeObject( item
);
491 WxNamingObject
* parent
= getTreeObject( parentItem
);
492 CosNaming::NamingContext_var context
= parent
->NamingContext();
495 context
->unbind( object
->Name());
496 clearChildren( item
);
499 } catch( CORBA::Exception
& ex
) {
501 wxMessageBox( ex
._rep_id(), "CORBA::Exception");
507 void WxNamingTree::onPopupViewReference( wxCommandEvent
&)
509 WxAutoDialog
<WxViewIORDialog
> dialog( new WxViewIORDialog(
511 getTreeObject()->Object(),
518 void WxNamingTree::onRMouseUClick( wxMouseEvent
& event
)
520 wxTreeItemId item
= HitTest( event
.GetPosition());
525 // First select the item, then popup the appropriate menu
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
);
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
)
545 if (!CORBA::is_nil( pRootContext
)) {
547 wxTreeItemId item
= AddRoot( "Root");
548 SetItemData( item
, new WxNamingObject( pRootContext
));
558 void WxNamingTree::setOrb( CORBA::ORB_ptr pOrb
)
560 // This can only be called once!