KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
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.xml.parsers.*;
28 import org.w3c.dom.*;
29 import org.xml.sax.*;
30
31 import org.openide.*;
32 import org.openide.util.*;
33
34 /**
35  * The second panel of the custom wizard used for registering an instance of
36  * the server. Here user should choose among the the existing local instances,
37  * or enter the host/port/username/password conbination for a remote one
38  *
39  * @author Kirill Sorokin
40  */

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

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

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

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

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

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

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

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

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

112     public boolean isValid() {
113         // clear the error message
114
wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, ""); // NOI18N
115

116         // if the server instance is local, then check the profile root
117
// directory for validity
118
if (serverTypeCombo.getSelectedItem().equals(NbBundle.getMessage(ServerPropertiesPanel.class, "SERVER_TYPE_LOCAL"))) { // NOI18N
119
if (!isValidDomainRoot(domainPathField.getText())) {
120                 wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, NbBundle.getMessage(ServerPropertiesPanel.class, "ERR_INVALID_DOMAIN_ROOT")); // NOI18N
121
return false;
122             }
123         }
124         
125         // check the host field (not empty)
126
if (hostField.getText().trim().equals("")) {
127             wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, NbBundle.getMessage(ServerPropertiesPanel.class, "ERR_INVALID_HOST")); // NOI18N
128
}
129         
130         // check the port field (not empty and a positive integer)
131
if (!portField.getText().trim().matches("[0-9]+")) {
132             wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, NbBundle.getMessage(ServerPropertiesPanel.class, "ERR_INVALID_PORT")); // NOI18N
133
}
134         
135         // no checks for username & password as they may be intentionally blank
136

137         // save the data to the parent instantiating iterator
138
instantiatingIterator.setDomainRoot(domainPathField.getText());
139         instantiatingIterator.setHost(hostField.getText());
140         instantiatingIterator.setPort(portField.getText());
141         instantiatingIterator.setUsername(usernameField.getText());
142         instantiatingIterator.setPassword(new String JavaDoc(passwordField.getPassword()));
143         instantiatingIterator.setIsLocal(serverTypeCombo.getSelectedItem().equals(NbBundle.getMessage(ServerPropertiesPanel.class, "SERVER_TYPE_LOCAL")) ? "true" : "false"); // NOI18N
144

145         // everything seems ok
146
return true;
147     }
148     
149     /**
150      * Checks whether the specified path is the valid domain root directory.
151      *
152      * @return true if the path is the valid domain root, false otherwise
153      */

154     private boolean isValidDomainRoot(String JavaDoc path) {
155         // set the child directories/files that should be present and validate
156
// the directory as the domain root
157

158         // the layout is different for 90b and 90, temporarilly leaving both
159
// versions in for testing TODO: remove
160
String JavaDoc[] children = {
161                     "servers", // NOI18N
162
"config", // NOI18N
163
"config/config.xml", // NOI18N
164
"init-info/domain-info.xml", // NOI18N
165
};
166         boolean is90 = hasChildren(path, children);
167         String JavaDoc[] children90b = {
168                     "servers", // NOI18N
169
"config", // NOI18N
170
"config/config.xml", // NOI18N
171
"domain-info.xml", // NOI18N
172
};
173         boolean is90b = hasChildren(path, children90b);
174         return is90 || is90b;
175     }
176     
177     /**
178      * Checks whether the supplied directory has the required children
179      *
180      * @return true if the directory contains all the children, false otherwise
181      */

182     private boolean hasChildren(String JavaDoc parent, String JavaDoc[] children) {
183         // if parent is null, it cannot contain any children
184
if (parent == null) {
185             return false;
186         }
187         
188         // if the children array is null, then the condition is fullfilled
189
if (children == null) {
190             return true;
191         }
192         
193         // for each child check whether it is contained and if it is not,
194
// return false
195
for (int i = 0; i < children.length; i++) {
196             if (!(new File(parent + File.separator + children[i]).exists())) {
197                 return false;
198             }
199         }
200         
201         // all is good
202
return true;
203     }
204     
205     ////////////////////////////////////////////////////////////////////////////
206
// JPanel section
207
////////////////////////////////////////////////////////////////////////////
208
private JLabel domainPathLabel;
209     private JLabel hostLabel;
210     private JLabel portLabel;
211     private JLabel userNameLabel;
212     private JLabel passwordLabel;
213     private JPasswordField passwordField;
214     private JTextField domainPathField;
215     private JTextField hostField;
216     private JTextField portField;
217     private JTextField usernameField;
218     private JPanel formattingPanel;
219     private JComboBox serverTypeCombo;
220     private JComboBox localInstancesCombo;
221     private JLabel localInstanceLabel;
222     private JLabel serverTypeLabel;
223     
224     /**
225      * Inits the GUI components
226      */

