1 19 20 package org.netbeans.modules.j2ee.common.ui; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import javax.swing.AbstractListModel ; 27 import javax.swing.JLabel ; 28 import javax.swing.JList ; 29 import javax.swing.JPanel ; 30 import javax.swing.ListCellRenderer ; 31 import javax.swing.ListSelectionModel ; 32 import javax.swing.event.ListSelectionEvent ; 33 import javax.swing.event.ListSelectionListener ; 34 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 35 import org.netbeans.modules.j2ee.deployment.devmodules.api.ServerManager; 36 import org.openide.DialogDescriptor; 37 import org.openide.DialogDisplayer; 38 import org.openide.util.NbBundle; 39 40 45 public final class NoSelectedServerWarning extends JPanel { 46 47 public static final String OK_ENABLED = "ok_enabled"; 49 private NoSelectedServerWarning(Object [] moduleTypes, String j2eeSpec) { 50 initComponents(); 51 serverList.setModel(new ServerListModel(moduleTypes, j2eeSpec)); 52 if (serverList.getModel().getSize() > 0) { 53 jTextArea2.setVisible(false); 54 } 55 initServerList(); 56 } 57 58 private void initServerList() { 59 serverList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 60 serverList.addListSelectionListener(new ListSelectionListener () { 61 public void valueChanged(ListSelectionEvent e) { 62 boolean enabled = !serverList.isSelectionEmpty(); 63 firePropertyChange(OK_ENABLED, !enabled, enabled); 64 } 65 } 66 ); 67 serverList.setCellRenderer(new ServersRenderer()); 68 } 69 70 82 public static String selectServerDialog(Object [] moduleTypes, String j2eeSpec, String title, String description) { 83 NoSelectedServerWarning panel = new NoSelectedServerWarning(moduleTypes, j2eeSpec); 84 Object [] options = new Object [] { 85 DialogDescriptor.OK_OPTION, 86 DialogDescriptor.CANCEL_OPTION 87 }; 88 final DialogDescriptor desc = new DialogDescriptor(panel, title, true, options, 89 DialogDescriptor.OK_OPTION, DialogDescriptor.DEFAULT_ALIGN, null, null); 90 desc.setMessageType(DialogDescriptor.WARNING_MESSAGE); 91 Dialog dlg = null; 92 try { 93 dlg = DialogDisplayer.getDefault().createDialog(desc); 94 dlg.getAccessibleContext().setAccessibleDescription(description); 95 panel.addPropertyChangeListener(new PropertyChangeListener () { 96 public void propertyChange(PropertyChangeEvent evt) { 97 if (evt.getPropertyName().equals(NoSelectedServerWarning.OK_ENABLED)) { 98 Object newvalue = evt.getNewValue(); 99 if ((newvalue != null) && (newvalue instanceof Boolean )) { 100 desc.setValid(((Boolean )newvalue).booleanValue()); 101 } 102 } 103 } 104 } 105 ); 106 desc.setValid(panel.getSelectedInstance() != null); 107 panel.setSize(panel.getPreferredSize()); 108 dlg.pack(); 109 dlg.setVisible(true); 110 } finally { 111 if (dlg != null) { 112 dlg.dispose(); 113 } 114 } 115 return desc.getValue() == DialogDescriptor.OK_OPTION 116 ? panel.getSelectedInstance() 117 : null; 118 } 119 120 124 public String getSelectedInstance() { 125 if (serverList.getSelectedIndex() == -1) { 126 return null; 127 } else { 128 return (String )serverList.getSelectedValue(); 129 } 130 } 131 132 137 private void initComponents() { 139 java.awt.GridBagConstraints gridBagConstraints; 140 141 jScrollPane2 = new javax.swing.JScrollPane (); 142 serverList = new javax.swing.JList (); 143 jTextArea1 = new javax.swing.JTextArea (); 144 jTextArea2 = new javax.swing.JTextArea (); 145 jButtonAddServer = new javax.swing.JButton (); 146 147 setLayout(new java.awt.GridBagLayout ()); 148 149 jScrollPane2.setMinimumSize(new java.awt.Dimension (200, 100)); 150 serverList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); 151 serverList.setPreferredSize(null); 152 serverList.setVisibleRowCount(4); 153 jScrollPane2.setViewportView(serverList); 154 155 gridBagConstraints = new java.awt.GridBagConstraints (); 156 gridBagConstraints.gridx = 0; 157 gridBagConstraints.gridy = 2; 158 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 159 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 160 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 161 gridBagConstraints.weightx = 1.0; 162 gridBagConstraints.weighty = 1.0; 163 gridBagConstraints.insets = new java.awt.Insets (6, 12, 12, 12); 164 add(jScrollPane2, gridBagConstraints); 165 166 jTextArea1.setColumns(25); 167 jTextArea1.setEditable(false); 168 jTextArea1.setLineWrap(true); 169 jTextArea1.setText(NbBundle.getMessage(NoSelectedServerWarning.class, "LBL_NoSelectedServerWarning_jLabel1")); 170 jTextArea1.setWrapStyleWord(true); 171 jTextArea1.setFocusable(false); 172 jTextArea1.setOpaque(false); 173 gridBagConstraints = new java.awt.GridBagConstraints (); 174 gridBagConstraints.gridx = 0; 175 gridBagConstraints.gridy = 0; 176 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 177 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 178 gridBagConstraints.insets = new java.awt.Insets (12, 12, 6, 12); 179 add(jTextArea1, gridBagConstraints); 180 181 jTextArea2.setEditable(false); 182 jTextArea2.setLineWrap(true); 183 jTextArea2.setText(org.openide.util.NbBundle.getMessage(NoSelectedServerWarning.class, "LBL_NoSuitableServerWarning_jLabel2")); 184 jTextArea2.setWrapStyleWord(true); 185 jTextArea2.setFocusable(false); 186 jTextArea2.setOpaque(false); 187 gridBagConstraints = new java.awt.GridBagConstraints (); 188 gridBagConstraints.gridx = 0; 189 gridBagConstraints.gridy = 1; 190 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 191 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 192 gridBagConstraints.weightx = 1.0; 193 gridBagConstraints.insets = new java.awt.Insets (12, 12, 6, 12); 194 add(jTextArea2, gridBagConstraints); 195 196 org.openide.awt.Mnemonics.setLocalizedText(jButtonAddServer, org.openide.util.NbBundle.getMessage(NoSelectedServerWarning.class, "LBL_AddServer")); 197 jButtonAddServer.addActionListener(new java.awt.event.ActionListener () { 198 public void actionPerformed(java.awt.event.ActionEvent evt) { 199 jButtonAddServerActionPerformed(evt); 200 } 201 }); 202 203 gridBagConstraints = new java.awt.GridBagConstraints (); 204 gridBagConstraints.gridx = 0; 205 gridBagConstraints.gridy = 3; 206 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 207 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 208 gridBagConstraints.anchor = java.awt.GridBagConstraints.LAST_LINE_START; 209 gridBagConstraints.insets = new java.awt.Insets (0, 12, 5, 0); 210 add(jButtonAddServer, gridBagConstraints); 211 jButtonAddServer.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(NoSelectedServerWarning.class, "ACSN_AddServer")); 212 jButtonAddServer.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(NoSelectedServerWarning.class, "ACSD_AddServer")); 213 214 } 216 private void jButtonAddServerActionPerformed(java.awt.event.ActionEvent evt) { ServerListModel model = (ServerListModel) serverList.getModel(); 218 String lastSelectedValue = (String ) serverList.getSelectedValue(); 219 boolean wasEmpty = (model.getSize() == 0); 220 ServerManager.showCustomizer((String ) lastSelectedValue); 221 model.refreshModel(); 222 boolean isEmpty = (model.getSize() == 0); 223 if (wasEmpty != isEmpty) { 224 jTextArea2.setVisible(isEmpty); 225 } 226 if (lastSelectedValue != null && Deployment.getDefault().getJ2eePlatform(lastSelectedValue) != null) { 227 serverList.setSelectedValue(lastSelectedValue, true); 229 } else { 230 serverList.clearSelection(); 231 } 232 } 234 235 private javax.swing.JButton jButtonAddServer; 237 private javax.swing.JScrollPane jScrollPane2; 238 private javax.swing.JTextArea jTextArea1; 239 private javax.swing.JTextArea jTextArea2; 240 private javax.swing.JList serverList; 241 243 244 private static final class ServerListModel extends AbstractListModel { 245 246 private String [] instances; 247 248 private final Object [] moduleTypes; 249 private final String j2eeSpec; 250 251 public ServerListModel(Object [] moduleTypes, String j2eeSpec) { 252 this.moduleTypes = moduleTypes; 253 this.j2eeSpec = j2eeSpec; 254 instances = Deployment.getDefault().getServerInstanceIDs(moduleTypes, j2eeSpec); 255 } 256 257 public synchronized int getSize() { 258 return instances.length; 259 } 260 261 public synchronized Object getElementAt(int index) { 262 if (index >= 0 && index < instances.length) { 263 return instances [index]; 264 } else { 265 return null; 266 } 267 } 268 269 public synchronized void refreshModel() { 270 int oldLength = instances.length; 271 instances = Deployment.getDefault().getServerInstanceIDs(moduleTypes, j2eeSpec); 272 if (instances.length > 0) { 273 fireContentsChanged(this, 0, instances.length - 1); 274 } else if (oldLength > 0) { 275 fireIntervalRemoved(this, 0, oldLength - 1); 276 } 277 } 278 279 } 280 281 private static final class ServersRenderer extends JLabel implements ListCellRenderer { 282 283 ServersRenderer() { 284 setOpaque(true); 285 } 286 287 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 288 if (value instanceof String ) { 289 String id = (String ) value; 290 setText(Deployment.getDefault().getServerInstanceDisplayName(id)); 291 } else { 293 setText(value.toString()); 294 setIcon(null); 295 } 296 if (isSelected) { 297 setBackground(list.getSelectionBackground()); 298 setForeground(list.getSelectionForeground()); 299 } else { 301 setBackground(list.getBackground()); 302 setForeground(list.getForeground()); 303 } 305 return this; 306 } 307 } 308 309 } 310 | Popular Tags |