KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
22 import java.util.*;
23 import java.awt.*;
24 import java.awt.event.*;
25 import javax.swing.*;
26 import javax.swing.event.*;
27 import javax.swing.filechooser.*;
28 import org.netbeans.modules.j2ee.weblogic9.WLPluginProperties;
29
30 import org.openide.*;
31 import org.openide.util.*;
32
33 /**
34  * The first panel of the custom wizard used to register new server instance.
35  * User is required to enter the local server's installation directory at this
36  * phase.
37  *
38  * @author Kirill Sorokin
39  */

40 public class ServerLocationPanel extends JPanel implements WizardDescriptor.Panel {
41     /**
42      * Since the WizardDescriptor does not expose the property name for the
43      * error message label, we have to keep it here also
44      */

45     private final static String JavaDoc PROP_ERROR_MESSAGE = "WizardPanel_errorMessage"; // NOI18N
46

47     /**
48      * The parent wizard descriptor handle
49      */

50     private transient WizardDescriptor wizardDescriptor;
51     
52     /**
53      * The parent instantiaing iterator handle
54      */

55     private transient WLInstantiatingIterator instantiatingIterator;
56     
57     /**
58      * Creates a new instance of the ServerLocationPanel. It initializes all the
59      * GUI components that appear on the panel.
60      *
61      * @param steps the names of the steps in the wizard
62      * @param index index of this panel in the wizard
63      * @param listener a listener that will propagate the chage event higher in
64      * the hierarchy
65      * @param instantiatingIterator the parent instantiating iterator
66      */

67     public ServerLocationPanel(String JavaDoc[] steps, int index, ChangeListener listener, WLInstantiatingIterator instantiatingIterator) {
68         // save the instantiating iterator
69
this.instantiatingIterator = instantiatingIterator;
70         
71         // set the required properties, so that the panel appear correct in
72
// the steps
73
putClientProperty("WizardPanel_contentData", steps); // NOI18N
74
putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(index)); // NOI18N
75

76         // register the supplied listener
77
addChangeListener(listener);
78         
79         // set the panel's name
80
setName(steps[index]);
81         
82         // init the GUI
83
init();
84     }
85     
86     /**
87      * Returns the named help article associated with this panel
88      *
89      * @return the associated help article
90      */

91     public HelpCtx getHelp() {
92         return new HelpCtx("j2eeplugins_registering_app_server_weblogic_location"); // NOI18N
93
}
94     
95     /**
96      * Gets the panel's AWT Component object, in our case it coincides with this
97      * object
98      *
99      * @return this
100      */

101     public Component getComponent() {
102         return this;
103     }
104     
105     /**
106      * Checks whether the data input is valid
107      *
108      * @return true if the entered installation directory is valid, false
109      * otherwise
110      */

111     public boolean isValid() {
112         // clear the error message
113
wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, "");
114
115         // test if IDE is run on correct JDK version
116
if (!WLPluginProperties.runningOnCorrectJdk()) {
117             String JavaDoc msg = NbBundle.getMessage(ServerLocationPanel.class, "ERR_INVALID_JDK");
118             wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, msg);
119             return false;
120         }
121                 
122         // check for the validity of the entered installation directory
123
// if it's invalid, return false
124
File JavaDoc serverRoot = new File JavaDoc (locationField.getText());
125         
126         if (!WLPluginProperties.isSupportedVersion(serverRoot)) {
127             String JavaDoc msg = NbBundle.getMessage(ServerLocationPanel.class, "ERR_INVALID_SERVER_VERSION");
128             wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, msg);
129             return false;
130         }
131         
132         if (!WLPluginProperties.isGoodServerLocation(serverRoot)) {
133             String JavaDoc msg = NbBundle.getMessage(ServerLocationPanel.class, "ERR_INVALID_SERVER_ROOT");
134             wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, msg);
135             return false;
136         }
137
138         if (!WLPluginProperties.domainListExists(serverRoot)) {
139             String JavaDoc msg = NbBundle.getMessage(ServerLocationPanel.class, "ERR_INVALID_SERVER_ROOT") +
140                          " " +
141                          NbBundle.getMessage(ServerLocationPanel.class, "DOMAIN_LIST_NOT_FOUND",
142                             serverRoot.getPath() + File.separator + WLPluginProperties.DOMAIN_LIST
143                          );
144             wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, msg);
145             return false;
146         }
147
148         
149         WLPluginProperties.getInstance().setInstallLocation(locationField.getText());
150         WLPluginProperties.getInstance().saveProperties();
151         // set the server root in the parent instantiating iterator
152
instantiatingIterator.setServerRoot(locationField.getText());
153         
154         // everything seems ok
155
return true;
156     }
157     
158     ////////////////////////////////////////////////////////////////////////////
159
// JPanel section
160
////////////////////////////////////////////////////////////////////////////
161
private JButton locationBrowseButton;
162     private JLabel locationLabel;
163     private JTextField locationField;
164     private JPanel formattingPanel;
165     
166     /**
167      * Inits the GUI components
168      */

