KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > ui > wizards > PanelOptionsVisual


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.ejbjarproject.ui.wizards;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.TreeSet JavaDoc;
26 import javax.swing.DefaultComboBoxModel JavaDoc;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.api.project.ProjectInformation;
29 import org.netbeans.api.project.ProjectUtils;
30 import org.netbeans.api.project.ui.OpenProjects;
31 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
32 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
33 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModuleContainer;
34 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
35 import org.netbeans.modules.j2ee.deployment.devmodules.api.ServerManager;
36 import org.netbeans.modules.j2ee.ejbjarproject.ui.FoldersListSettings;
37 import org.openide.WizardDescriptor;
38 import org.openide.util.NbBundle;
39
40 public class PanelOptionsVisual extends javax.swing.JPanel JavaDoc {
41     
42 // private static boolean lastMainClassCheck = false; // XXX Store somewhere
43

44     private PanelConfigureProject panel;
45     private J2eeVersionWarningPanel warningPanel;
46     private final DefaultComboBoxModel JavaDoc serversModel = new DefaultComboBoxModel JavaDoc();
47     
48     private List JavaDoc earProjects;
49     
50     private static final String JavaDoc J2EE_SPEC_15_LABEL = NbBundle.getMessage(PanelOptionsVisual.class, "J2EESpecLevel_15"); //NOI18N
51
private static final String JavaDoc J2EE_SPEC_14_LABEL = NbBundle.getMessage(PanelOptionsVisual.class, "J2EESpecLevel_14"); //NOI18N
52
// private String j2eeLevel;
53

54     /** Creates new form PanelOptionsVisual */
55     public PanelOptionsVisual(PanelConfigureProject panel) {
56         initComponents();
57         this.panel = panel;
58         setJ2eeVersionWarningPanel();
59         initServers(FoldersListSettings.getDefault().getLastUsedServer());
60         // preselect the first item in the j2ee spec combo
61
if (j2eeSpecComboBox.getModel().getSize() > 0) {
62             j2eeSpecComboBox.setSelectedIndex(0);
63         }
64         initEnterpriseApplications();
65     }
66     
67     /** This method is called from within the constructor to
68      * initialize the form.
69      * WARNING: Do NOT modify this code. The content of this method is
70      * always regenerated by the Form Editor.
71      */

72     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
73
private void initComponents() {
74         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
75
76         setAsMainCheckBox = new javax.swing.JCheckBox JavaDoc();
77         j2eeSpecLabel = new javax.swing.JLabel JavaDoc();
78         j2eeSpecComboBox = new javax.swing.JComboBox JavaDoc();
79         serverInstanceLabel = new javax.swing.JLabel JavaDoc();
80         serverInstanceComboBox = new javax.swing.JComboBox JavaDoc();
81         addToAppLabel = new javax.swing.JLabel JavaDoc();
82         addToAppComboBox = new javax.swing.JComboBox JavaDoc();
83         warningPlaceHolderPanel = new javax.swing.JPanel JavaDoc();
84         manageServersButton = new javax.swing.JButton JavaDoc();
85
86         setLayout(new java.awt.GridBagLayout JavaDoc());
87
88         setAsMainCheckBox.setSelected(true);
89         org.openide.awt.Mnemonics.setLocalizedText(setAsMainCheckBox, NbBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("LBL_NWP1_SetAsMain_CheckBox"));
90         setAsMainCheckBox.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
91         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
92         gridBagConstraints.gridx = 0;
93         gridBagConstraints.gridy = 4;
94         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
95         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
96         gridBagConstraints.weightx = 1.0;
97         gridBagConstraints.weighty = 1.0;
98         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
99         add(setAsMainCheckBox, gridBagConstraints);
100         setAsMainCheckBox.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelOptionsVisual.class, "ACS_LBL_NWP1_SetAsMain_A11YDesc"));
101
102         j2eeSpecLabel.setLabelFor(j2eeSpecComboBox);
103         org.openide.awt.Mnemonics.setLocalizedText(j2eeSpecLabel, NbBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("LBL_NWP1_J2EESpecLevel_Label"));
104         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
105         gridBagConstraints.gridx = 0;
106         gridBagConstraints.gridy = 2;
107         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
108         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 11);
109         add(j2eeSpecLabel, gridBagConstraints);
110
111         j2eeSpecComboBox.setPrototypeDisplayValue("MMMMMMMMM" /* "Java EE 5" */);
112         j2eeSpecComboBox.addActionListener(new java.awt.event.ActionListener JavaDoc() {
113             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
114                 j2eeSpecComboBoxActionPerformed(evt);
115             }
116         });
117
118         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
119         gridBagConstraints.gridx = 1;
120         gridBagConstraints.gridy = 2;
121         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
122         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
123         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
124         add(j2eeSpecComboBox, gridBagConstraints);
125         j2eeSpecComboBox.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelOptionsVisual.class, "ACS_LBL_NPW1_J2EESpecLevel_A11YDesc"));
126
127         serverInstanceLabel.setLabelFor(serverInstanceComboBox);
128         org.openide.awt.Mnemonics.setLocalizedText(serverInstanceLabel, org.openide.util.NbBundle.getMessage(PanelOptionsVisual.class, "LBL_NWP1_Server_Label"));
129         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
130         gridBagConstraints.gridx = 0;
131         gridBagConstraints.gridy = 1;
132         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
133         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 11);
134         add(serverInstanceLabel, gridBagConstraints);
135
136         serverInstanceComboBox.setModel(serversModel);
137         serverInstanceComboBox.addActionListener(new java.awt.event.ActionListener JavaDoc() {
138             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
139                 serverInstanceComboBoxActionPerformed(evt);
140             }
141         });
142
143         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
144         gridBagConstraints.gridx = 1;
145         gridBagConstraints.gridy = 1;
146         gridBagConstraints.gridwidth = 2;
147         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
148         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
149         gridBagConstraints.weightx = 1.0;
150         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 6);
151         add(serverInstanceComboBox, gridBagConstraints);
152         serverInstanceComboBox.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelOptionsVisual.class, "ACS_NEJB_Server_ComboBox_A11YDesc"));
153
154         addToAppLabel.setLabelFor(addToAppComboBox);
155         org.openide.awt.Mnemonics.setLocalizedText(addToAppLabel, java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ejbjarproject/ui/wizards/Bundle").getString("LBL_NWP1_AddToEApp_CheckBox"));
156         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
157         gridBagConstraints.gridx = 0;
158         gridBagConstraints.gridy = 0;
159         gridBagConstraints.gridwidth = 2;
160         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
161         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 11);
162         add(addToAppLabel, gridBagConstraints);
163
164         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
165         gridBagConstraints.gridx = 2;
166         gridBagConstraints.gridy = 0;
167         gridBagConstraints.gridwidth = 2;
168         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
169         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
170         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
171         add(addToAppComboBox, gridBagConstraints);
172
173         warningPlaceHolderPanel.setLayout(new java.awt.BorderLayout JavaDoc());
174
175         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
176         gridBagConstraints.gridx = 0;
177         gridBagConstraints.gridy = 3;
178         gridBagConstraints.gridwidth = 3;
179         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
180         gridBagConstraints.weightx = 1.0;
181         add(warningPlaceHolderPanel, gridBagConstraints);
182
183         org.openide.awt.Mnemonics.setLocalizedText(manageServersButton, org.openide.util.NbBundle.getMessage(PanelOptionsVisual.class, "LBL_ManageServers"));
184         manageServersButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
185             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
186                 manageServersButtonActionPerformed(evt);
187             }
188         });
189
190         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
191         gridBagConstraints.gridx = 3;
192         gridBagConstraints.gridy = 1;
193         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
194         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
195         add(manageServersButton, gridBagConstraints);
196         manageServersButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelOptionsVisual.class, "ASCN_ManageServers"));
197         manageServersButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelOptionsVisual.class, "ASCD_ManageServers"));
198
199     }// </editor-fold>//GEN-END:initComponents
200