227     private void init() {
228         // we use the GridBagLayout so we need the GridBagConstraints to
229
// properly place the components
230
GridBagConstraints gridBagConstraints;
231         
232         // initialize the components
233
domainPathLabel = new JLabel();
234         domainPathField = new JTextField();
235         hostLabel = new JLabel();
236         hostField = new JTextField();
237         portLabel = new JLabel();
238         portField = new JTextField();
239         userNameLabel = new JLabel();
240         usernameField = new JTextField();
241         passwordLabel = new JLabel();
242         passwordField = new JPasswordField();
243         formattingPanel = new JPanel();
244         serverTypeCombo = new JComboBox(new Object JavaDoc[] {NbBundle.getMessage(ServerPropertiesPanel.class, "SERVER_TYPE_LOCAL")/*, NbBundle.getMessage(ServerPropertiesPanel.class, "SERVER_TYPE_REMOTE")*/}); // NOI18N
245
localInstanceLabel = new JLabel();
246         localInstancesCombo = new JComboBox(new InstancesModel(getServerInstances()));
247         serverTypeLabel = new JLabel();
248         
249         // set the desired layout
250
setLayout(new GridBagLayout());
251         
252         //temporarily commented for issue #64480 (remote instances not supported in 5.0)
253

254         // add server type field label
255
// serverTypeLabel.setText(NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_serverType")); // NOI18N
256
// gridBagConstraints = new GridBagConstraints();
257
// gridBagConstraints.gridx = 0;
258
// gridBagConstraints.gridy = 0;
259
// gridBagConstraints.anchor = GridBagConstraints.EAST;
260
// gridBagConstraints.insets = new Insets(0, 0, 5, 0);
261
// add(serverTypeLabel, gridBagConstraints);
262
//
263
// // add server type combobox
264
// serverTypeCombo.addActionListener(new ServerTypeActionListener());
265
// gridBagConstraints = new GridBagConstraints();
266
// gridBagConstraints.gridx = 1;
267
// gridBagConstraints.gridy = 0;
268
// gridBagConstraints.anchor = GridBagConstraints.WEST;
269
// gridBagConstraints.insets = new Insets(0, 10, 5, 0);
270
// add(serverTypeCombo, gridBagConstraints);
271

272         // add local instances field label
273
localInstanceLabel.setLabelFor(localInstancesCombo);
274         localInstanceLabel.setText(NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_LOCAL_INSTANCE")); // NOI18N
275
gridBagConstraints = new GridBagConstraints();
276         gridBagConstraints.gridx = 0;
277         gridBagConstraints.gridy = 1;
278         gridBagConstraints.anchor = GridBagConstraints.EAST;
279         gridBagConstraints.insets = new Insets(0, 0, 5, 0);
280         add(localInstanceLabel, gridBagConstraints);
281         
282         // add local instances combobox
283
localInstancesCombo.addActionListener(new InstanceSelectionListener());
284         gridBagConstraints = new GridBagConstraints();
285         gridBagConstraints.gridx = 1;
286         gridBagConstraints.gridy = 1;
287         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
288         gridBagConstraints.insets = new Insets(0, 10, 5, 0);
289         add(localInstancesCombo, gridBagConstraints);
290         localInstancesCombo.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerPropertiesPanel.class, "ACSD_ServerPropertiesPanel_localInstancesCombo")); // NOI18N
291

