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()
84 wxTreeItemId item
= GetRootItem();
91 void WxNamingTree::clearChildren( wxTreeItemId
& item
)
94 wxTreeItemId child
= GetFirstChild( item
, cookie
);
96 clearChildren( child
);
98 child
= GetFirstChild( item
, cookie
);
103 void WxNamingTree::copySelectedToClipboard()
105 WxNamingObject
* object
= getTreeObject();
107 wxString ior
= orb
->object_to_string( object
->Object());
108 if (wxTheClipboard
->Open()) {
109 wxTheClipboard
->SetData( new wxTextDataObject( ior
));
110 wxTheClipboard
->Close();
113 } catch( CORBA::Exception
& ex
) {
114 wxMessageBox( ex
._rep_id(), "CORBA::Exception");
119 WxNamingObject
* WxNamingTree::getTreeObject() const
121 wxTreeItemId item
= GetSelection();
122 if (item
== wxTreeItemId( (wxGenericTreeItem
*)0)) {
125 return getTreeObject (item
);
128 WxNamingObject
* WxNamingTree::getTreeObject( wxTreeItemId
& item
) const
130 WxNamingObject
* object
= static_cast<WxNamingObject
*>(
136 void WxNamingTree::listBindingList(
138 CosNaming::NamingContext_ptr context
,
139 CosNaming::BindingList_var
& bl
)
142 for( unsigned int i
=0; i
< bl
->length(); i
++) {
143 // Add each entry into the tree control
144 CORBA::Object_var object
= context
->resolve( bl
[i
].binding_name
);
145 bool isContext
=(bl
[i
].binding_type
== CosNaming::ncontext
);
146 WxNamingObject
* newObject
= new WxNamingObject(
150 wxString name
= static_cast<const char*>( (bl
[i
].binding_name
[0]).id
);
151 const wxString kind
= static_cast<const char*>( (bl
[i
].binding_name
[0]).kind
);
152 if (!kind
.IsNull()) {
153 name
<< " | " << kind
;
155 wxTreeItemId contextItem
= AppendItem( item
, name
);
156 SetItemData( contextItem
, newObject
);
157 switch( bl
[i
].binding_type
) {
158 case CosNaming::ncontext
: {
159 // TODO: set a different icon for contexts
160 // TODO: set only if there are children
161 SetItemHasChildren( contextItem
);
165 case CosNaming::nobject
:
171 } catch( CORBA::Exception
& ex
) {
172 wxMessageBox( ex
._rep_id(), "CORBA::Exception");
177 void WxNamingTree::listContext( wxTreeItemId
& item
)
179 // TODO: use hourglass
180 // SetCursor( *wxHOURGLASS_CURSOR);
182 // Get the item's object and make sure we have a context
183 WxNamingObject
* namingObject
= getTreeObject( item
);
184 CosNaming::NamingContext_var context
= namingObject
->NamingContext();
185 if (CORBA::is_nil( context
.in ())) {
189 // List the context's entries
190 CosNaming::BindingList_var bl
;
191 CosNaming::BindingIterator_var bi
;
192 context
->list( listQuantum
, bl
, bi
);
193 listBindingList( item
, context
.in(), bl
);
194 if (!CORBA::is_nil( bi
.in ())) {
195 while( bl
->length()) {
197 text
<< "This context contains more than " << listQuantum
<<
198 " entries, list the next " << listQuantum
<< "?";
202 wxYES_NO
| wxICON_QUESTION
) == wxYES
) {
203 bi
->next_n( listQuantum
, bl
);
204 listBindingList( item
, context
.in(), bl
);
211 } catch( CORBA::Exception
& ex
) {
212 wxMessageBox( ex
._rep_id(), "CORBA::Exception");
218 void WxNamingTree::onContextPopupBindContext( wxCommandEvent
& event
)
220 WxAutoDialog
<WxBindDialog
> dialog( new WxBindDialog(
224 if (dialog
->ShowModal() != wxID_OK
) {
228 WxNamingObject
* object
= getTreeObject();
229 CosNaming::NamingContext_var context
= object
->NamingContext();
230 if (CORBA::is_nil( context
.in ())) {
233 CosNaming::NamingContext_var newContext
=
234 CosNaming::NamingContext::_narrow( dialog
->getObject());
235 if (CORBA::is_nil( newContext
.in ())) {
237 "Object is not a CosNaming::NamingContext",
239 wxOK
| wxICON_EXCLAMATION
,
243 context
->bind_context(
246 onContextPopupRefresh( event
);
247 } catch( CORBA::Exception
& ex
) {
255 void WxNamingTree::onContextPopupBindObject( wxCommandEvent
& event
)
257 WxAutoDialog
<WxBindDialog
> dialog( new WxBindDialog(
261 if (dialog
->ShowModal() != wxID_OK
) {
265 wxTreeItemId item
= GetSelection();
266 WxNamingObject
* object
= getTreeObject( item
);
267 CosNaming::NamingContext_var context
= object
->NamingContext();
268 if (CORBA::is_nil( context
.in ())) {
271 context
->bind( dialog
->getName(), dialog
->getObject());
272 onContextPopupRefresh( event
);
273 } catch( CORBA::Exception
& ex
) {
281 void WxNamingTree::onContextPopupBindNewContext( wxCommandEvent
& event
)
283 wxTreeItemId item
= GetSelection();
284 WxNamingObject
* object
= getTreeObject( item
);
285 CosNaming::NamingContext_var context
= object
->NamingContext();
286 if (CORBA::is_nil( context
.in ())) {
289 WxAutoDialog
<WxBindNewContext
> dialog( new WxBindNewContext( this));
290 if (dialog
->ShowModal() != wxID_OK
) {
294 CosNaming::NamingContext_var newContext
= context
->new_context();
295 context
->bind_context( dialog
->getName(), newContext
.in ());
296 onContextPopupRefresh( event
);
297 } catch( CORBA::Exception
& ex
) {
305 void WxNamingTree::onContextPopupDestroy( wxCommandEvent
&)
308 "Are you sure you want to destroy this object?",
310 wxYES_NO
| wxICON_QUESTION
) != wxYES
) {
313 wxTreeItemId item
= GetSelection();
314 wxTreeItemId parentItem
= GetParent( item
);
315 if (parentItem
== 0) {
318 WxNamingObject
* object
= getTreeObject( item
);
319 WxNamingObject
* parentObject
= getTreeObject( parentItem
);
320 CosNaming::NamingContext_var parentNaming
= parentObject
->NamingContext();
322 // First try to destroy, it will raise an exception if it's not empty
323 CosNaming::NamingContext_var context
= object
->NamingContext();
325 // OK it's destroyed, cleanup any children we might have lying aroung
326 clearChildren( item
);
329 parentNaming
->unbind( object
->Name());
330 } catch( CORBA::Exception
& ex
) {
331 wxMessageBox( ex
._rep_id(), "CORBA::Exception");
336 void WxNamingTree::onContextPopupRefresh( wxCommandEvent
&)
338 wxTreeItemId item
= GetSelection();
339 clearChildren( item
);
344 void WxNamingTree::onContextPopupUnbind( wxCommandEvent
&)
347 "Are you sure you want to unbind this context?",
349 wxYES_NO
| wxICON_QUESTION
) != wxYES
) {
352 wxTreeItemId item
= GetSelection();
353 wxTreeItemId parentItem
= GetParent( item
);
354 if (parentItem
== 0) {
357 WxNamingObject
* object
= getTreeObject( item
);
358 WxNamingObject
* parent
= getTreeObject( parentItem
);
359 CosNaming::NamingContext_var context
= parent
->NamingContext();
361 context
->unbind( object
->Name());
362 clearChildren( item
);
364 } catch( CORBA::Exception
& ex
) {
365 wxMessageBox( ex
._rep_id(), "CORBA::Exception");
370 void WxNamingTree::onItemExpanding( wxTreeEvent
& event
)
372 wxTreeItemId item
= event
.GetItem();
373 // If this item has a child it has already been listed so nothing to do.
374 if (GetLastChild( item
) != wxTreeItemId( 0L)) {
381 void WxNamingTree::onLeftDClick( wxMouseEvent
& event
)
383 wxTreeItemId item
= HitTest( event
.GetPosition());
387 WxAutoDialog
<WxViewIORDialog
> dialog( new WxViewIORDialog(
389 getTreeObject( item
)->Object(),
396 void WxNamingTree::onObjectPopupUnbind( wxCommandEvent
& )
399 "Are you sure you want to unbind this object?",
401 wxYES_NO
| wxICON_QUESTION
) != wxYES
) {
404 wxTreeItemId item
= GetSelection();
406 // Make sure we don't unbind "Root"
407 wxTreeItemId parentItem
= GetParent( item
);
408 if (parentItem
== 0) {
411 WxNamingObject
* object
= getTreeObject( item
);
412 WxNamingObject
* parent
= getTreeObject( parentItem
);
413 CosNaming::NamingContext_var context
= parent
->NamingContext();
415 context
->unbind( object
->Name());
416 clearChildren( item
);
418 } catch( CORBA::Exception
& ex
) {
419 wxMessageBox( ex
._rep_id(), "CORBA::Exception");
424 void WxNamingTree::onPopupViewReference( wxCommandEvent
&)
426 WxAutoDialog
<WxViewIORDialog
> dialog( new WxViewIORDialog(
428 getTreeObject()->Object(),
435 void WxNamingTree::onRMouseUClick( wxMouseEvent
& event
)
437 wxTreeItemId item
= HitTest( event
.GetPosition());
441 // First select the item, then popup the appropriate menu
443 WxNamingObject
* object
= getTreeObject( item
);
444 CosNaming::NamingContext_var context
= object
->NamingContext();
445 if (CORBA::is_nil( context
.in ())) {
446 PopupMenu( objectPopup
, event
.m_x
, event
.m_y
);
448 contextPopup
->Enable( contextPopupDestroy
, item
!= GetRootItem());
449 contextPopup
->Enable( contextPopupUnbind
, item
!= GetRootItem());
450 PopupMenu( contextPopup
, event
.m_x
, event
.m_y
);
454 void WxNamingTree::resolve( CosNaming::NamingContext_ptr pRootContext
)
457 if (!CORBA::is_nil( pRootContext
)) {
458 wxTreeItemId item
= AddRoot( "Root");
459 SetItemData( item
, new WxNamingObject( pRootContext
));
466 void WxNamingTree::setOrb( CORBA::ORB_ptr pOrb
)
468 // This can only be called once!