201     private void manageServersButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_manageServersButtonActionPerformed
202
ServerInstanceWrapper serverInstanceWrapper = (ServerInstanceWrapper) serversModel.getSelectedItem();
203         String JavaDoc lastSelectedServerInstanceID = null;
204         if (serverInstanceWrapper != null) {
205             lastSelectedServerInstanceID = serverInstanceWrapper.getServerInstanceID();
206         }
207         ServerManager.showCustomizer(lastSelectedServerInstanceID);
208         String JavaDoc lastSelectedJ2eeSpecLevel = (String JavaDoc) j2eeSpecComboBox.getSelectedItem();
209         // refresh the list of servers
210
initServers(lastSelectedServerInstanceID);
211         if (lastSelectedJ2eeSpecLevel != null) {
212             j2eeSpecComboBox.setSelectedItem(lastSelectedJ2eeSpecLevel);
213         }
214     }//GEN-LAST:event_manageServersButtonActionPerformed
215

216     private void j2eeSpecComboBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_j2eeSpecComboBoxActionPerformed
217
setJ2eeVersionWarningPanel();
218     }//GEN-LAST:event_j2eeSpecComboBoxActionPerformed
219

220     private void serverInstanceComboBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_serverInstanceComboBoxActionPerformed
221
String JavaDoc prevSelectedItem = (String JavaDoc) j2eeSpecComboBox.getSelectedItem();
222         // update the j2ee spec list according to the selected server
223
ServerInstanceWrapper serverInstanceWrapper = (ServerInstanceWrapper) serversModel.getSelectedItem();
224         if (serverInstanceWrapper != null) {
225             J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(serverInstanceWrapper.getServerInstanceID());
226             Set JavaDoc supportedVersions = j2eePlatform.getSupportedSpecVersions(J2eeModule.EJB);
227             j2eeSpecComboBox.removeAllItems();
228             if (supportedVersions.contains(J2eeModule.JAVA_EE_5)) {
229                 j2eeSpecComboBox.addItem(J2EE_SPEC_15_LABEL);
230             }
231             if (supportedVersions.contains(J2eeModule.J2EE_14)) {
232                 j2eeSpecComboBox.addItem(J2EE_SPEC_14_LABEL);
233             }
234             if (prevSelectedItem != null) {
235                 j2eeSpecComboBox.setSelectedItem(prevSelectedItem);
236             }
237         } else {
238             j2eeSpecComboBox.removeAllItems();
239         }
240         // revalidate the form
241
panel.fireChangeEvent();
242     }//GEN-LAST:event_serverInstanceComboBoxActionPerformed
243

