1 19 20 package org.netbeans.modules.j2ee.deployment.impl.ui.wizard; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import javax.swing.ComboBoxModel ; 29 import javax.swing.event.ChangeEvent ; 30 import javax.swing.event.ChangeListener ; 31 import javax.swing.event.DocumentEvent ; 32 import javax.swing.event.DocumentListener ; 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 43 public class ServerChooserVisual extends javax.swing.JPanel { 44 private final List listeners = new ArrayList (); 45 private AddServerInstanceWizard wizard; 46 private HashMap displayNames; 47 48 public ServerChooserVisual() { 49 displayNames = new HashMap (); 50 initComponents(); 51 52 ServerAdapter selected = (ServerAdapter)serverComboBox.getSelectedItem(); 53 if (selected != null) 54 fillDisplayName(selected.getServer()); 55 56 displayNameEditField.getDocument().addDocumentListener(new DocumentListener () { 57 public void insertUpdate(DocumentEvent e) { 58 displayNameEditFieldUpdate(); 59 } 60 61 public void removeUpdate(DocumentEvent e) { 62 displayNameEditFieldUpdate(); 63 } 64 65 public void changedUpdate(DocumentEvent e) { 66 displayNameEditFieldUpdate(); 67 } 68 }); 69 } 70 71 public void addChangeListener(ChangeListener l) { 72 synchronized (listeners) { 73 listeners.add(l); 74 } 75 } 76 77 public void removeChangeListener(ChangeListener 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 prop = wizard.getProperty(AddServerInstanceWizard.PROP_DISPLAY_NAME); 88 if (prop != null) 89 displayNameEditField.setText((String )prop); 90 } 91 92 public void store(AddServerInstanceWizard wizard) { 93 wizard.putProperty(AddServerInstanceWizard.PROP_DISPLAY_NAME, displayNameEditField.getText()); 94 Object 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 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 displayName) { 133 Iterator 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 event = new ChangeEvent (this); 148 ArrayList tempList; 149 150 synchronized (listeners) { 151 tempList = new ArrayList (listeners); 152 } 153 154 Iterator iter = tempList.iterator(); 155 while (iter.hasNext()) 156 ((ChangeListener )iter.next()).stateChanged(event); 157 } 158 159 private String generateDisplayName(Server server) { 160 String 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 name = (String )displayNames.get(server); 176 if (name == null) 177 name = generateDisplayName(server); 178 displayNameEditField.setText(name); 179 } 180 181 186 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 188 189 jLabel1 = new javax.swing.JLabel (); 190 serverComboBox = new javax.swing.JComboBox (); 191 jLabel2 = new javax.swing.JLabel (); 192 displayNameEditField = new javax.swing.JTextField (); 193 jPanel1 = new javax.swing.JPanel (); 194 195 setLayout(new java.awt.GridBagLayout ()); 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 (); 204 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 205 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 206 add(jLabel1, gridBagConstraints); 207 208 serverComboBox.setModel(new ServerModel()); 209 serverComboBox.addItemListener(new java.awt.event.ItemListener () { 210 public void itemStateChanged(java.awt.event.ItemEvent evt) { 211 serverComboBoxItemStateChanged(evt); 212 } 213 }); 214 215 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (); 228 gridBagConstraints.gridx = 0; 229 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 230 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 12); 231 add(jLabel2, gridBagConstraints); 232 233 displayNameEditField.addKeyListener(new java.awt.event.KeyAdapter () { 234 public void keyReleased(java.awt.event.KeyEvent evt) { 235 displayNameEditFieldKeyReleased(evt); 236 } 237 }); 238 239 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (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 (); 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 } 259 private void displayNameEditFieldKeyReleased(java.awt.event.KeyEvent evt) { ServerAdapter serverAdapter = (ServerAdapter) serverComboBox.getSelectedItem(); 261 if (serverAdapter != null) { 262 displayNames.put(serverAdapter.getServer(), displayNameEditField.getText()); 263 } 264 } 266 private void serverComboBoxItemStateChanged(java.awt.event.ItemEvent evt) { if (evt.getStateChange() == java.awt.event.ItemEvent.SELECTED) { 268 Server server = ((ServerAdapter)evt.getItem()).getServer(); 269 fillDisplayName(server); 270 } 271 fireChange(); 272 } 274 275 private javax.swing.JTextField displayNameEditField; 277 private javax.swing.JLabel jLabel1; 278 private javax.swing.JLabel jLabel2; 279 private javax.swing.JPanel jPanel1; 280 private javax.swing.JComboBox serverComboBox; 281 283 private static class ServerModel implements ComboBoxModel { 284 private List servers; 285 private ServerAdapter selected; 286 287 public ServerModel() { 288 servers = new ArrayList (); 289 Collection allServers = ServerRegistry.getInstance().getServers(); 290 Iterator 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())) { 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 getElementAt(int index) { 309 return servers.get(index); 310 } 311 312 public void removeListDataListener(javax.swing.event.ListDataListener l) { 313 } 314 315 public void addListDataListener(javax.swing.event.ListDataListener l) { 316 } 317 318 public int getSize() { 319 return servers.size(); 320 } 321 322 public Object getSelectedItem() { 323 return selected; 324 } 325 326 public void setSelectedItem(Object anItem) { 327 selected = (ServerAdapter)anItem; 328 } 329 } 330 331 private static class ServerAdapter implements Comparable { 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 toString() { 343 return server.getDisplayName(); 344 } 345 346 public int compareTo(Object o) { 347 return toString().compareTo(o.toString()); 348 } 349 } 350 } 351 | Popular Tags |