169     private void init() {
170         // we use the GridBagLayout so we need the GridBagConstraints to
171
// properly place the components
172
GridBagConstraints gridBagConstraints;
173         
174         // initialize the components
175
locationLabel = new JLabel();
176         locationField = new JTextField();
177         locationBrowseButton = new JButton();
178         formattingPanel = new JPanel();
179         
180         // set the desired layout
181
setLayout(new GridBagLayout());
182         
183         // add server installation directory field label
184
locationLabel.setText(NbBundle.getMessage(ServerLocationPanel.class, "LBL_SERVER_LOCATION")); // NOI18N
185
gridBagConstraints = new GridBagConstraints();
186         gridBagConstraints.gridx = 0;
187         gridBagConstraints.gridy = 0;
188         gridBagConstraints.anchor = GridBagConstraints.EAST;
189         locationLabel.setLabelFor(locationField);
190         add(locationLabel, gridBagConstraints);
191         
192         // add server installation directory field
193
locationField.setColumns(15);
194         locationField.addKeyListener(new LocationKeyListener());
195         String JavaDoc loc = WLPluginProperties.getInstance().getInstallLocation();
196         if (loc != null) { // NOI18N
197
locationField.setText(loc);
198         }
199         gridBagConstraints = new GridBagConstraints();
200         gridBagConstraints.gridx = 1;
201         gridBagConstraints.gridy = 0;
202         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
203         gridBagConstraints.weightx = 1.0;
204         gridBagConstraints.insets = new Insets(0, 10, 0, 10);
205         locationField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerLocationPanel.class, "ACSD_ServerLocationPanel_locationField")); // NOI18N
206
add(locationField, gridBagConstraints);
207         
208         // add server installation directory field browse button
209
org.openide.awt.Mnemonics.setLocalizedText(locationBrowseButton, NbBundle.getMessage(ServerLocationPanel.class, "LBL_BROWSE_BUTTON"));
210         locationBrowseButton.addActionListener(new BrowseActionListener());
211         gridBagConstraints = new GridBagConstraints();
212         gridBagConstraints.gridx = 2;
213         gridBagConstraints.gridy = 0;
214         gridBagConstraints.anchor = GridBagConstraints.WEST;
215         locationBrowseButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerLocationPanel.class, "ACSD_ServerLocationPanel_locationBrowseButton")); // NOI18N
216
add(locationBrowseButton, gridBagConstraints);
217         
218         // add the empty panel, that will take up all the remaining space
219
gridBagConstraints = new GridBagConstraints();
220         gridBagConstraints.gridx = 0;
221         gridBagConstraints.gridy = 1;
222         gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
223         gridBagConstraints.weighty = 1.0;
224         add(formattingPanel, gridBagConstraints);
225     }
226     
227     /**
228      * An instance of the fileschooser that is used for locating the server
229      * installation directory
230      */

231     private JFileChooser fileChooser;
232     
233     /**
234      * Shows the filechooser set to currently selected directory or to the
235      * default system root if the directory is invalid
236      */

237     private void showFileChooser() {
238         
239         if (fileChooser == null) {
240             fileChooser = new JFileChooser();
241         }
242         
243         // set the chooser's properties
244
fileChooser.setFileFilter(new DirectoryFileFilter());
245         fileChooser.setMultiSelectionEnabled(false);
246         fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
247         
248         // set the current directory
249
File JavaDoc currentLocation = new File JavaDoc(locationField.getText());
250         if (currentLocation.exists() && currentLocation.isDirectory()) {
251             fileChooser.setCurrentDirectory(currentLocation.getParentFile());
252             fileChooser.setSelectedFile(currentLocation);
253         }
254         
255         // wait for the user to choose the directory and if he clicked the OK
256
// button store the selected directory in the server location field
257
if (fileChooser.showOpenDialog(this) == fileChooser.APPROVE_OPTION) {
258             locationField.setText(fileChooser.getSelectedFile().getPath());
259             fireChangeEvent();
260         }
261     }
262     
263     ////////////////////////////////////////////////////////////////////////////
264
// Settings section
265
////////////////////////////////////////////////////////////////////////////
266
/**
267      * Reads the supplied setting. The only one that can arrive this way is the
268      * WizardDescriptor, thus we only convert the incoming object and save
269      *
270      * @param object the incoming setting (WizardDescriptor)
271      */

