KickJava   Java API By Example, From Geeks To Geeks.

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

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

49     private final static String JavaDoc PROP_ERROR_MESSAGE =
50             "WizardPanel_errorMessage"; // NOI18N
51

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

55     private transient WizardDescriptor wizardDescriptor;
56     
57     /**
58      * The parent instantiaing iterator handle
59      */

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

72     public ServerLocationPanel(String JavaDoc[] steps, int index,
73             ChangeListener listener,
74             WSInstantiatingIterator instantiatingIterator) {
75         // save the instantiating iterator
76
this.instantiatingIterator = instantiatingIterator;
77         
78         // set the required properties, so that the panel appear correct in
79
// the steps
80
putClientProperty("WizardPanel_contentData", steps); // NOI18N
81
putClientProperty("WizardPanel_contentSelectedIndex", // NOI18N
82
new Integer JavaDoc(index));
83         
84         // register the supplied listener
85
addChangeListener(listener);
86         
87         // set the panel's name
88
setName(steps[index]);
89         
90         // init the GUI
91
init();
92     }
93     
94     /**
95      * Returns the named help article associated with this panel
96      *
97      * @return the associated help article
98      */

99     public HelpCtx getHelp() {
100         return new HelpCtx("j2eeplugins_registering_app_" + // NOI18N
101
"server_websphere"); // NOI18N
102
}
103     
104     /**
105      * Gets the panel's AWT Component object, in our case it coincides with this
106      * object
107      *
108      * @return this
109      */

110     public Component getComponent() {
111         return this;
112     }
113     
114     /**
115      * Checks whether the data input is valid
116      *
117      * @return true if the entered installation directory is valid, false
118      * otherwise
119      */

120     public boolean isValid() {
121         // clear the error message
122
wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, "");
123         
124         // check for the validity of the entered installation directory
125
// if it's invalid, return false
126
if (!isValidServerRoot(locationField.getText())) {
127             wizardDescriptor.putProperty(PROP_ERROR_MESSAGE,
128                     NbBundle.getMessage(ServerLocationPanel.class,
129                     "ERR_INVALID_SERVER_ROOT")); // NOI18N
130
return false;
131         }
132         
133         // set the server root in the parent instantiating iterator
134
instantiatingIterator.setServerRoot(locationField.getText());
135         
136         // everything seems ok
137
return true;
138     }
139     
140     ////////////////////////////////////////////////////////////////////////////
141
// JPanel section
142
////////////////////////////////////////////////////////////////////////////
143
private JButton locationBrowseButton;
144     private JLabel locationLabel;
145     private JTextField locationField;
146     private JPanel formattingPanel;
147     
148     /**
149      * Inits the GUI components
150      */

