KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > explorer > CosNaming > BindObject


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 package org.objectweb.openccm.explorer.CosNaming;
27
28 import java.awt.Dimension JavaDoc;
29 import java.io.BufferedReader JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileReader JavaDoc;
32
33 import org.objectweb.openccm.explorer.menu.JFileChooserSingleton;
34 import org.objectweb.openccm.explorer.menu.OpenCCMBrowserConstants;
35 import org.objectweb.openccm.explorer.menu.TreeDialogSingleton;
36 import org.objectweb.util.explorer.api.MenuItem;
37 import org.objectweb.util.explorer.api.MenuItemTreeView;
38 import org.objectweb.util.explorer.api.TreeView;
39 import org.objectweb.util.explorer.swing.gui.api.DialogAction;
40 import org.objectweb.util.explorer.swing.gui.api.DialogBox;
41 import org.objectweb.util.explorer.swing.gui.lib.DefaultDialogBox;
42 import org.objectweb.util.explorer.swing.gui.lib.FileChooserBox;
43 import org.objectweb.util.explorer.swing.gui.lib.LabelBox;
44 import org.objectweb.util.explorer.swing.gui.lib.TreeBox;
45 import org.omg.CORBA.ORB JavaDoc;
46 import org.omg.CosNaming.NamingContextExt JavaDoc;
47
48 /**
49  * This action binds an object into the NameService.
50  *
51  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
52  * @version 0.1
53  */

54 public class BindObject
55   implements MenuItem, DialogAction
56 {
57
58     //==================================================================
59
//
60
// Internal states.
61
//
62
//==================================================================
63

64     /** The dialog box */
65     protected DialogBox dialog_;
66     
67     /** The associated NamingContext */
68     protected NamingContextExt JavaDoc namingContext_ = null;
69
70     /** The name of the object */
71     protected LabelBox name_ = null;
72
73     /** The IOR file to use to bind the object */
74     protected FileChooserBox iorFile_ = null;
75
76     /** The tree used to select the object to bind */
77     protected TreeBox tree_ = null;
78
79     //==================================================================
80
//
81
// No constructor.
82
//
83
//==================================================================
84

85     //==================================================================
86
//
87
// Internal methods.
88
//
89
//==================================================================
90

91     /**
92      * Binds the object into the current NameService
93      */

94     protected void bind(NamingContextExt JavaDoc naming, String JavaDoc id, org.omg.CORBA.Object JavaDoc o) {
95         try {
96             naming.rebind(naming.to_name(id), o);
97         } catch (org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc e) {
98             System.out.println(getClass().getName() + " : Object Not Found Exception !");
99         } catch (org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc e) {
100             System.out.println(getClass().getName() + " : CannotProceed Exception");
101         } catch (org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc e) {
102             System.out.println(getClass().getName() + " : InvalidName Exception");
103         }
104     }
105
106     /**
107      * Creates a box containing all the box to specify all the params
108      */

109     protected void createBox() {
110         name_ = new LabelBox("Object's name");
111         dialog_.addElementBox(name_);
112         iorFile_ = new FileChooserBox("IOR file",JFileChooserSingleton.getInstance(OpenCCMBrowserConstants.IOR_FILE_CHOOSER),false);
113         dialog_.addElementBox(iorFile_);
114         tree_ = new TreeBox(TreeDialogSingleton.getInstance(),false);
115         tree_.setPreferredSize(new Dimension JavaDoc(350, 150));
116         dialog_.addElementBox(tree_);
117     }
118
119     //==================================================================
120
//
121
// Public methods for MenuItem interface.
122
//
123
//==================================================================
124

125     /* (non-Javadoc)
126      * @see org.objectweb.util.explorer.api.MenuItem#getStatus(org.objectweb.util.explorer.api.TreeView)
127      */

128     public int getStatus(TreeView treeView) {
129         return MenuItem.ENABLED_STATUS;
130     }
131
132     /* (non-Javadoc)
133      * @see org.objectweb.util.explorer.api.MenuItem#actionPerformed(org.objectweb.util.explorer.api.MenuItemTreeView)
134      */

135     public void actionPerformed(MenuItemTreeView e) {
136         namingContext_ = (NamingContextExt JavaDoc) e.getSelectedObject();
137         dialog_ = new DefaultDialogBox("Binds an object into the NameService");
138         createBox();
139         dialog_.setValidateAction(this);
140         dialog_.show();
141     }
142
143     //==================================================================
144
//
145
// Public methods for DialogAction interface.
146
//
147
//==================================================================
148

149     /* (non-Javadoc)
150      * @see org.objectweb.util.explorer.swing.gui.api.DialogAction#executeAction()
151      */

152     public void executeAction() throws Exception JavaDoc {
153         File JavaDoc file = iorFile_.getFile();
154         Object JavaDoc selectedObject = tree_.getObject();
155         org.omg.CORBA.Object JavaDoc object = null;
156         if (file != null) {
157             String JavaDoc ior = null;
158             try {
159                 BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
160                 ior = in.readLine();
161             } catch (java.io.FileNotFoundException JavaDoc err) {
162                 System.out.println(file + " : File not found !");
163             } catch (java.io.IOException JavaDoc err) {
164                 System.out.println(file + " : Error while reading !");
165             }
166             if (ior != null && ior.startsWith("IOR:")) {
167                 ORB JavaDoc orb = org.objectweb.openccm.corba.TheORB.getORB();
168                 object = orb.string_to_object(ior);
169             }
170         } else if (selectedObject != null) {
171             try {
172                 object = (org.omg.CORBA.Object JavaDoc) selectedObject;
173             } catch (ClassCastException JavaDoc e1) {
174                 throw new Exception JavaDoc("You must select a org.omg.CORBA.Object \n Found : " + selectedObject.getClass().getName());
175             }
176         }
177         if (object != null)
178             bind(namingContext_, name_.getLabel(), object);
179         else
180             throw new Exception JavaDoc("The valid object to bind is expected !");
181     }
182 }
183
Popular Tags