244     boolean valid(WizardDescriptor wizardDescriptor) {
245         if (getSelectedServer() == null) {
246             String JavaDoc errMsg = NbBundle.getMessage(PanelOptionsVisual.class, "MSG_NoServer");
247             wizardDescriptor.putProperty( "WizardPanel_errorMessage", errMsg); // NOI18N
248
return false;
249         }
250         return true;
251     }
252
253     void store(WizardDescriptor d) {
254         d.putProperty(WizardProperties.SET_AS_MAIN, setAsMainCheckBox.isSelected() ? Boolean.TRUE : Boolean.FALSE );
255         d.putProperty(WizardProperties.SERVER_INSTANCE_ID, getSelectedServer());
256         d.putProperty(WizardProperties.J2EE_LEVEL, getSelectedJ2eeSpec());
257         d.putProperty(WizardProperties.EAR_APPLICATION, getSelectedEarApplication());
258         if (warningPanel != null && warningPanel.getWarningType() != null && warningPanel.getDowngradeAllowed()) {
259             d.putProperty(WizardProperties.JAVA_PLATFORM, warningPanel.getSuggestedJavaPlatformName());
260             
261             String JavaDoc j2ee = getSelectedJ2eeSpec();
262             if (j2ee != null) {
263                 String JavaDoc warningType = J2eeVersionWarningPanel.findWarningType(j2ee);
264                 FoldersListSettings fls = FoldersListSettings.getDefault();
265                 String JavaDoc srcLevel = "1.6"; //NOI18N
266
if (warningType.equals(J2eeVersionWarningPanel.WARN_SET_SOURCE_LEVEL_14) && fls.isAgreedSetSourceLevel14())
267                     srcLevel = "1.4"; //NOI18N
268
else if (warningType.equals(J2eeVersionWarningPanel.WARN_SET_SOURCE_LEVEL_15) && fls.isAgreedSetSourceLevel15())
269                     srcLevel = "1.5"; //NOI18N
270

271                 d.putProperty(WizardProperties.SOURCE_LEVEL, srcLevel);
272             }
273         } else
274             d.putProperty(WizardProperties.SOURCE_LEVEL, null);
275     }
276     
277     void read(WizardDescriptor d) {
278     }
279     
280     // Variables declaration - do not modify//GEN-BEGIN:variables
281
private javax.swing.JComboBox JavaDoc addToAppComboBox;
282     private javax.swing.JLabel JavaDoc addToAppLabel;
283     private javax.swing.JComboBox JavaDoc j2eeSpecComboBox;
284     private javax.swing.JLabel JavaDoc j2eeSpecLabel;
285     private javax.swing.JButton JavaDoc manageServersButton;
286     private javax.swing.JComboBox JavaDoc serverInstanceComboBox;
287     private javax.swing.JLabel JavaDoc serverInstanceLabel;
288     private javax.swing.JCheckBox JavaDoc setAsMainCheckBox;
289     private javax.swing.JPanel JavaDoc warningPlaceHolderPanel;
290     // End of variables declaration//GEN-END:variables
291