292         // add domain path field label
293
domainPathLabel.setLabelFor(domainPathField);
294         domainPathLabel.setText(NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_DOMAIN_LOCATION")); // NOI18N
295
gridBagConstraints = new GridBagConstraints();
296         gridBagConstraints.gridx = 0;
297         gridBagConstraints.gridy = 2;
298         gridBagConstraints.anchor = GridBagConstraints.EAST;
299         gridBagConstraints.insets = new Insets(0, 0, 5, 0);
300         add(domainPathLabel, gridBagConstraints);
301         
302         // add domain path field
303
domainPathField.setColumns(20);
304         domainPathField.setText(""); // NOI18N
305
domainPathField.setEditable(false);
306         gridBagConstraints = new GridBagConstraints();
307         gridBagConstraints.gridx = 1;
308         gridBagConstraints.gridy = 2;
309         gridBagConstraints.weightx = 1.0;
310         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
311         gridBagConstraints.insets = new Insets(0, 10, 5, 0);
312         add(domainPathField, gridBagConstraints);
313         domainPathField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerPropertiesPanel.class, "ACSD_ServerPropertiesPanel_domainPathField")); // NOI18N
314

315         // add host field label
316
hostLabel.setLabelFor(hostField);
317         hostLabel.setText(NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_HOST")); // NOI18N
318
gridBagConstraints = new GridBagConstraints();
319         gridBagConstraints.gridx = 0;
320         gridBagConstraints.gridy = 3;
321         gridBagConstraints.anchor = GridBagConstraints.EAST;
322         gridBagConstraints.insets = new Insets(0, 0, 5, 0);
323         add(hostLabel, gridBagConstraints);
324         
325         // add host field
326
hostField.setColumns(20);
327         hostField.setText(""); // NOI18N
328
hostField.addKeyListener(new KeyListener());
329         gridBagConstraints = new GridBagConstraints();
330         gridBagConstraints.gridx = 1;
331         gridBagConstraints.gridy = 3;
332         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
333         gridBagConstraints.insets = new Insets(0, 10, 5, 0);
334         add(hostField, gridBagConstraints);
335         hostField.setEditable(false);
336         hostField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerPropertiesPanel.class, "ACSD_ServerPropertiesPanel_hostField")); // NOI18N
337

338         // add port field label
339
portLabel.setLabelFor(portField);
340         portLabel.setText(NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_PORT")); // NOI18N
341
gridBagConstraints = new GridBagConstraints();
342         gridBagConstraints.gridx = 0;
343         gridBagConstraints.gridy = 4;
344         gridBagConstraints.anchor = GridBagConstraints.EAST;
345         gridBagConstraints.insets = new Insets(0, 0, 5, 0);
346         add(portLabel, gridBagConstraints);
347         
348         // add port field
349
portField.setColumns(20);
350         portField.setText(""); // NOI18N
351
portField.addKeyListener(new KeyListener());
352         gridBagConstraints = new GridBagConstraints();
353         gridBagConstraints.gridx = 1;
354         gridBagConstraints.gridy = 4;
355         gridBagConstraints.anchor = GridBagConstraints.WEST;
356         gridBagConstraints.insets = new Insets(0, 10, 5, 0);
357         add(portField, gridBagConstraints);
358         portField.setEditable(false);
359         portField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerPropertiesPanel.class, "ACSD_ServerPropertiesPanel_portField")); // NOI18N
360

361         // add username field label
362
userNameLabel.setLabelFor(usernameField);
363 // userNameLabel.setText(NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_USERNAME")); // NOI18N
364
org.openide.awt.Mnemonics.setLocalizedText(userNameLabel, NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_USERNAME"));
365         gridBagConstraints = new GridBagConstraints();
366         gridBagConstraints.gridx = 0;
367         gridBagConstraints.gridy = 5;
368         gridBagConstraints.anchor = GridBagConstraints.EAST;
369         gridBagConstraints.insets = new Insets(0, 0, 5, 0);
370         add(userNameLabel, gridBagConstraints);
371         
372         // add username field
373
usernameField.setColumns(20);
374         usernameField.setText(""); // NOI18N
375
usernameField.addKeyListener(new KeyListener());
376         gridBagConstraints = new GridBagConstraints();
377         gridBagConstraints.gridx = 1;
378         gridBagConstraints.gridy = 5;
379         gridBagConstraints.anchor = GridBagConstraints.WEST;
380         gridBagConstraints.insets = new Insets(0, 10, 5, 0);
381         add(usernameField, gridBagConstraints);
382         usernameField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerPropertiesPanel.class, "ACSD_ServerPropertiesPanel_usernameField")); // NOI18N
383

