KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > impl > ui > wizard > ServerChooserVisual


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
20 package org.netbeans.modules.j2ee.deployment.impl.ui.wizard;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.swing.ComboBoxModel JavaDoc;
29 import javax.swing.event.ChangeEvent JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import javax.swing.event.DocumentEvent JavaDoc;
32 import javax.swing.event.DocumentListener JavaDoc;
33 import org.netbeans.modules.j2ee.deployment.impl.Server;
34 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;
35 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry;
36 import org.netbeans.modules.j2ee.deployment.plugins.api.OptionalDeploymentManagerFactory;
37 import org.openide.util.NbBundle;
38
39 /**
40  *
41  * @author Andrei Badea
42  */

43 public class ServerChooserVisual extends javax.swing.JPanel JavaDoc {
44     private final List JavaDoc listeners = new ArrayList JavaDoc();
45     private AddServerInstanceWizard wizard;
46     private HashMap JavaDoc displayNames;
47     
48     public ServerChooserVisual() {
49         displayNames = new HashMap JavaDoc();
50         initComponents();
51         
52         ServerAdapter selected = (ServerAdapter)serverComboBox.getSelectedItem();
53         if (selected != null)
54             fillDisplayName(selected.getServer());
55         
56         displayNameEditField.getDocument().addDocumentListener(new DocumentListener JavaDoc() {
57             public void insertUpdate(DocumentEvent JavaDoc e) {
58                 displayNameEditFieldUpdate();
59             }
60             
61             public void removeUpdate(DocumentEvent JavaDoc e) {
62                 displayNameEditFieldUpdate();
63             }
64             
65             public void changedUpdate(DocumentEvent JavaDoc e) {
66                 displayNameEditFieldUpdate();
67             }
68         });
69     }
70     
71     public void addChangeListener(ChangeListener JavaDoc l) {
72         synchronized (listeners) {
73             listeners.add(l);
74         }
75     }
76     
77     public void removeChangeListener(ChangeListener JavaDoc l) {
78         synchronized (listeners) {
79             listeners.remove(l);
80         }
81     }
82     
83     public void read(AddServerInstanceWizard wizard) {
84         if (this.wizard == null)
85             this.wizard = wizard;
86         
87         Object JavaDoc prop = wizard.getProperty(AddServerInstanceWizard.PROP_DISPLAY_NAME);
88         if (prop != null)
89             displayNameEditField.setText((String JavaDoc)prop);
90     }
91     
92     public void store(AddServerInstanceWizard wizard) {
93         wizard.putProperty(AddServerInstanceWizard.PROP_DISPLAY_NAME, displayNameEditField.getText());
94         Object JavaDoc selectedItem = serverComboBox.getSelectedItem();
95         if (selectedItem != null) {
96             wizard.putProperty(AddServerInstanceWizard.PROP_SERVER, ((ServerAdapter)selectedItem).getServer());
97         }
98     }
99     
100     public boolean isValid() {
101         boolean result = isServerValid() && isDisplayNameValid();
102         if (result)
103             wizard.setErrorMessage(null);
104         
105         return result;
106     }
107     
108     private boolean isServerValid() {
109         boolean result = serverComboBox.getSelectedItem() != null;
110         if (!result)
111             wizard.setErrorMessage(NbBundle.getMessage(ServerChooserVisual.class, "MSG_SCV_ChooseServer"));
112         return result;
113     }
114     
115     private boolean isDisplayNameValid() {
116         String JavaDoc trimmed = displayNameEditField.getText().trim();
117         boolean result;
118         
119         if (trimmed.length() <= 0) {
120             wizard.setErrorMessage(NbBundle.getMessage(ServerChooserVisual.class, "MSG_SCV_DisplayName"));
121             return false;
122         }
123         
124         if (getServerInstance(trimmed) != null) {
125             wizard.setErrorMessage(NbBundle.getMessage(ServerChooserVisual.class, "MSG_SCV_DisplayNameExists"));
126             return false;
127         }
128         
129         return true;
130     }
131     
132     private ServerInstance getServerInstance(String JavaDoc displayName) {
133         Iterator JavaDoc iter = ServerRegistry.getInstance().getInstances().iterator();
134         while (iter.hasNext()) {
135             ServerInstance instance = (ServerInstance)iter.next();
136             if (instance.getDisplayName().compareToIgnoreCase(displayName) == 0)
137                 return instance;
138         }
139         return null;
140     }
141     
142     private void displayNameEditFieldUpdate() {
143         fireChange();
144     }
145     
146     private void fireChange() {
147         ChangeEvent JavaDoc event = new ChangeEvent JavaDoc(this);
148         ArrayList JavaDoc tempList;
149
150         synchronized (listeners) {
151             tempList = new ArrayList JavaDoc(listeners);
152         }
153
154         Iterator JavaDoc iter = tempList.iterator();
155         while (iter.hasNext())
156             ((ChangeListener JavaDoc)iter.next()).stateChanged(event);
157     }
158     
159     private String JavaDoc generateDisplayName(Server server) {
160         String JavaDoc name;
161         int count = 0;
162         
163         do {
164             name = server.getDisplayName();
165             if (count != 0)
166                 name += " (" + String.valueOf(count) + ")";
167             
168             count++;
169         } while (getServerInstance(name) != null);
170         
171         return name;
172     }
173     
174     private void fillDisplayName(Server server) {
175         String JavaDoc name = (String JavaDoc)displayNames.get(server);
176         if (name == null)
177             name = generateDisplayName(server);
178         displayNameEditField.setText(name);
179     }
180     
181     /** This method is called from within the constructor to
182      * initialize the form.
183      * WARNING: Do NOT modify this code. The content of this method is
184      * always regenerated by the Form Editor.
185      */

186     private void initComponents() {//GEN-BEGIN:initComponents
187
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
188
189         jLabel1 = new javax.swing.JLabel JavaDoc();
190         serverComboBox = new javax.swing.JComboBox JavaDoc();
191         jLabel2 = new javax.swing.JLabel JavaDoc();
192         displayNameEditField = new javax.swing.JTextField JavaDoc();
193         jPanel1 = new javax.swing.JPanel JavaDoc();
194
195         setLayout(new java.awt.GridBagLayout JavaDoc());
196
197         setName(org.openide.util.NbBundle.getBundle(ServerChooserVisual.class).getString("LBL_SCV_Name"));
198         getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ServerChooserVisual.class, "A11Y_SCV_NAME"));
199         getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ServerChooserVisual.class, "A11Y_SCV_DESC"));
200         jLabel1.setDisplayedMnemonic(org.openide.util.NbBundle.getBundle(ServerChooserVisual.class).getString("LBL_SCV_Server_mnem").charAt(0));
201         jLabel1.setLabelFor(serverComboBox);
202         jLabel1.setText(org.openide.util.NbBundle.getBundle(ServerChooserVisual.class).getString("LBL_SCV_Server"));
203         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
204         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
205         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
206         add(jLabel1, gridBagConstraints);
207
208         serverComboBox.setModel(new ServerModel());
209         serverComboBox.addItemListener(new java.awt.event.ItemListener JavaDoc() {
210             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
211                 serverComboBoxItemStateChanged(evt);
212             }
213         });
214
215         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
216         gridBagConstraints.gridy = 0;
217         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
218         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
219         gridBagConstraints.weightx = 1.0;
220         add(serverComboBox, gridBagConstraints);
221         serverComboBox.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ServerChooserVisual.class, "A11Y_SCV_NAME_Server"));
222         serverComboBox.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ServerChooserVisual.class, "A11Y_SCV_DESC_Server"));
223
224         jLabel2.setDisplayedMnemonic(org.openide.util.NbBundle.getBundle(ServerChooserVisual.class).getString("LBL_SCV_DisplayName_mnem").charAt(0));
225         jLabel2.setLabelFor(displayNameEditField);
226         jLabel2.setText(org.openide.util.NbBundle.getBundle(ServerChooserVisual.class).getString("LBL_SCV_DisplayName"));
227         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
228         gridBagConstraints.gridx = 0;
229         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
230         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 12);
231         add(jLabel2, gridBagConstraints);
232
233         displayNameEditField.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
234             public void keyReleased(java.awt.event.KeyEvent JavaDoc evt) {
235                 displayNameEditFieldKeyReleased(evt);
236             }
237         });
238
239         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
240         gridBagConstraints.gridy = 1;
241         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
242         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
243         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
244         add(displayNameEditField, gridBagConstraints);
245         displayNameEditField.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ServerChooserVisual.class, "A11Y_SCV_NAME_DisplayName"));
246         displayNameEditField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ServerChooserVisual.class, "A11Y_SCV_DESC_DisplayName"));
247
248         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
249         gridBagConstraints.gridx = 0;
250         gridBagConstraints.gridy = 2;
251         gridBagConstraints.gridwidth = 2;
252         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
253         gridBagConstraints.weightx = 1.0;
254         gridBagConstraints.weighty = 1.0;
255         add(jPanel1, gridBagConstraints);
256
257     }//GEN-END:initComponents
258

