KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > mobilitools > util > corba > NSbrowser


1 /*
2 * MobiliTools: an implementation of the Object Management Group's
3 * Mobile Agent Facility specification.
4 * Copyright (C) 2003 France Telecom R&D
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * MobiliTools $Name: $
21 *
22 * Contact: mobilitools-smi@lists.debian-sf.objectweb.org
23 *
24 * Authors: Bruno Dillenseger
25 */

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 /**
37  * MobiliTools $Name: $, $Id: NSbrowser.java,v 1.1.1.1 2003/03/28 14:48:06 dillense Exp $
38  * <P>
39  * Browser for CORBA name service.
40  * This GUI is based on the NameService wrapper (@see NameService).
41  * It can be used:
42  * <UL>
43  * <LI>either directly as a standalone programme, to browse the default context of the default CORBA name service;
44  * <LI>or from any programme by providing a NameService object to the constructor, to browse any name service from any context.
45  * </UL>
46  * Buttons:
47  * <UL>
48  * <LI>"up", to go to the parent context;
49  * <LI>"down", to list the selected context (a double-click does the same);
50  * <LI>"update", to re-list the content of current context;
51  * <LI>"view", to see the string'fied IOR of the selected object or context;
52  * <LI>"bind", to bind an object or a context in current context. If a new context
53  * is created, then a name (id and kind) is sufficient. Otherwise, one must provide
54  * also the string'fied IOR of the object or context to bind.
55  * <LI>"unbind", to unbind the selected object or context. Note that removing a context
56  * doesn't unbind its children objects, and does not destroy the context.
57  * <LI>"destroy", to destroy the selected context, and destroy its children contexts recursively,
58  * while removing any binding in those contexts.
59  * </UL>
60  * @see NameService
61 */

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 JavaDoc path;
70     static boolean standalone = false;
71
72
73     /**
74         Opens a CORBA naming service GUI for the default context of the default name service
75         (i.e. from the initial reference).
76     */

77     static public void main(String JavaDoc[] 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     /**
94         Opens a GUI for the specified NameService object.
95         @param the_orb is a reference to the ORB object (see CORBA's ORB_init())
96         @param the_ns the NameService object
97         @see NameService
98     */

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     ////////////////////////////////////////////////
178
// Implementation of interface ActionListener //
179
////////////////////////////////////////////////
180

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 JavaDoc 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             // GUI layout
226
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             // GUI listeners setup
244
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     //////////////////////////////////////////////
282
// Implementation of interface ItemListener //
283
//////////////////////////////////////////////
284

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     ///////////////////////////////////////////////////////////
315
// inner WindowAdapter-derived class for the main window //
316
///////////////////////////////////////////////////////////
317

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     /////////////////////////////////////////////////
333
// inner classes for the "bind" option's window //
334
/////////////////////////////////////////////////
335

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