KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > common > ui > NoSelectedServerWarning


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.common.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import javax.swing.AbstractListModel JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.JList JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import javax.swing.ListCellRenderer JavaDoc;
31 import javax.swing.ListSelectionModel JavaDoc;
32 import javax.swing.event.ListSelectionEvent JavaDoc;
33 import javax.swing.event.ListSelectionListener JavaDoc;
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 /**
41  * Shows a warning that no server is set and allows choose it.
42  *
43  * @author Pavel Buzek
44  */

45 public final class NoSelectedServerWarning extends JPanel JavaDoc {
46     
47     public static final String JavaDoc OK_ENABLED = "ok_enabled"; //NOI18N
48

49     private NoSelectedServerWarning(Object JavaDoc[] moduleTypes, String JavaDoc 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 JavaDoc() {
61             public void valueChanged(ListSelectionEvent JavaDoc e) {
62                 boolean enabled = !serverList.isSelectionEmpty();
63                 firePropertyChange(OK_ENABLED, !enabled, enabled);
64             }
65         }
66         );
67         serverList.setCellRenderer(new ServersRenderer());
68     }
69     
70     /**
71      * Show the "no selected server" dialog and let the user choose server instance from
72      * the list.
73      *
74      * @param moduleTypes module types that servers should support
75      * @param j2eeSpec lowest j2ee specification version that servers should support
76      * @param title dialog title
77      * @param description dialog accessible description
78      *
79      * @return serverInstanceId of the selected server instance, <code>null</code>
80      * if canceled.
81      */

82     public static String JavaDoc selectServerDialog(Object JavaDoc[] moduleTypes, String JavaDoc j2eeSpec, String JavaDoc title, String JavaDoc description) {
83         NoSelectedServerWarning panel = new NoSelectedServerWarning(moduleTypes, j2eeSpec);
84         Object JavaDoc[] options = new Object JavaDoc[] {
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 JavaDoc dlg = null;
92         try {
93             dlg = DialogDisplayer.getDefault().createDialog(desc);
94             dlg.getAccessibleContext().setAccessibleDescription(description);
95             panel.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
96                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
97                     if (evt.getPropertyName().equals(NoSelectedServerWarning.OK_ENABLED)) {
98                         Object JavaDoc newvalue = evt.getNewValue();
99                         if ((newvalue != null) && (newvalue instanceof Boolean JavaDoc)) {
100                             desc.setValid(((Boolean JavaDoc)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     /** Returns the selected server instance Id or null if no instance was selected.
121      *
122      * @return server instance ID or null if no instance is selected
123      */

124     public String JavaDoc getSelectedInstance() {
125         if (serverList.getSelectedIndex() == -1) {
126             return null;
127         } else {
128             return (String JavaDoc)serverList.getSelectedValue();
129         }
130     }
131     
132     /** This method is called from within the constructor to
133      * initialize the form.
134      * WARNING: Do NOT modify this code. The content of this method is
135      * always regenerated by the Form Editor.
136      */

137     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
138
private void initComponents() {
139         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
140
141         jScrollPane2 = new javax.swing.JScrollPane JavaDoc();
142         serverList = new javax.swing.JList JavaDoc();
143         jTextArea1 = new javax.swing.JTextArea JavaDoc();
144         jTextArea2 = new javax.swing.JTextArea JavaDoc();
145         jButtonAddServer = new javax.swing.JButton JavaDoc();
146
147         setLayout(new java.awt.GridBagLayout JavaDoc());
148
149         jScrollPane2.setMinimumSize(new java.awt.Dimension JavaDoc(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 JavaDoc();
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 JavaDoc(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 JavaDoc();
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 JavaDoc(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 JavaDoc();
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 JavaDoc(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 JavaDoc() {
198             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
199                 jButtonAddServerActionPerformed(evt);
200             }
201         });
202
203         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(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     }// </editor-fold>//GEN-END:initComponents
215

216     private void jButtonAddServerActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonAddServerActionPerformed
217
ServerListModel model = (ServerListModel) serverList.getModel();
218         String JavaDoc lastSelectedValue = (String JavaDoc) serverList.getSelectedValue();
219         boolean wasEmpty = (model.getSize() == 0);
220         ServerManager.showCustomizer((String JavaDoc) 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             // if the last selected server instance still exists, select it again
228
serverList.setSelectedValue(lastSelectedValue, true);
229         } else {
230             serverList.clearSelection();
231         }
232     }//GEN-LAST:event_jButtonAddServerActionPerformed
233

234     
235     // Variables declaration - do not modify//GEN-BEGIN:variables
236
private javax.swing.JButton JavaDoc jButtonAddServer;
237     private javax.swing.JScrollPane JavaDoc jScrollPane2;
238     private javax.swing.JTextArea JavaDoc jTextArea1;
239     private javax.swing.JTextArea JavaDoc jTextArea2;
240     private javax.swing.JList JavaDoc serverList;
241     // End of variables declaration//GEN-END:variables
242

243     
244     private static final class ServerListModel extends AbstractListModel JavaDoc {
245         
246         private String JavaDoc[] instances;
247         
248         private final Object JavaDoc[] moduleTypes;
249         private final String JavaDoc j2eeSpec;
250         
251         public ServerListModel(Object JavaDoc[] moduleTypes, String JavaDoc 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 JavaDoc 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 JavaDoc implements ListCellRenderer JavaDoc {
282         
283         ServersRenderer() {
284             setOpaque(true);
285         }
286         
287         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
288             if (value instanceof String JavaDoc) {
289                 String JavaDoc id = (String JavaDoc) value;
290                 setText(Deployment.getDefault().getServerInstanceDisplayName(id));
291                 // setIcon (ProjectUtils.getInformation (prj).getIcon ());
292
} else {
293                 setText(value.toString());
294                 setIcon(null);
295             }
296             if (isSelected) {
297                 setBackground(list.getSelectionBackground());
298                 setForeground(list.getSelectionForeground());
299                 //setBorder (BorderFactory.createLineBorder (Color.BLACK));
300
} else {
301                 setBackground(list.getBackground());
302                 setForeground(list.getForeground());
303                 //setBorder (null);
304
}
305             return this;
306         }
307     }
308     
309 }
310
Popular Tags