259     private void displayNameEditFieldKeyReleased(java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_displayNameEditFieldKeyReleased
260
ServerAdapter serverAdapter = (ServerAdapter) serverComboBox.getSelectedItem();
261         if (serverAdapter != null) {
262             displayNames.put(serverAdapter.getServer(), displayNameEditField.getText());
263         }
264     }//GEN-LAST:event_displayNameEditFieldKeyReleased
265

266     private void serverComboBoxItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_serverComboBoxItemStateChanged
267
if (evt.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
268             Server server = ((ServerAdapter)evt.getItem()).getServer();
269             fillDisplayName(server);
270         }
271         fireChange();
272     }//GEN-LAST:event_serverComboBoxItemStateChanged
273

274     
275     // Variables declaration - do not modify//GEN-BEGIN:variables
276
private javax.swing.JTextField JavaDoc displayNameEditField;
277     private javax.swing.JLabel JavaDoc jLabel1;
278     private javax.swing.JLabel JavaDoc jLabel2;
279     private javax.swing.JPanel JavaDoc jPanel1;
280     private javax.swing.JComboBox JavaDoc serverComboBox;
281     // End of variables declaration//GEN-END:variables
282

283     private static class ServerModel implements ComboBoxModel JavaDoc {
284         private List JavaDoc servers;
285         private ServerAdapter selected;
286                 
287         public ServerModel() {
288             servers = new ArrayList JavaDoc();
289             Collection JavaDoc allServers = ServerRegistry.getInstance().getServers();
290             Iterator JavaDoc iter = allServers.iterator();
291             while (iter.hasNext()) {
292                 Server server = (Server)iter.next();
293                 OptionalDeploymentManagerFactory factory = server.getOptionalFactory();
294                 if (factory != null && factory.getAddInstanceIterator() != null) {
295                     ServerAdapter serverAdapter = new ServerAdapter(server);
296                     servers.add(serverAdapter);
297                     if ("J2EE".equals(server.getShortName())) { // NOI18N
298
selected = serverAdapter;
299                     }
300                 }
301             }
302             Collections.sort(servers);
303             if (selected == null) {
304                 selected = (servers.size() > 0) ? (ServerAdapter)servers.get(0) : null;
305             }
306         }
307         
308         public Object JavaDoc getElementAt(int index) {
309             return servers.get(index);
310         }
311
312         public void removeListDataListener(javax.swing.event.ListDataListener JavaDoc l) {
313         }
314
315         public void addListDataListener(javax.swing.event.ListDataListener JavaDoc l) {
316         }
317
318         public int getSize() {
319             return servers.size();
320         }
321         
322         public Object JavaDoc getSelectedItem() {
323             return selected;
324         }
325         
326         public void setSelectedItem(Object JavaDoc anItem) {
327             selected = (ServerAdapter)anItem;
328         }
329     }
330     
331     private static class ServerAdapter implements Comparable JavaDoc {
332         private Server server;
333         
334         public ServerAdapter(Server server) {
335             this.server = server;
336         }
337         
338         public Server getServer() {
339             return server;
340         }
341         
342         public String JavaDoc toString() {
343             return server.getDisplayName();
344         }
345         
346         public int compareTo(Object JavaDoc o) {
347             return toString().compareTo(o.toString());
348         }
349     }
350 }
351
Popular Tags