384         // add password field label
385
passwordLabel.setLabelFor(passwordField);
386 // passwordLabel.setText(NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_PASSWORD")); // NOI18N
387
org.openide.awt.Mnemonics.setLocalizedText(passwordLabel, NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_PASSWORD"));
388         gridBagConstraints = new GridBagConstraints();
389         gridBagConstraints.gridx = 0;
390         gridBagConstraints.gridy = 6;
391         gridBagConstraints.anchor = GridBagConstraints.EAST;
392         gridBagConstraints.insets = new Insets(0, 0, 5, 0);
393         add(passwordLabel, gridBagConstraints);
394         
395         // add password field
396
passwordField.setColumns(20);
397         passwordField.addKeyListener(new KeyListener());
398         gridBagConstraints = new GridBagConstraints();
399         gridBagConstraints.gridx = 1;
400         gridBagConstraints.gridy = 6;
401         gridBagConstraints.anchor = GridBagConstraints.WEST;
402         gridBagConstraints.insets = new Insets(0, 10, 5, 0);
403         add(passwordField, gridBagConstraints);
404         passwordField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerPropertiesPanel.class, "ACSD_ServerPropertiesPanel_passwordField")); // NOI18N
405

406         // add the empty panel, that will take up all the remaining space
407
gridBagConstraints = new GridBagConstraints();
408         gridBagConstraints.gridx = 0;
409         gridBagConstraints.gridy = 7;
410         gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
411         gridBagConstraints.weighty = 1.0;
412         add(formattingPanel, gridBagConstraints);
413     }
414     
415     /**
416      * Gets the list of registered domains according to the given server
417      * installation root
418      *
419      * @param serverRoot the server's installation location
420      *
421      * @return an array if strings with the domains' paths
422      */

423     private String JavaDoc[] getRegisteredDomains(String JavaDoc serverRoot){
424         // init the resulting vector
425
Vector result = new Vector();
426         
427         // is the server root was not defined, return an empty array of domains
428
if (serverRoot == null) {
429             return new String JavaDoc[0];
430         }
431         
432         // the relative path to the domains list file
433
String JavaDoc domainListFile = "/common/nodemanager/nodemanager.domains"; // NOI18N
434

435         // init the input stream for the file and the w3c document object
436
File file = new File(serverRoot + domainListFile);
437         LineNumberReader lnr = null;
438         
439         // read the list file line by line fetching out the domain paths
440
try {
441             // create a new reader for the FileInputStream
442
lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(file)));
443             
444             // read the lines
445
String JavaDoc line;
446             while ((line = lnr.readLine()) != null){
447                 // skip the comments
448
if (line.startsWith("#")) {
449                     continue;
450                 }
451                 
452                 // fetch the domain path
453
String JavaDoc path = line.split("=")[1].replaceAll("\\\\\\\\", "/").replaceAll("\\\\:", ":"); // NOI18N
454

455                 // add the path to the resulting set
456
result.add(path);
457             }
458         } catch (FileNotFoundException e) {
459             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
460         } catch (IOException e) {
461             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
462         } finally {
463             try {
464                 // close the stream
465
if (lnr != null) {
466                     lnr.close();
467                 }
468             } catch (IOException e) {
469                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
470             }
471         }
472         
473         // convert the vector to an array and return
474
return (String JavaDoc[]) result.toArray(new String JavaDoc[result.size()]);
475     }
476     
477     /**
478      * Gets the list of local server instances.
479      *
480      * @return a vector with the local instances
481      */

