Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / utils / wxNamingViewer / wxNamingTree.cpp
blob4037b8a84fa91de3f04c3e4483f4836ed6e771b5
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()
84 wxTreeItemId item = GetRootItem();
85 if (item) {
86 clearChildren( item);
87 Delete( item);
91 void WxNamingTree::clearChildren( wxTreeItemId& item)
93 long cookie;
94 wxTreeItemId child = GetFirstChild( item, cookie);
95 while( child) {
96 clearChildren( child);
97 Delete( child);
98 child = GetFirstChild( item, cookie);
103 void WxNamingTree::copySelectedToClipboard()
105 WxNamingObject* object = getTreeObject();
106 try {
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)) {
123 return 0;
125 return getTreeObject (item);
128 WxNamingObject* WxNamingTree::getTreeObject( wxTreeItemId& item) const
130 WxNamingObject* object = static_cast<WxNamingObject*>(
131 GetItemData( item));
132 return object;
136 void WxNamingTree::listBindingList(
137 wxTreeItemId& item,
138 CosNaming::NamingContext_ptr context,
139 CosNaming::BindingList_var& bl)
141 try {
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(
147 bl[i].binding_name,
148 object.in(),
149 isContext);
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);
163 break;
165 case CosNaming::nobject:
166 break;
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);
181 try {
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 ())) {
186 return;
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()) {
196 wxString text;
197 text << "This context contains more than " << listQuantum <<
198 " entries, list the next " << listQuantum << "?";
199 if (wxMessageBox(
200 text,
201 "Question",
202 wxYES_NO | wxICON_QUESTION) == wxYES) {
203 bi->next_n( listQuantum, bl);
204 listBindingList( item, context.in(), bl);
208 bi->destroy();
211 } catch( CORBA::Exception& ex) {
212 wxMessageBox( ex._rep_id(), "CORBA::Exception");
218 void WxNamingTree::onContextPopupBindContext( wxCommandEvent& event)
220 WxAutoDialog<WxBindDialog> dialog( new WxBindDialog(
221 true,
222 orb,
223 this));
224 if (dialog->ShowModal() != wxID_OK) {
225 return;
227 try {
228 WxNamingObject* object = getTreeObject();
229 CosNaming::NamingContext_var context = object->NamingContext();
230 if (CORBA::is_nil( context.in ())) {
231 return;
233 CosNaming::NamingContext_var newContext =
234 CosNaming::NamingContext::_narrow( dialog->getObject());
235 if (CORBA::is_nil( newContext.in ())) {
236 wxMessageBox(
237 "Object is not a CosNaming::NamingContext",
238 "Error",
239 wxOK | wxICON_EXCLAMATION,
240 this);
241 return;
243 context->bind_context(
244 dialog->getName(),
245 newContext.in ());
246 onContextPopupRefresh( event);
247 } catch( CORBA::Exception& ex) {
248 wxMessageBox(
249 ex._rep_id(),
250 "CORBA::Exception");
255 void WxNamingTree::onContextPopupBindObject( wxCommandEvent& event)
257 WxAutoDialog<WxBindDialog> dialog( new WxBindDialog(
258 false,
259 orb,
260 this));
261 if (dialog->ShowModal() != wxID_OK) {
262 return;
264 try {
265 wxTreeItemId item = GetSelection();
266 WxNamingObject* object = getTreeObject( item);
267 CosNaming::NamingContext_var context = object->NamingContext();
268 if (CORBA::is_nil( context.in ())) {
269 return;
271 context->bind( dialog->getName(), dialog->getObject());
272 onContextPopupRefresh( event);
273 } catch( CORBA::Exception& ex) {
274 wxMessageBox(
275 ex._rep_id(),
276 "CORBA::Exception");
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 ())) {
287 return;
289 WxAutoDialog<WxBindNewContext> dialog( new WxBindNewContext( this));
290 if (dialog->ShowModal() != wxID_OK) {
291 return;
293 try {
294 CosNaming::NamingContext_var newContext = context->new_context();
295 context->bind_context( dialog->getName(), newContext.in ());
296 onContextPopupRefresh( event);
297 } catch( CORBA::Exception& ex) {
298 wxMessageBox(
299 ex._rep_id(),
300 "CORBA::Exception");
305 void WxNamingTree::onContextPopupDestroy( wxCommandEvent&)
307 if (wxMessageBox(
308 "Are you sure you want to destroy this object?",
309 "Confirm",
310 wxYES_NO | wxICON_QUESTION) != wxYES) {
311 return;
313 wxTreeItemId item = GetSelection();
314 wxTreeItemId parentItem = GetParent( item);
315 if (parentItem == 0) {
316 return;
318 WxNamingObject* object = getTreeObject( item);
319 WxNamingObject* parentObject = getTreeObject( parentItem);
320 CosNaming::NamingContext_var parentNaming = parentObject->NamingContext();
321 try {
322 // First try to destroy, it will raise an exception if it's not empty
323 CosNaming::NamingContext_var context = object->NamingContext();
324 context->destroy();
325 // OK it's destroyed, cleanup any children we might have lying aroung
326 clearChildren( item);
327 Delete( item);
328 // Do the unbind
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);
340 listContext( item);
344 void WxNamingTree::onContextPopupUnbind( wxCommandEvent&)
346 if (wxMessageBox(
347 "Are you sure you want to unbind this context?",
348 "Confirm",
349 wxYES_NO | wxICON_QUESTION) != wxYES) {
350 return;
352 wxTreeItemId item = GetSelection();
353 wxTreeItemId parentItem = GetParent( item);
354 if (parentItem == 0) {
355 return;
357 WxNamingObject* object = getTreeObject( item);
358 WxNamingObject* parent = getTreeObject( parentItem);
359 CosNaming::NamingContext_var context = parent->NamingContext();
360 try {
361 context->unbind( object->Name());
362 clearChildren( item);
363 Delete( 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)) {
375 return;
377 listContext( item);
381 void WxNamingTree::onLeftDClick( wxMouseEvent& event)
383 wxTreeItemId item = HitTest( event.GetPosition());
384 if (!item) {
385 return;
387 WxAutoDialog<WxViewIORDialog> dialog( new WxViewIORDialog(
388 orb,
389 getTreeObject( item)->Object(),
390 this));
392 dialog->ShowModal();
396 void WxNamingTree::onObjectPopupUnbind( wxCommandEvent& )
398 if (wxMessageBox(
399 "Are you sure you want to unbind this object?",
400 "Confirm",
401 wxYES_NO | wxICON_QUESTION) != wxYES) {
402 return;
404 wxTreeItemId item = GetSelection();
406 // Make sure we don't unbind "Root"
407 wxTreeItemId parentItem = GetParent( item);
408 if (parentItem == 0) {
409 return;
411 WxNamingObject* object = getTreeObject( item);
412 WxNamingObject* parent = getTreeObject( parentItem);
413 CosNaming::NamingContext_var context = parent->NamingContext();
414 try {
415 context->unbind( object->Name());
416 clearChildren( item);
417 Delete( item);
418 } catch( CORBA::Exception& ex) {
419 wxMessageBox( ex._rep_id(), "CORBA::Exception");
424 void WxNamingTree::onPopupViewReference( wxCommandEvent&)
426 WxAutoDialog<WxViewIORDialog> dialog( new WxViewIORDialog(
427 orb,
428 getTreeObject()->Object(),
429 this));
431 dialog->ShowModal();
435 void WxNamingTree::onRMouseUClick( wxMouseEvent& event)
437 wxTreeItemId item = HitTest( event.GetPosition());
438 if (!item) {
439 return;
441 // First select the item, then popup the appropriate menu
442 SelectItem( item);
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);
447 } else {
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)
456 clearChildren();
457 if (!CORBA::is_nil( pRootContext)) {
458 wxTreeItemId item = AddRoot( "Root");
459 SetItemData( item, new WxNamingObject( pRootContext));
460 listContext( item);
461 } else {
462 AddRoot( "<null>");
466 void WxNamingTree::setOrb( CORBA::ORB_ptr pOrb)
468 // This can only be called once!
469 assert( orb == 0);
470 orb = pOrb;