KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > admin > control > CreateInstanceAction


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.admin.control;
25
26 import org.objectweb.fractal.api.control.BindingController;
27 import org.objectweb.fractal.gui.model.Component;
28 import org.objectweb.fractal.gui.model.Configuration;
29 import org.objectweb.fractal.swing.AbstractAction;
30 import org.objectweb.fractal.gui.admin.model.AdminModel;
31 import org.objectweb.fractal.gui.selection.model.Selection;
32 import org.objectweb.fractal.gui.selection.model.SelectionListener;
33 import org.objectweb.fractal.rmi.registry.*;
34
35 import org.objectweb.fractal.util.Fractal;
36
37 import java.awt.event.ActionEvent JavaDoc;
38 import java.net.URL JavaDoc;
39
40 import javax.swing.ImageIcon JavaDoc;
41 import javax.swing.KeyStroke JavaDoc;
42 import javax.swing.JOptionPane JavaDoc;
43
44 /**
45  * An action to exit FractalGUI.
46  */

47
48 public class CreateInstanceAction extends AbstractAction
49   implements BindingController, SelectionListener
50 {
51   /**
52    * A mandatory client interface bound to a {@link Configuration configuration}
53    * model. This is the configuration that is saved by this action.
54    */

55
56   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
57
58   /**
59    * A mandatory client interface bound to a {@link AdminModel AdminModel}
60    * model.
61    */

62
63   public final static String JavaDoc ADMIN_MODEL_BINDING = "admin-model";
64
65   /**
66    * An mandatory client interface bound to a {@link Selection Selection}.
67    */

68
69   public final static String JavaDoc SELECTION_BINDING = "selection";
70
71   /**
72    * The configuration client interface.
73    */

74
75   private Configuration configuration;
76
77   /**
78    * The AdminModel client interface.
79    */

80
81   private AdminModel adminmodel;
82
83   /**
84    * The SelectionModel client interface.
85    */

86
87   private Selection selection;
88
89
90   /**
91    * Constructs a new {@link CreateInstanceAction} component.
92    */

93
94   public CreateInstanceAction () {
95     putValue(NAME, "Create Instance");
96     putValue(SHORT_DESCRIPTION, "CreateInstance");
97     URL JavaDoc url = getClass().getResource(
98       "/org/objectweb/fractal/gui/resources/empty.gif");
99     putValue(SMALL_ICON, new ImageIcon JavaDoc(url));
100     putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control shift C"));
101   }
102
103   // -------------------------------------------------------------------------
104
// Implementation of the BindingController interface
105
// -------------------------------------------------------------------------
106

107   public String JavaDoc[] listFc () {
108     return new String JavaDoc[] {
109       CONFIGURATION_BINDING,
110       ADMIN_MODEL_BINDING,
111       SELECTION_BINDING
112     };
113   }
114
115   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
116     if (CONFIGURATION_BINDING.equals(clientItfName)) {
117       return configuration;
118     } else if (ADMIN_MODEL_BINDING.equals(clientItfName)) {
119       return adminmodel;
120     } else if (SELECTION_BINDING.equals(clientItfName)) {
121       return selection;
122     }
123     return null;
124   }
125
126   public void bindFc (final String JavaDoc clientItfName, final Object JavaDoc serverItf) {
127     if (CONFIGURATION_BINDING.equals(clientItfName)) {
128       configuration = (Configuration)serverItf;
129     } else if (ADMIN_MODEL_BINDING.equals(clientItfName)) {
130       adminmodel = (AdminModel)serverItf;
131     } else if (SELECTION_BINDING.equals(clientItfName)) {
132       selection = (Selection)serverItf;
133     }
134   }
135
136   public void unbindFc (final String JavaDoc clientItfName) {
137     if (CONFIGURATION_BINDING.equals(clientItfName)) {
138       configuration = null;
139     } else if (ADMIN_MODEL_BINDING.equals(clientItfName)) {
140       adminmodel = null;
141     } else if (SELECTION_BINDING.equals(clientItfName)) {
142       selection = null;
143     }
144   }
145
146   // -------------------------------------------------------------------------
147
// Implementation of the SelectionListener interface
148
// -------------------------------------------------------------------------
149

150   public void selectionChanged () {
151     Object JavaDoc o = selection.getSelection();
152     if (o instanceof Component) {
153       setEnabled (true);
154     } else {
155       setEnabled(false);
156     }
157   }
158
159   // -------------------------------------------------------------------------
160
// Implementation of the ActionListener interface
161
// -------------------------------------------------------------------------
162

163   public void actionPerformed (final ActionEvent JavaDoc e) {
164
165     Object JavaDoc o = selection.getSelection();
166
167     String JavaDoc classpath = System.getProperty("java.class.path");
168     //System.setProperty("java.rmi.server.codebase", configuration.getStorage());
169

170     try {
171       if (o instanceof Component) {
172         Component c = (Component)o;
173
174         org.objectweb.fractal.api.Component boot = null;
175
176         String JavaDoc rmi_url = "localhost";
177         String JavaDoc port = null;
178         String JavaDoc name = "localhost";
179         Integer JavaDoc in = new Integer JavaDoc (1234);
180         String JavaDoc oneMoreTime = "";
181         String JavaDoc debut = "P";
182         while (true) {
183           String JavaDoc input = (String JavaDoc)JOptionPane.showInputDialog (
184             null,
185             "TODO",
186             debut+"lease input "+oneMoreTime+"the URL for wished registry"+"\n machine (host:port/name)",
187             JOptionPane.QUESTION_MESSAGE,
188             null,
189             null,
190             rmi_url+":1234/"+name);
191           if (input != null) {
192             int ina = input.indexOf(':');
193             int inb = input.indexOf('/');
194             if ((ina < 1) || (inb < ina)) {
195               oneMoreTime = "AGAIN ";
196               debut = "ERROR : p";
197               continue;
198             }
199             rmi_url = input.substring(0, ina);
200             name = input.substring(inb+1);
201             port = input.substring(ina+1, inb);
202             in = new Integer JavaDoc (port);
203             if (in.intValue() < 80) {
204               oneMoreTime = "AGAIN ";
205               debut = "ERROR port number : p";
206               continue;
207             }
208
209             if ((rmi_url.equals("localhost")) &&
210                 (port.equals("1234")) &&
211                 (name.equals("localhost"))) {
212                 boot = Fractal.getBootstrapComponent();
213             }
214             else {
215               NamingService ns = Registry.getRegistry (rmi_url, in.intValue());
216               boot = ns.lookup (name);
217             }
218           }
219           else { return; }
220           break;
221         }
222
223         try {
224           adminmodel.createInstance (c, boot);
225         } catch (Exception JavaDoc ex) {
226           JOptionPane.showMessageDialog (
227             null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
228             return;
229         }
230       }
231     } catch (Exception JavaDoc ex) {
232       JOptionPane.showMessageDialog (
233         null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
234     }
235   }
236 }
237
238
Popular Tags