482     private Vector getServerInstances(){
483         // initialize the resulting vector
484
Vector result = new Vector();
485         
486         // get the list of registered profiles
487
String JavaDoc[] domains = getRegisteredDomains(instantiatingIterator.getServerRoot());
488         
489         // for each domain get the list of instances
490
for (int i = 0; i < domains.length; i++) {
491             // get the instances configuration file's path
492
String JavaDoc configPath = domains[i] + "/config/config.xml"; // NOI18N
493

494             // init the input stream for the file and the w3c document object
495
InputStream inputStream = null;
496             Document document = null;
497
498             try {
499                 // open the stream from the instances config file
500
inputStream = new FileInputStream(new File(configPath));
501                 
502                 // parse the document
503
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream);
504                 
505                 // get the root element
506
Element root = document.getDocumentElement();
507                 
508                 // get the child nodes
509
NodeList children = root.getChildNodes();
510                 
511                 // for each child
512
for (int j = 0; j < children.getLength(); j++) {
513                     Node child = children.item(j);
514                     // if the child's name equals 'server' get its children
515
// and iterate over them
516
if (child.getNodeName().matches("(?:[a-z]+\\:)?server")) { // NOI18N
517
NodeList nl = child.getChildNodes();
518                         
519                         // desclare the server's name/host/port
520
String JavaDoc name = "";
521                         String JavaDoc port = "";
522                         String JavaDoc host = "";
523                         
524                         // iterate over the children
525
for (int k = 0; k < nl.getLength(); k++){
526                             Node ch = nl.item(k);
527                             
528                             // if the child's name equals 'name' fetch the
529
// instance's name
530
if (ch.getNodeName().matches("(?:[a-z]+\\:)?name")) { // NOI18N
531
name = ch.getFirstChild().getNodeValue();
532                             }
533
534                             // if the child's name equals 'listen-port' fetch the
535
// instance's port
536
if (ch.getNodeName().matches("(?:[a-z]+\\:)?listen-port")) { // NOI18N
537
port = ch.getFirstChild().getNodeValue();
538                             }
539
540                             // if the child's name equals 'listen-address' fetch the
541
// instance's host
542
if (ch.getNodeName().matches("(?:[a-z]+\\:)?listen-address")) { // NOI18N
543
if (ch.hasChildNodes()){
544                                     host = ch.getFirstChild().getNodeValue();
545                                 }
546                             }
547                         }
548                         
549                         // if all the parameters were fetched successfully add
550
// them to the result
551
if ((name != null) && (!name.equals(""))) { // NOI18N
552
//address and port have minOccurs=0 and are missing in 90 examples server
553
port = port == null || port.equals("") ? "7001" : port; //NOI18N
554
host = host == null || host.equals("") ? "localhost" : host; // NOI18N
555
result.add(new Instance(name, host, port, domains[i]));
556                         }
557                     }
558                 }
559             } catch(FileNotFoundException e) {
560                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
561             } catch (IOException e) {
562                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
563             } catch (ParserConfigurationException e) {
564                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
565             } catch (SAXException e) {
566                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
567             } finally {
568                 try {
569                     if (inputStream != null) {
570                         inputStream.close();
571                     }
572                 } catch (IOException e) {
573                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
574                 }
575             }
576         }
577         
578         // convert the vector to an array and return
579
return result;
580     }
581     
582     /**
583      * Updates the local instances combobox model with the fresh local
584      * instances list
585      */

586     public void updateInstancesList() {
587         localInstancesCombo.setModel(new InstancesModel(getServerInstances()));
588         updateInstanceInfo();
589     }
590     
591     /**
592      * Updates the selected local instance information, i.e. profile path,
593      * host, port.
594      */

595     private void updateInstanceInfo() {
596         // get the selected local instance
597
Instance instance = (Instance) localInstancesCombo.getSelectedItem();
598         
599         if (instance != null) {
600             // set the fields' values
601
domainPathField.setText(instance.getDomainPath());
602             hostField.setText(instance.getHost());
603             portField.setText(instance.getPort());
604         }
605         
606     }
607     ////////////////////////////////////////////////////////////////////////////
608
// Settings section
609
////////////////////////////////////////////////////////////////////////////
610
/**
611      * Reads the supplied setting. The only one that can arrive this way is the
612      * WizardDescriptor, thus we only convert the incoming object and save
613      *
614      * @param object the incoming setting (WizardDescriptor)
615      */

616     public void readSettings(Object JavaDoc object) {
617         this.wizardDescriptor = (WizardDescriptor) object;
618     }
619     
620     /**
621      * Stores the supplied setting. I don't know the purpose of this method
622      * thus we do not implement it
623      */

624     public void storeSettings(Object JavaDoc object) {}
625     
626     ////////////////////////////////////////////////////////////////////////////
627
// Listeners section
628
////////////////////////////////////////////////////////////////////////////
629
/**
630      * The registrered listeners vector
631      */

632     private Vector listeners = new Vector();
633     
634     /**
635      * Removes a registered listener
636      *
637      * @param listener the listener to be removed
638      */

639     public void removeChangeListener(ChangeListener listener) {
640         if (listeners != null) {
641             synchronized (listeners) {
642                 listeners.remove(listener);
643             }
644         }
645     }
646     
647     /**
648      * Adds a listener
649      *
650      * @param listener the listener to be added
651      */

