1 26 27 28 package org.objectweb.mobilitools.util.corba; 29 30 31 import java.awt.*; 32 import org.omg.CORBA.*; 33 import java.awt.event.*; 34 import org.objectweb.mobilitools.util.gui.AlertWindow; 35 36 62 public class NSbrowser extends Frame implements ActionListener, ItemListener 63 { 64 Button upBtn, downBtn, updateBtn, viewBtn, bindBtn, unbindBtn, destroyBtn; 65 List bindingsLst; 66 NameService my_ns; 67 ORB my_orb; 68 NSbinding[] bindings; 69 String path; 70 static boolean standalone = false; 71 72 73 77 static public void main(String [] args) 78 { 79 try 80 { 81 ORB orb = ORB.init(args, null); 82 standalone = true; 83 new NSbrowser(orb, new NameService(orb)); 84 } 85 catch (NameServiceException e) 86 { 87 System.err.println(e.toString()); 88 e.printStackTrace(); 89 } 90 } 91 92 93 99 NSbrowser(ORB the_orb, NameService the_ns) 100 { 101 my_orb = the_orb; 102 my_ns = the_ns; 103 path = "/"; 104 setLayout(new BorderLayout()); 105 Panel navigationPnl = new Panel(); 106 navigationPnl.setLayout(new FlowLayout()); 107 navigationPnl.add(upBtn = new Button("up")); 108 upBtn.addActionListener(this); 109 navigationPnl.add(downBtn = new Button("down")); 110 downBtn.addActionListener(this); 111 navigationPnl.add(updateBtn = new Button("update")); 112 updateBtn.addActionListener(this); 113 add("North", navigationPnl); 114 add("Center", bindingsLst = new List()); 115 bindingsLst.addActionListener(this); 116 bindingsLst.addItemListener(this); 117 Panel editionPnl = new Panel(); 118 editionPnl.setLayout(new FlowLayout()); 119 editionPnl.add(viewBtn = new Button("view")); 120 viewBtn.addActionListener(this); 121 editionPnl.add(bindBtn = new Button("bind")); 122 bindBtn.addActionListener(this); 123 editionPnl.add(unbindBtn = new Button("unbind")); 124 unbindBtn.addActionListener(this); 125 editionPnl.add(destroyBtn = new Button("destroy")); 126 destroyBtn.addActionListener(this); 127 add("South", editionPnl); 128 updateBindings(); 129 addWindowListener(new OnWindowClosing()); 130 pack(); 131 show(); 132 } 133 134 135 void updateBindings() 136 { 137 try 138 { 139 bindings = my_ns.list(path); 140 if (bindingsLst.getItemCount() > 0) 141 { 142 bindingsLst.removeAll(); 143 } 144 for (int i=0 ; i<bindings.length ; ++i) 145 { 146 if (bindings[i].type == NSbinding.OBJECT) 147 { 148 bindingsLst.add(bindings[i].name + "!" + bindings[i].kind); 149 } 150 else 151 { 152 bindingsLst.add(bindings[i].name + "!" + bindings[i].kind + "/"); 153 } 154 } 155 setTitle(path); 156 viewBtn.setEnabled(false); 157 unbindBtn.setEnabled(false); 158 destroyBtn.setEnabled(false); 159 downBtn.setEnabled(false); 160 if (path.equals("/")) 161 { 162 upBtn.setEnabled(false); 163 } 164 else 165 { 166 upBtn.setEnabled(true); 167 } 168 } 169 catch (NameServiceException e) 170 { 171 new AlertWindow(this, "Name Service Exception", e.toString(), "OK"); 172 e.printStackTrace(); 173 } 174 } 175 176 177 181 182 public void actionPerformed(ActionEvent evt) 183 { 184 if (evt.getSource() == upBtn) 185 { 186 try 187 { 188 path = path.substring(0, 1 + path.substring(0, path.length()-1).lastIndexOf("/")); 189 } 190 catch (StringIndexOutOfBoundsException e) 191 { 192 path = "/"; 193 } 194 updateBindings(); 195 } 196 else if (evt.getSource() == downBtn || evt.getSource() == bindingsLst) 197 { 198 if (bindings[bindingsLst.getSelectedIndex()].type == NSbinding.CONTEXT) 199 { 200 path = path + bindingsLst.getSelectedItem(); 201 updateBindings(); 202 } 203 } 204 else if (evt.getSource() == updateBtn) 205 { 206 updateBindings(); 207 } 208 else if (evt.getSource() == viewBtn) 209 { 210 new AlertWindow( 211 this, 212 path + bindingsLst.getSelectedItem(), 213 my_orb.object_to_string(bindings[bindingsLst.getSelectedIndex()].object), 214 "OK"); 215 } 216 else if (evt.getSource() == bindBtn) 217 { 218 Dialog bindDlg = new Dialog(this, "Add a binding in " + path, true); 219 TextArea refTxt; 220 TextField idFld, kindFld; 221 Checkbox contextChk; 222 Button bindBtn, cancelBtn; 223 Panel namePnl, buttonPnl; 224 225 bindDlg.setLayout(new BorderLayout()); 227 namePnl = new Panel(); 228 namePnl.setLayout(new GridLayout(1,5)); 229 namePnl.add(new Label("id:")); 230 namePnl.add(idFld = new TextField()); 231 namePnl.add(new Label("kind:")); 232 namePnl.add(kindFld = new TextField()); 233 namePnl.add(contextChk = new Checkbox("new context", true)); 234 bindDlg.add("North", namePnl); 235 bindDlg.add("Center", refTxt = new TextArea()); 236 refTxt.setEnabled(false); 237 buttonPnl = new Panel(); 238 buttonPnl.setLayout(new FlowLayout()); 239 buttonPnl.add(bindBtn = new Button("bind")); 240 buttonPnl.add(cancelBtn = new Button("cancel")); 241 bindDlg.add("South", buttonPnl); 242 243 contextChk.addItemListener(new ContextChkMgr(refTxt)); 245 bindDlg.addWindowListener(new BindDlgMgr(bindDlg)); 246 bindBtn.addActionListener(new BindBtnMgr(bindDlg, idFld, kindFld, contextChk, refTxt)); 247 cancelBtn.addActionListener(new CancelBtnMgr(bindDlg)); 248 249 bindDlg.pack(); 250 bindDlg.show(); 251 } 252 else if (evt.getSource() == unbindBtn) 253 { 254 try 255 { 256 my_ns.unbind(path + bindingsLst.getSelectedItem()); 257 } 258 catch (NameServiceException e) 259 { 260 new AlertWindow(this, "Name Service Exception", e.toString(), "OK"); 261 e.printStackTrace(); 262 } 263 updateBindings(); 264 } 265 else if (evt.getSource() == destroyBtn) 266 { 267 try 268 { 269 my_ns.deepUnbind(path + bindingsLst.getSelectedItem()); 270 } 271 catch (NameServiceException e) 272 { 273 new AlertWindow(this, "Name Service Exception", e.toString(), "OK"); 274 e.printStackTrace(); 275 } 276 updateBindings(); 277 } 278 } 279 280 281 285 286 public void itemStateChanged(ItemEvent e) 287 { 288 switch (e.getStateChange()) 289 { 290 case ItemEvent.SELECTED: 291 viewBtn.setEnabled(true); 292 unbindBtn.setEnabled(true); 293 if (bindings[bindingsLst.getSelectedIndex()].type == NSbinding.CONTEXT) 294 { 295 downBtn.setEnabled(true); 296 destroyBtn.setEnabled(true); 297 } 298 else 299 { 300 downBtn.setEnabled(false); 301 destroyBtn.setEnabled(false); 302 } 303 break; 304 case ItemEvent.DESELECTED: 305 downBtn.setEnabled(false); 306 viewBtn.setEnabled(false); 307 unbindBtn.setEnabled(false); 308 destroyBtn.setEnabled(false); 309 break; 310 } 311 } 312 313 314 318 319 class OnWindowClosing extends WindowAdapter 320 { 321 public void windowClosing(WindowEvent e) 322 { 323 NSbrowser.this.dispose(); 324 if (standalone) 325 { 326 System.exit(0); 327 } 328 } 329 } 330 331 332 336 337 class ContextChkMgr implements ItemListener 338 { 339 TextArea txt; 340 341 public ContextChkMgr(TextArea the_txt) 342 { 343 txt = the_txt; 344 } 345 346 public void itemStateChanged(ItemEvent evt) 347 { 348 if (((Checkbox)evt.getSource()).getState()) 349 { 350 txt.setEnabled(false); 351 } 352 else 353 { 354 txt.setEnabled(true); 355 } 356 } 357 } 358 359 class BindDlgMgr extends WindowAdapter 360 { 361 Dialog my_dlg; 362 363 public BindDlgMgr(Dialog the_dlg) 364 { 365 my_dlg = the_dlg; 366 } 367 368 public void windowClosing(WindowEvent event) 369 { 370 my_dlg.dispose(); 371 } 372 } 373 374 class CancelBtnMgr implements ActionListener 375 { 376 Dialog my_dlg; 377 378 public CancelBtnMgr(Dialog the_dlg) 379 { 380 my_dlg = the_dlg; 381 } 382 383 public void actionPerformed(ActionEvent event) 384 { 385 my_dlg.dispose(); 386 } 387 } 388 389 class BindBtnMgr implements ActionListener 390 { 391 Dialog bindDlg; 392 TextField idFld, kindFld; 393 Checkbox contextChk; 394 TextArea refTxt; 395 396 public BindBtnMgr(Dialog bind, 397 TextField id, 398 TextField kind, 399 Checkbox context, 400 TextArea ref) 401 { 402 bindDlg = bind; 403 idFld = id; 404 kindFld = kind; 405 contextChk = context; 406 refTxt = ref; 407 } 408 409 public void actionPerformed(ActionEvent event) 410 { 411 if (contextChk.getState()) 412 { 413 try 414 { 415 NSbrowser.this.my_ns.makePath( 416 NSbrowser.this.path + idFld.getText() + "!" + kindFld.getText()); 417 } 418 catch (NameServiceException e) 419 { 420 new AlertWindow(NSbrowser.this, "Cannot create the new context", e.toString(), "OK"); 421 } 422 } 423 else 424 { 425 try 426 { 427 NSbrowser.this.my_ns.bind( 428 NSbrowser.this.path + idFld.getText() + "!" + kindFld.getText(), 429 NSbrowser.this.my_orb.string_to_object(refTxt.getText()) 430 ); 431 } 432 catch (SystemException e) 433 { 434 new AlertWindow(NSbrowser.this, "Invalid Object Reference", e.toString(), "OK"); 435 } 436 catch (NameServiceException e) 437 { 438 new AlertWindow(NSbrowser.this, "Name Service Exception", e.toString(), "OK"); 439 } 440 } 441 updateBindings(); 442 bindDlg.dispose(); 443 } 444 } 445 } 446 | Popular Tags |