151     private void init() {
152         // we use the GridBagLayout so we need the GridBagConstraints to
153
// properly place the components
154
GridBagConstraints gridBagConstraints;
155         
156         // initialize the components
157
locationLabel = new JLabel();
158         locationField = new JTextField();
159         locationBrowseButton = new JButton();
160         formattingPanel = new JPanel();
161         
162         // set the desired layout
163
setLayout(new GridBagLayout());
164         
165         // add server installation directory field label
166
locationLabel.setText(NbBundle.getMessage(ServerLocationPanel.class,
167                 "LBL_SERVER_LOCATION")); // NOI18N
168
locationLabel.setDisplayedMnemonic(NbBundle.getMessage(ServerLocationPanel.class,
169                 "MNE_SERVER_LOCATION").charAt(0));
170         locationLabel.setLabelFor(locationField);
171         gridBagConstraints = new GridBagConstraints();
172         gridBagConstraints.gridx = 0;
173         gridBagConstraints.gridy = 0;
174         gridBagConstraints.anchor = GridBagConstraints.EAST;
175         add(locationLabel, gridBagConstraints);
176         
177         // add server installation directory field
178
locationField.addKeyListener(new LocationKeyListener());
179         if(System.getProperty("websphere.home")==null ||
180                 System.getProperty("websphere.home").equals("")) {
181             String JavaDoc home = System.getProperty("user.home");
182             if(home!=null) {
183                 try{
184                     File JavaDoc f = new File JavaDoc(home + File.separator + ".WASRegistry");
185                     BufferedReader JavaDoc reader = new BufferedReader JavaDoc(
186                             new InputStreamReader JavaDoc(
187                             new FileInputStream JavaDoc(f)));
188                     String JavaDoc string;
189                     while((string=reader.readLine())!=null) {
190                         if(string.length()>1 && new File JavaDoc(string).exists()) {
191                             System.setProperty("websphere.home",string);
192                         }
193                     }
194                 } catch (IOException JavaDoc e){
195                     e=null;
196                     //either the file does not exist or not available. Do nothing
197
}
198             }
199         }
200         
201         if (System.getProperty("websphere.home") != null) { // NOI18N
202
locationField.setText(System.getProperty(
203                     "websphere.home")); // NOI18N
204
}
205         gridBagConstraints = new GridBagConstraints();
206         gridBagConstraints.gridx = 1;
207         gridBagConstraints.gridy = 0;
208         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
209         gridBagConstraints.weightx = 1.0;
210         gridBagConstraints.insets = new Insets(0, 10, 0, 10);
211         locationField.getAccessibleContext().setAccessibleName(
212                 java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/websphere6/ui/wizard/Bundle").getString("TTL_Location"));
213         locationField.getAccessibleContext().setAccessibleDescription(
214                 java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/websphere6/ui/wizard/Bundle").getString("MSG_Location"));
215         
216         add(locationField, gridBagConstraints);
217         
218         // add server installation directory field browse button
219
locationBrowseButton.setText(NbBundle.getMessage(
220                 ServerLocationPanel.class, "LBL_BROWSE_BUTTON")); // NOI18N
221
locationBrowseButton.setMnemonic(KeyEvent.VK_O);
222         locationBrowseButton.setDisplayedMnemonicIndex(2);
223         locationBrowseButton.addActionListener(new BrowseActionListener());
224         gridBagConstraints = new GridBagConstraints();
225         gridBagConstraints.gridx = 2;
226         gridBagConstraints.gridy = 0;
227         gridBagConstraints.anchor = GridBagConstraints.WEST;
228         locationBrowseButton.getAccessibleContext().setAccessibleName(
229                 java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/websphere6/ui/wizard/Bundle").getString("TTL_Browse"));
230         locationBrowseButton.getAccessibleContext().setAccessibleDescription(
231                 java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/websphere6/ui/wizard/Bundle").getString("MSG_Browse"));
232         
233         add(locationBrowseButton, gridBagConstraints);
234         
235         // add the empty panel, that will take up all the remaining space
236
gridBagConstraints = new GridBagConstraints();
237         gridBagConstraints.gridx = 0;
238         gridBagConstraints.gridy = 1;
239         gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
240         gridBagConstraints.weighty = 1.0;
241         add(formattingPanel, gridBagConstraints);
242         getAccessibleContext().setAccessibleDescription(
243                 java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/websphere6/ui/wizard/Bundle").
244                 getString("MSG_ServerLocationPanelDescription"));
245     }
246     
247     /**
248      * An instance of the fileschooser that is used for locating the server
249      * installation directory
250      */

251     private JFileChooser fileChooser = new JFileChooser();
252     
253     /**
254      * Shows the filechooser set to currently selected directory or to the
255      * default system root if the directory is invalid
256      */

257     private void showFileChooser() {
258         // set the chooser's properties
259
fileChooser.setFileFilter(new DirectoryFileFilter());
260         fileChooser.setMultiSelectionEnabled(false);
261         fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
262         
263         // set the current directory
264
File JavaDoc currentLocation = new File JavaDoc(locationField.getText());
265         if (currentLocation.exists() && currentLocation.isDirectory()) {
266             fileChooser.setCurrentDirectory(currentLocation.getParentFile());
267             fileChooser.setSelectedFile(currentLocation);
268         }
269         
270         // wait for the user to choose the directory and if he clicked the OK
271
// button store the selected directory in the server location field
272
if (fileChooser.showOpenDialog(this) == fileChooser.APPROVE_OPTION) {
273             locationField.setText(fileChooser.getSelectedFile().getPath());
274             fireChangeEvent();
275         }
276     }
277     
278     /**
279      * Checks whether the supplied directory is the valid server installation
280      * directory.
281      *
282      * @return true if the supplied directory is valid, false otherwise
283      */

284     private static boolean isValidServerRoot(String JavaDoc path) {
285         // set the child directories/files that should be present and validate
286
// the directory as the server's installation one
287
String JavaDoc[] children = {
288             "bin", // NOI18N
289
"cloudscape", // NOI18N
290
"profiles", // NOI18N
291
"properties/wsadmin.properties", // NOI18N
292
"lib/j2ee.jar", // NOI18N
293
"lib/wjmxapp.jar" // NOI18N
294
};
295         return hasChildren(path, children);
296     }
297     
298     /**
299      * Checks whether the supplied directory has the required children
300      *
301      * @return true if the directory contains all the children, false otherwise
302      */

303     private static boolean hasChildren(String JavaDoc parent, String JavaDoc[] children) {
304         // if parent is null, it cannot contain any children
305
if (parent == null) {
306             return false;
307         }
308         
309         // if the children array is null, then the condition is fullfilled
310
if (children == null) {
311             return true;
312         }
313         
314         // for each child check whether it is contained and if it is not,
315
// return false
316
for (int i = 0; i < children.length; i++) {
317             if (!(new File JavaDoc(parent + File.separator + children[i]).exists())) {
318                 return false;
319             }
320         }
321         
322         // all is good
323
return true;
324     }
325     
326     ////////////////////////////////////////////////////////////////////////////
327
// Settings section
328
////////////////////////////////////////////////////////////////////////////
329
/**
330      * Reads the supplied setting. The only one that can arrive this way is the
331      * WizardDescriptor, thus we only convert the incoming object and save
332      *
333      * @param object the incoming setting (WizardDescriptor)
334      */

335     public void readSettings(Object JavaDoc object) {
336         this.wizardDescriptor = (WizardDescriptor) object;
337     }
338     
339     /**
340      * Stores the supplied setting. I don't know the purpose of this method
341      * thus we do not implement it
342      */

343     public void storeSettings(Object JavaDoc object) {
344     }
345     
346     ////////////////////////////////////////////////////////////////////////////
347
// Listeners section
348
////////////////////////////////////////////////////////////////////////////
349
/**
350      * The registrered listeners vector
351      */

352     private Vector listeners = new Vector();
353     
354     /**
355      * Removes a registered listener
356      *
357      * @param listener the listener to be removed
358      */

359     public void removeChangeListener(ChangeListener listener) {
360         if (listeners != null) {
361             synchronized (listeners) {
362                 listeners.remove(listener);
363             }
364         }
365     }
366     
367     /**
368      * Adds a listener
369      *
370      * @param listener the listener to be added
371      */

372     public void addChangeListener(ChangeListener listener) {
373         synchronized (listeners) {
374             listeners.add(listener);
375         }
376     }
377     
378     /**
379      * Fires a change event originating from this panel
380      */

381     private void fireChangeEvent() {
382         ChangeEvent event = new ChangeEvent(this);
383         fireChangeEvent(event);
384     }
385     
386     /**
387      * Fires a custom change event
388      *
389      * @param event the event
390      */

391     private void fireChangeEvent(ChangeEvent event) {
392         Vector targetListeners;
393         synchronized (listeners) {
394             targetListeners = (Vector) listeners.clone();
395         }
396         
397         for (int i = 0; i < targetListeners.size(); i++) {
398             ChangeListener listener = (ChangeListener) targetListeners.
399                     elementAt(i);
400             listener.stateChanged(event);
401         }
402     }
403     
404     ////////////////////////////////////////////////////////////////////////////
405
// Inner Classes
406
////////////////////////////////////////////////////////////////////////////
407
/**
408      * Simple key listener that delegates the event to its parent's listeners
409      *
410      * @author Kirill Sorokin
411      */

412     private class LocationKeyListener extends KeyAdapter {
413         /**
414          * This method is called when a user presses a key on the keyboard
415          */

416         public void keyTyped(KeyEvent event) {
417             fireChangeEvent();
418         }
419         
420         /**
421          * This method is called when a user releases a key on the keyboard
422          */

423         public void keyReleased(KeyEvent event) {
424             fireChangeEvent();
425         }
426     }
427     
428     /**
429      * Simple listener that reacts on the user's clicking the Browse button
430      *
431      * @author Kirill Sorokin
432      */

433     private class BrowseActionListener implements ActionListener {
434         /**
435          * this methos is called when a user clicks Browse and show the file
436          * chooser dialog in response
437          */

438         public void actionPerformed(ActionEvent event) {
439             showFileChooser();
440         }
441     }
442     
443     /**
444      * An extension of the FileFilter class that is setup to accept only
445      * directories.
446      *
447      * @author Kirill Sorokin
448      */

449     private static class DirectoryFileFilter extends FileFilter {
450         /**
451          * This method is called when it is needed to decide whether a chosen
452          * file meets the filter's requirements
453          *
454          * @return true if the file meets the requirements, false otherwise
455          */

456         public boolean accept(File JavaDoc file) {
457             // if the file exists and it's a directory - accept it
458
if (file.exists() && file.isDirectory()) {
459                 return true;
460             }
461             
462             // in all other cases - refuse
463
return false;
464         }
465         
466         /**
467          * Returns the description of file group described by this filter
468          *
469          * @return group name
470          */

471         public String JavaDoc getDescription() {
472             return NbBundle.getMessage(ServerLocationPanel.class,
473                     "DIRECTORIES_FILTER_NAME"); // NOI18N
474
}
475     }
476 }
477
Popular Tags