652     public void addChangeListener(ChangeListener listener) {
653         synchronized (listeners) {
654             listeners.add(listener);
655         }
656     }
657     
658     /**
659      * Fires a change event originating from this panel
660      */

661     private void fireChangeEvent() {
662         ChangeEvent event = new ChangeEvent(this);
663         fireChangeEvent(event);
664     }
665     
666     /**
667      * Fires a custom change event
668      *
669      * @param event the event
670      */

671     private void fireChangeEvent(ChangeEvent event) {
672         Vector targetListeners;
673         synchronized (listeners) {
674             targetListeners = (Vector) listeners.clone();
675         }
676         
677         for (int i = 0; i < targetListeners.size(); i++) {
678             ChangeListener listener = (ChangeListener) targetListeners.elementAt(i);
679             listener.stateChanged(event);
680         }
681     }
682     
683     ////////////////////////////////////////////////////////////////////////////
684
// Inner classes
685
////////////////////////////////////////////////////////////////////////////
686
/**
687      * Simple key listener that delegates the event to its parent's listeners
688      *
689      * @author Kirill Sorokin
690      */

691     private class KeyListener extends KeyAdapter {
692         /**
693          * This method is called when a user presses a key on the keyboard
694          */

695         public void keyTyped(KeyEvent event) {
696             fireChangeEvent();
697         }
698         
699         /**
700          * This method is called when a user releases a key on the keyboard
701          */

702         public void keyReleased(KeyEvent event) {
703             fireChangeEvent();
704         }
705     }
706     
707     /**
708      * A listener that reacts to the change of the server type combobox,
709      * is the local server type is selected we should disable several fields
710      * and enable some others instead.
711      *
712      * @author Kirill Sorokin
713      */

714     private class ServerTypeActionListener implements ActionListener {
715         /**
716          * The main action handler. This method is called when the combobox
717          * value changes
718          */

719         public void actionPerformed(ActionEvent e) {
720             // if the selected type is local
721
if (serverTypeCombo.getSelectedItem().equals(NbBundle.getMessage(ServerPropertiesPanel.class, "SERVER_TYPE_LOCAL"))) { // NOI18N
722
Instance instance = (Instance) localInstancesCombo.getSelectedItem();
723                 
724                 // enable the local instances combo
725
localInstancesCombo.setEnabled(true);
726                 
727                 // enable and set as read-only the domain path field
728
domainPathField.setEnabled(true);
729                 domainPathField.setEditable(false);
730                 
731                 // enable and set as read-only the host field
732
hostField.setEnabled(true);
733                 hostField.setEditable(false);
734                 hostField.setText(instance.getHost());
735                 
736                 // enable and set as read-only the port field
737
portField.setEnabled(true);
738                 portField.setEditable(false);
739                 portField.setText(instance.getPort());
740             } else {
741                 // disable the local instances combo
742
localInstancesCombo.setEnabled(false);
743                 
744                 // disable the domain path field
745
domainPathField.setEnabled(false);
746                 domainPathField.setEditable(false);
747                 
748                 // enable and set as read-write the host field
749
hostField.setEnabled(true);
750                 hostField.setEditable(true);
751                 
752                 // enable and set as read-write the port field
753
portField.setEnabled(true);
754                 portField.setEditable(true);
755             }
756             
757             isValid();
758         }
759     }
760     
761     /**
762      * A simple listeners that reacts to user's selectin a local instance. It
763      * updates the selected instance info.
764      *
765      * @author Kirill Sorokin
766      */

767     private class InstanceSelectionListener implements ActionListener {
768         /**
769          * The main action handler. This method is called when a new local
770          * instance is selected
771          */

772         public void actionPerformed(ActionEvent e) {
773             updateInstanceInfo();
774         }
775     }
776     
777     /**
778      * A combobox model that represents the list of local instances. It
779      * contains a vector of objects of Instance class that contain all data
780      * for the instance
781      *
782      * @author Kirill Sorokin
783      */

