KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > weblogic9 > ui > wizard > WLInstantiatingIterator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.weblogic9.ui.wizard;
20
21 import java.util.*;
22 import java.io.*;
23 import javax.swing.event.*;
24
25 import org.openide.*;
26 import org.openide.util.*;
27 import org.netbeans.modules.j2ee.deployment.plugins.api.*;
28
29 import org.netbeans.modules.j2ee.weblogic9.*;
30
31 /**
32  * The main class of the custom wizard for registering a new server instance.
33  * It performs all the orchestration of the panels and actually creates the
34  * instance.
35  *
36  * @author Kirill Sorokin
37  */

38 public class WLInstantiatingIterator implements WizardDescriptor.InstantiatingIterator {
39     
40     /**
41      * Since the WizardDescriptor does not expose the property name for the
42      * error message label, we have to keep it here also
43      */

44     private final static String JavaDoc PROP_DISPLAY_NAME = "ServInstWizard_displayName"; // NOI18N
45

46     /**
47      * The default debugger port for the instance, it will be assigned to it
48      * at creation time and can be changed via the properties sheet
49      */

50     private static final String JavaDoc DEFAULT_DEBUGGER_PORT = "8787"; // NOI18N
51

52     /**
53      * The parent wizard descriptor
54      */

55     private WizardDescriptor wizardDescriptor;
56     
57     /**
58      * A misterious method whose purpose is obviously in freeing the resources
59      * obtained by the wizard during instance registration. We do not need such
60      * functionality, thus we do not implement it.
61      */

62     public void uninitialize(WizardDescriptor wizardDescriptor) {
63         // do nothing as we do not need to release any resources
64
}
65
66     /**
67      * This method initializes the wizard. AS for us the only thing we should
68      * do is save the wizard descriptor handle.
69      *
70      * @param wizardDescriptor the parent wizard descriptor
71      */

72     public void initialize(WizardDescriptor wizardDescriptor) {
73         this.wizardDescriptor = wizardDescriptor;
74     }
75     
76     /**
77      * Returns the name for the wizard. I failed to find a place where it
78      * could be used, so we do not return anything sensible
79      *
80      * @return the wizard name
81      */

82     public String JavaDoc name() {
83         return ""; // NOI18N
84
}
85
86     /**
87      * This methos actually creates the instance. It fetches all the required
88      * parameters, builds the URL and calls
89      * InstanceProperties.createInstamceProperties(), which registers the
90      * instance.
91      *
92      * @return a set of created instance properties
93      */

94     public Set instantiate() throws IOException {
95         // initialize the resulting set
96
Set result = new HashSet();
97         
98         // build the URL
99
String JavaDoc url = WLDeploymentFactory.URI_PREFIX + this.host + ":" + this.port + ":" + serverRoot; // NOI18N
100

101         // set the username and password
102
String JavaDoc username = this.username;
103         String JavaDoc password = this.password;
104         
105         String JavaDoc displayName = (String JavaDoc)wizardDescriptor.getProperty(PROP_DISPLAY_NAME);
106         
107         // set the additional properties of the instance: server installation
108
// directory, profile root directory, whether the server is local or
109
// remote and the instance name
110
String JavaDoc serverRoot = this.serverRoot;
111         String JavaDoc domainRoot = this.domainRoot;
112         String JavaDoc isLocal = this.isLocal;
113         
114         // if all the data is normally validated - create the instance and
115
// attach the additional properties
116
InstanceProperties ip = InstanceProperties.createInstanceProperties(url, username, password, displayName);
117         ip.setProperty(WLPluginProperties.SERVER_ROOT_ATTR, serverRoot);
118         ip.setProperty(WLPluginProperties.DOMAIN_ROOT_ATTR, domainRoot);
119         ip.setProperty(WLPluginProperties.IS_LOCAL_ATTR, isLocal);
120         ip.setProperty(WLPluginProperties.DEBUGGER_PORT_ATTR, DEFAULT_DEBUGGER_PORT);
121
122         // add the created instance properties to the result set
123
result.add(ip);
124         
125         // return the result
126
return result;
127     }
128     
129     // the main and additional instance properties
130
private String JavaDoc serverRoot;
131     private String JavaDoc domainRoot;
132     private String JavaDoc isLocal;
133     private String JavaDoc host;
134     private String JavaDoc port;
135     private String JavaDoc username;
136     private String JavaDoc password;
137     
138     /**
139      * Setter for the server installation directory
140      *
141      * @param serverRoot the new server installation directory path
142      */

143     public void setServerRoot(String JavaDoc serverRoot) {
144         this.serverRoot = serverRoot;
145         
146         // reinit the instances list
147
serverPropertiesPanel.updateInstancesList();
148     }
149     
150     /**
151      * Getter for the server installation directory
152      *
153      * @return the server installation directory path
154      */

155     public String JavaDoc getServerRoot() {
156         return this.serverRoot;
157     }
158     
159     /**
160      * Setter for the profile root directory
161      *
162      * @param domainRoot the new profile root directory path
163      */

164     public void setDomainRoot(String JavaDoc domainRoot) {
165         this.domainRoot = domainRoot;
166     }
167
168     /**
169      * Getter for the profile root directory
170      *
171      * @return the profile root directory path
172      */

173     public String JavaDoc getDomainRoot() {
174         return domainRoot;
175     }
176
177     /**
178      * Getter for the server host
179      *
180      * @return the server host
181      */

182     public String JavaDoc getHost() {
183         return host;
184     }
185
186     /**
187      * Setter for the server host
188      *
189      * @param host the new server host
190      */

191     public void setHost(String JavaDoc host) {
192         this.host = host;
193     }
194
195     /**
196      * Getter for the server port
197      *
198      * @return the server port
199      */

200     public String JavaDoc getPort() {
201         return port;
202     }
203
204     /**
205      * Setter for the server port
206      *
207      * @param port the new server port
208      */

209     public void setPort(String JavaDoc port) {
210         this.port = port;
211     }
212
213     /**
214      * Getter for the username
215      *
216      * @return the username
217      */

218     public String JavaDoc getUsername() {
219         return username;
220     }
221
222     /**
223      * Setter for the username
224      *
225      * @param username the new username
226      */

227     public void setUsername(String JavaDoc username) {
228         this.username = username;
229     }
230
231     /**
232      * Getter for the password
233      *
234      * @return the password
235      */

236     public String JavaDoc getPassword() {
237         return password;
238     }
239
240     /**
241      * Setter for the password
242      *
243      * @param password the new password
244      */

245     public void setPassword(String JavaDoc password) {
246         this.password = password;
247     }
248     
249     /**
250      * Getter for the isLocal property
251      *
252      * @return "true" if the server is local, "false" otherwise
253      */

254     public String JavaDoc getIsLocal() {
255         return this.isLocal;
256     }
257     
258     /**
259      * Setter for the isLocal property
260      *
261      * @param isLocal "true" if the server is local, "false" otherwise
262      */

263     public void setIsLocal(String JavaDoc isLocal) {
264         this.isLocal = isLocal;
265     }
266     
267     ////////////////////////////////////////////////////////////////////////////
268
// Panels section
269
////////////////////////////////////////////////////////////////////////////
270
/**
271      * The steps names for the wizard: Server Location & Instance properties
272      */

273     private Vector steps = new Vector();
274     {
275         steps.add(NbBundle.getMessage(ServerPropertiesPanel.class, "SERVER_LOCATION_STEP")); // NOI18N
276
steps.add(NbBundle.getMessage(ServerPropertiesPanel.class, "SERVER_PROPERTIES_STEP")); // NOI18N
277
}
278     
279     /**
280      * The wizard's panels
281      */

282     private WizardDescriptor.Panel[] panels;
283     private ServerLocationPanel serverLocationPanel;
284     private ServerPropertiesPanel serverPropertiesPanel;
285     
286     /**
287      * Index of the currently shown panel
288      */

289     private int index = 0;
290     
291     /**
292      * Tells whether the wizard has previous panels. Basically controls the
293      * Back button
294      */

295     public boolean hasPrevious() {
296         return index > 0;
297     }
298
299     /**
300      * Reverts the wizard to the previous panel if available.
301      * If the previous panel is not available a NoSuchElementException will be
302      * thrown.
303      */

304     public void previousPanel() {
305         if (!hasPrevious()) {
306             throw new NoSuchElementException();
307         }
308         index--;
309     }
310
311     /**
312      * Tells whether the wizard has next panels. Basically controls the
313      * Next button
314      */

315     public boolean hasNext() {
316         return index < panels.length - 1;
317     }
318
319     /**
320      * Proceeds the wizard to the next panel if available.
321      * If the next panel is not available a NoSuchElementException will be
322      * thrown.
323      */

324     public void nextPanel() {
325         if (!hasNext()) {
326             throw new NoSuchElementException();
327         }
328         index++;
329     }
330     
331     /**
332      * Returns the current panel of the wizard
333      *
334      * @return current panel of the wizard
335      */

336     public WizardDescriptor.Panel current() {
337         getPanels();
338         return panels[index];
339     }
340     
341     protected final WizardDescriptor.Panel[] getPanels() {
342         if (panels == null) {
343             panels = createPanels();
344         }
345         return panels;
346     }
347     
348     protected WizardDescriptor.Panel[] createPanels() {
349
350         serverLocationPanel = new ServerLocationPanel((String JavaDoc[]) steps.toArray(new String JavaDoc[steps.size()]), 0, new IteratorListener(), this);
351         serverPropertiesPanel = new ServerPropertiesPanel((String JavaDoc[]) steps.toArray(new String JavaDoc[steps.size()]), 1, new IteratorListener(), this);
352
353         return new WizardDescriptor.Panel[] { serverLocationPanel, serverPropertiesPanel };
354     }
355     
356     
357     ////////////////////////////////////////////////////////////////////////////
358
// Listeners section
359
////////////////////////////////////////////////////////////////////////////
360
/**
361      * The registered listeners
362      */

363     private Vector listeners = new Vector();
364     
365     /**
366      * Removes an already registered listener in a synchronized manner
367      *
368      * @param listener a listener to be removed
369      */

370     public void removeChangeListener(ChangeListener listener) {
371         if (listeners != null) {
372             synchronized (listeners) {
373                 listeners.remove(listener);
374             }
375         }
376     }
377
378     /**
379      * Registers a new listener in a synchronized manner
380      *
381      * @param listener a listener to be registered
382      */

383     public void addChangeListener(ChangeListener listener) {
384         synchronized (listeners) {
385             listeners.add(listener);
386         }
387     }
388     
389     /**
390      * Notifies all the listeners of the supplied event
391      *
392      * @param event the event to be passed to the listeners
393      */

394     private void fireChangeEvent(ChangeEvent event) {
395         // copy the registered listeners, to avoid conflicts if the listeners'
396
// list changes
397
Vector targetListeners;
398         synchronized (listeners) {
399             targetListeners = (Vector) listeners.clone();
400         }
401         
402         // notify each listener of the event
403
for (int i = 0; i < targetListeners.size(); i++) {
404             ChangeListener listener = (ChangeListener) targetListeners.elementAt(i);
405             listener.stateChanged(event);
406         }
407     }
408     
409     /**
410      * A simple listener that only notifies the parent iterator of all the
411      * events that come to it
412      *
413      * @author Kirill Sorokin
414      */

415     private class IteratorListener implements ChangeListener {
416         /**
417          * Notifies the parent iterator of the supplied event
418          *
419          * @param event the event to be passed to the parent iterator
420          */

421         public void stateChanged(ChangeEvent event) {
422             fireChangeEvent(event);
423         }
424     }
425     
426 }
427
Popular Tags