292     /**
293      * Init servers model
294      * @param selectedServerInstanceID preselected instance or null if non is preselected
295      */

296     private void initServers(String JavaDoc selectedServerInstanceID) {
297         // init the list of server instances
298
serversModel.removeAllElements();
299         Set JavaDoc<ServerInstanceWrapper> servers = new TreeSet JavaDoc<ServerInstanceWrapper>();
300         ServerInstanceWrapper selectedItem = null;
301         boolean sjasFound = false;
302         for (String JavaDoc serverInstanceID : Deployment.getDefault().getServerInstanceIDs()) {
303             String JavaDoc displayName = Deployment.getDefault().getServerInstanceDisplayName(serverInstanceID);
304             J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(serverInstanceID);
305             if (displayName != null && j2eePlatform != null && j2eePlatform.getSupportedModuleTypes().contains(J2eeModule.EJB)) {
306                 ServerInstanceWrapper serverWrapper = new ServerInstanceWrapper(serverInstanceID, displayName);
307                 // decide whether this server should be preselected
308
if (selectedItem == null || !sjasFound) {
309                     if (selectedServerInstanceID != null) {
310                         if (selectedServerInstanceID.equals(serverInstanceID)) {
311                             selectedItem = serverWrapper;
312                         }
313                     } else {
314                         // preselect the best server ;)
315
String JavaDoc shortName = Deployment.getDefault().getServerID(serverInstanceID);
316                         if ("J2EE".equals(shortName)) { // NOI18N
317
selectedItem = serverWrapper;
318                             sjasFound = true;
319                         }
320                         else
321                         if ("JBoss4".equals(shortName)) { // NOI18N
322
selectedItem = serverWrapper;
323                         }
324                     }
325                 }
326                 servers.add(serverWrapper);
327             }
328         }
329         for (ServerInstanceWrapper item : servers) {
330             serversModel.addElement(item);
331         }
332         if (selectedItem != null) {
333             // set the preselected item
334
serversModel.setSelectedItem(selectedItem);
335         } else if (serversModel.getSize() > 0) {
336             // set the first item
337
serversModel.setSelectedItem(serversModel.getElementAt(0));
338         }
339     }
340     
341     private String JavaDoc getSelectedJ2eeSpec() {
342         Object JavaDoc item = j2eeSpecComboBox.getSelectedItem();
343         return item == null ? null
344                             : item.equals(J2EE_SPEC_14_LABEL) ? J2eeModule.J2EE_14
345                             : item.equals(J2EE_SPEC_15_LABEL) ? J2eeModule.JAVA_EE_5 : J2eeModule.J2EE_13;
346     }
347     
348     private String JavaDoc getSelectedServer() {
349         ServerInstanceWrapper serverInstanceWrapper = (ServerInstanceWrapper) serversModel.getSelectedItem();
350         if (serverInstanceWrapper == null) {
351             return null;
352         }
353         return serverInstanceWrapper.getServerInstanceID();
354     }
355     
356     private Project getSelectedEarApplication() {
357         int idx = addToAppComboBox.getSelectedIndex();
358         return (idx <= 0) ? null : (Project) earProjects.get(idx - 1);
359     }
360     
361     private void initEnterpriseApplications() {
362         addToAppComboBox.addItem(NbBundle.getMessage(PanelOptionsVisual.class, "LBL_NWP1_AddToEApp_None"));
363         addToAppComboBox.setSelectedIndex(0);
364         
365         Project[] allProjects = OpenProjects.getDefault().getOpenProjects();
366         earProjects = new ArrayList JavaDoc();
367         for (int i = 0; i < allProjects.length; i++) {
368             J2eeModuleContainer container = (J2eeModuleContainer) allProjects[i].getLookup().lookup(J2eeModuleContainer.class);
369             ProjectInformation projectInfo = ProjectUtils.getInformation(allProjects[i]);
370             if (container != null) {
371                 earProjects.add(projectInfo.getProject());
372                 addToAppComboBox.addItem(projectInfo.getDisplayName());
373             }
374         }
375         if (earProjects.size() <= 0) {
376             addToAppComboBox.setEnabled(false);
377         }
378     }
379     
380     private void setJ2eeVersionWarningPanel() {
381         String JavaDoc j2ee = getSelectedJ2eeSpec();
382         if (j2ee == null)
383             return;
384         String JavaDoc warningType = J2eeVersionWarningPanel.findWarningType(j2ee);
385         if (warningType == null && warningPanel == null)
386             return;
387         if (warningPanel == null) {
388             warningPanel = new J2eeVersionWarningPanel(warningType);
389             warningPlaceHolderPanel.add(warningPanel, java.awt.BorderLayout.CENTER);
390             warningPanel.setWarningType(warningType);
391         } else {
392             warningPanel.setWarningType(warningType);
393         }
394     }
395     
396     
397     /**
398      * Server instance wrapper represents server instances in the servers combobox.
399      * @author sherold
400      */

401     private static class ServerInstanceWrapper implements Comparable JavaDoc {
402
403         private final String JavaDoc serverInstanceID;
404         private final String JavaDoc displayName;
405
406         ServerInstanceWrapper(String JavaDoc serverInstanceID, String JavaDoc displayName) {
407             this.serverInstanceID = serverInstanceID;
408             this.displayName = displayName;
409         }
410
411         public String JavaDoc getServerInstanceID() {
412             return serverInstanceID;
413         }
414
415         public String JavaDoc toString() {
416             return displayName;
417         }
418
419         public int compareTo(Object JavaDoc o) {
420             return toString().compareTo(o.toString());
421         }
422     }
423 }
424
425
Popular Tags