784     private static class InstancesModel extends AbstractListModel implements ComboBoxModel {
785         /**
786          * A vector with the instances
787          */

788         private Vector instances;
789         
790         /**
791          * The index of the selected instance
792          */

793         private int selectedIndex = 0;
794         
795         /**
796          * Creates a new instance of InstancesModel
797          *
798          * @param instances a vector with the locally found instances
799          */

800         public InstancesModel(Vector instances) {
801             // save the instances
802
this.instances = instances;
803             
804             // set the selected index to zero
805
this.selectedIndex = 0;
806         }
807         
808         /**
809          * Sets the selected index to the index of the supplied item
810          *
811          * @param item the instance which should be selected
812          */

813         public void setSelectedItem(Object JavaDoc item) {
814             // set the index to the given item's index or to -1
815
// if the item does not exists
816
selectedIndex = instances.indexOf(item);
817         }
818
819         /**
820          * Get the instance with the specified instance
821          *
822          * @param index the index of the desired instance
823          *
824          * @return the instance at the given index
825          */

826         public Object JavaDoc getElementAt(int index) {
827             return instances.elementAt(index);
828         }
829
830         /**
831          * Returns the total number of instances
832          *
833          * @return the number of instances
834          */

835         public int getSize() {
836             return instances.size();
837         }
838
839         /**
840          * Returns the instance at the selected index
841          *
842          * @return the instance at the selected index
843          */

844         public Object JavaDoc getSelectedItem() {
845             // if there are no instances return null
846
if (instances.size() == 0) {
847                 return null;
848             }
849             
850             // return the element at the index
851
return instances.elementAt(selectedIndex);
852         }
853         
854     }
855     
856     /**
857      * A model for the server instance. It contains all the critical properties
858      * for the plugin: name, host, port, profile path.
859      *
860      * @author Kirill Sorokin
861      */

862     private static class Instance {
863         /**
864          * Instance's name, it is used a the parameter to the startup/shutdown
865          * scripts
866          */

867         private String JavaDoc name;
868         
869         /**
870          * Instance's host
871          */

872         private String JavaDoc host;
873         
874         /**
875          * Instance's port
876          */

877         private String JavaDoc port;
878         
879         /**
880          * Instance's profile directory
881          */

882         private String JavaDoc domainPath;
883         
884         /**
885          * Creates a new instance of Instance
886          *
887          * @param name the instance's name
888          * @param host the instance's host
889          * @param port the instance's port
890          * @param domainPath the instance's profile path
891          */

892         public Instance(String JavaDoc name, String JavaDoc host, String JavaDoc port, String JavaDoc domainPath) {
893             // save the properties
894
this.name = name;
895             this.host = host;
896             this.port = port;
897             this.domainPath = domainPath;
898         }
899         
900         /**
901          * Getter for the instance's name
902          *
903          * @return the instance's name
904          */

905         public String JavaDoc getName() {
906             return this.name;
907         }
908         
909         /**
910          * Setter for the instance's name
911          *
912          * @param the new instance's name
913          */

914         public void setName(String JavaDoc name) {
915             this.name = name;
916         }
917         
918         /**
919          * Getter for the instance's host
920          *
921          * @return the instance's host
922          */

923         public String JavaDoc getHost() {
924             return this.host;
925         }
926         
927         /**
928          * Setter for the instance's host
929          *
930          * @param the new instance's host
931          */

932         public void setHost(String JavaDoc host) {
933             this.host = host;
934         }
935         
936         /**
937          * Getter for the instance's port
938          *
939          * @return the instance's port
940          */

941         public String JavaDoc getPort() {
942             return this.port;
943         }
944         
945         /**
946          * Setter for the instance's port
947          *
948          * @param the new instance's port
949          */

950         public void setPort(String JavaDoc port) {
951             this.port = port;
952         }
953         
954         /**
955          * Getter for the instance's profile path
956          *
957          * @return the instance's profile path
958          */

959         public String JavaDoc getDomainPath() {
960             return this.domainPath;
961         }
962         
963         /**
964          * Setter for the instance's profile path
965          *
966          * @param the new instance's profile path
967          */

968         public void setDomainPath(String JavaDoc domainPath) {
969             this.domainPath = domainPath;
970         }
971         
972         /**
973          * An overriden version of the Object's toString() so that the
974          * instance is displayed properly in the combobox
975          */

976         public String JavaDoc toString() {
977             return name + " [" + host + ":" + port + "]"; // NOI18N
978
}
979     }
980 }
981
Popular Tags