KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > explorer > menu > LoadIORFileAction


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2003 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.menu;
27
28 import java.awt.Component JavaDoc;
29 import java.awt.Dimension JavaDoc;
30 import java.io.BufferedReader JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.FileReader JavaDoc;
33
34 import javax.swing.ImageIcon JavaDoc;
35
36 import org.objectweb.util.explorer.swing.gui.api.DialogAction;
37 import org.objectweb.util.explorer.swing.gui.api.DialogBox;
38 import org.objectweb.util.explorer.swing.gui.lib.DefaultDialogBox;
39 import org.objectweb.util.explorer.swing.gui.lib.TreeBox;
40 import org.omg.CORBA.ORB JavaDoc;
41
42 /**
43  * This class represents the action which loads an IOR file into to initial context.
44  *
45  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
46  *
47  * @version 0.1
48  */

49 public class LoadIORFileAction
50     extends AbstractMenuAction
51     implements DialogAction {
52
53     //==================================================================
54
//
55
// Internal states.
56
//
57
//==================================================================
58

59     /** The name of the object */
60     protected org.objectweb.util.explorer.swing.gui.lib.LabelBox name_ = null;
61
62     /** The IOR file to use to bind the object */
63     protected org.objectweb.util.explorer.swing.gui.lib.FileChooserBox iorFile_ = null;
64
65     /** The tree used to select the object to bind */
66     protected TreeBox treeBox_ = null;
67
68     //==================================================================
69
//
70
// Constructors.
71
//
72
//==================================================================
73

74     /**
75      * The default constructor
76      */

77     public LoadIORFileAction(String JavaDoc nom, ImageIcon JavaDoc image, String JavaDoc desc, Integer JavaDoc mnemonic, org.objectweb.fractal.api.Component tree, Component JavaDoc parent) {
78         super(nom, image, desc, mnemonic, tree);
79     }
80
81     //==================================================================
82
//
83
// Internal methods.
84
//
85
//==================================================================
86

87     /**
88      * Creates a box containing all the box to specify all the params
89      */

90     protected void createBox(DialogBox dialogBox) {
91         name_ = new org.objectweb.util.explorer.swing.gui.lib.LabelBox("Object's name");
92         dialogBox.addElementBox(name_);
93         iorFile_ = new org.objectweb.util.explorer.swing.gui.lib.FileChooserBox("IOR file",JFileChooserSingleton.getInstance(OpenCCMBrowserConstants.IOR_FILE_CHOOSER),false);
94         dialogBox.addElementBox(iorFile_);
95         treeBox_ = new TreeBox(TreeDialogSingleton.getInstance(),false);
96         treeBox_.setPreferredSize(new Dimension JavaDoc(350, 150));
97         dialogBox.addElementBox(treeBox_);
98     }
99
100     //==================================================================
101
//
102
// Public methods.
103
//
104
//==================================================================
105

106     /**
107      * Shows a JFileChooser component and tries to load the new configuration file
108      */

109     public void actionPerformed(java.awt.event.ActionEvent JavaDoc ae) {
110         DialogBox dialogBox = new DefaultDialogBox("Adds a CORBA object");
111         createBox(dialogBox);
112         dialogBox.setValidateAction(this);
113         dialogBox.show();
114     }
115     
116     //==================================================================
117
//
118
// Public methods for DialogAction interface.
119
//
120
//==================================================================
121

122     /**
123      * Executes an action
124      */

125     public void executeAction() throws Exception JavaDoc {
126         File JavaDoc file = iorFile_.getFile();
127         Object JavaDoc selectedObject = treeBox_.getObject();
128         org.omg.CORBA.Object JavaDoc object = null;
129         if (file != null) {
130             String JavaDoc ior = null;
131             try {
132                 BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
133                 ior = in.readLine();
134             } catch (java.io.FileNotFoundException JavaDoc err) {
135                 System.out.println(file + " : File not found !");
136             } catch (java.io.IOException JavaDoc err) {
137                 System.out.println(file + " : Error while reading !");
138             }
139             if (ior != null && ior.startsWith("IOR:")) {
140                 ORB JavaDoc orb = org.objectweb.openccm.corba.TheORB.getORB();
141                 object = orb.string_to_object(ior);
142             }
143         } else if (selectedObject != null) {
144             try {
145                 object = (org.omg.CORBA.Object JavaDoc) selectedObject;
146             } catch (ClassCastException JavaDoc e1) {
147                 throw new Exception JavaDoc("You must select a org.omg.CORBA.Object \n Found : " + selectedObject.getClass().getName());
148             }
149         }
150         if (object != null)
151             treeItf_.addEntry(name_.getLabel(), object);
152         else
153             throw new Exception JavaDoc("The object to bind is missing !");
154     }
155
156 }
157
Popular Tags