272     public void readSettings(Object JavaDoc object) {
273         this.wizardDescriptor = (WizardDescriptor) object;
274     }
275     
276     /**
277      * Stores the supplied setting. I don't know the purpose of this method
278      * thus we do not implement it
279      */

280     public void storeSettings(Object JavaDoc object) {}
281     
282     ////////////////////////////////////////////////////////////////////////////
283
// Listeners section
284
////////////////////////////////////////////////////////////////////////////
285
/**
286      * The registrered listeners vector
287      */

288     private Vector listeners = new Vector();
289     
290     /**
291      * Removes a registered listener
292      *
293      * @param listener the listener to be removed
294      */

295     public void removeChangeListener(ChangeListener listener) {
296         if (listeners != null) {
297             synchronized (listeners) {
298                 listeners.remove(listener);
299             }
300         }
301     }
302     
303     /**
304      * Adds a listener
305      *
306      * @param listener the listener to be added
307      */

308     public void addChangeListener(ChangeListener listener) {
309         synchronized (listeners) {
310             listeners.add(listener);
311         }
312     }
313     
314     /**
315      * Fires a change event originating from this panel
316      */

317     private void fireChangeEvent() {
318         ChangeEvent event = new ChangeEvent(this);
319         fireChangeEvent(event);
320     }
321     
322     /**
323      * Fires a custom change event
324      *
325      * @param event the event
326      */

327     private void fireChangeEvent(ChangeEvent event) {
328         Vector targetListeners;
329         synchronized (listeners) {
330             targetListeners = (Vector) listeners.clone();
331         }
332         
333         for (int i = 0; i < targetListeners.size(); i++) {
334             ChangeListener listener = (ChangeListener) targetListeners.elementAt(i);
335             listener.stateChanged(event);
336         }
337     }
338     
339     ////////////////////////////////////////////////////////////////////////////
340
// Inner Classes
341
////////////////////////////////////////////////////////////////////////////
342
/**
343      * Simple key listener that delegates the event to its parent's listeners
344      *
345      * @author Kirill Sorokin
346      */

347     private class LocationKeyListener extends KeyAdapter {
348         /**
349          * This method is called when a user presses a key on the keyboard
350          */

351         public void keyTyped(KeyEvent event) {
352             fireChangeEvent();
353         }
354         
355         /**
356          * This method is called when a user releases a key on the keyboard
357          */

358         public void keyReleased(KeyEvent event) {
359             fireChangeEvent();
360         }
361     }
362     
363     /**
364      * Simple listener that reacts on the user's clicking the Browse button
365      *
366      * @author Kirill Sorokin
367      */

368     private class BrowseActionListener implements ActionListener {
369         /**
370          * this methos is called when a user clicks Browse and show the file
371          * chooser dialog in response
372          */

373         public void actionPerformed(ActionEvent event) {
374             showFileChooser();
375         }
376     }
377     
378     /**
379      * An extension of the FileFilter class that is setup to accept only
380      * directories.
381      *
382      * @author Kirill Sorokin
383      */

384     private static class DirectoryFileFilter extends FileFilter {
385         /**
386          * This method is called when it is needed to decide whether a chosen
387          * file meets the filter's requirements
388          *
389          * @return true if the file meets the requirements, false otherwise
390          */

391         public boolean accept(File JavaDoc file) {
392             // if the file exists and it's a directory - accept it
393
if (file.exists() && file.isDirectory()) {
394                 return true;
395             }
396             
397             // in all other cases - refuse
398
return false;
399         }
400         
401         /**
402          * Returns the description of file group described by this filter
403          *
404          * @return group name
405          */

406         public String JavaDoc getDescription() {
407             return NbBundle.getMessage(ServerLocationPanel.class, "DIRECTORIES_FILTER_NAME"); // NOI18N
408
}
409     }
410 }
411
Popular Tags