KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbfreeform > ui > EJBLocationsPanel


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.ejbfreeform.ui;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FilenameFilter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.math.BigDecimal JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.swing.JFileChooser JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
34 import javax.swing.event.DocumentEvent JavaDoc;
35 import javax.swing.event.DocumentListener JavaDoc;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.api.queries.CollocationQuery;
38 import org.netbeans.modules.ant.freeform.spi.support.Util;
39 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
40 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
41 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
42 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
43 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
44 import org.netbeans.modules.j2ee.ejbfreeform.EJBProjectGenerator;
45 import org.netbeans.modules.j2ee.ejbfreeform.EJBProjectNature;
46 import org.netbeans.modules.j2ee.ejbfreeform.EjbFreeformProperties;
47 import org.openide.ErrorManager;
48 import org.openide.filesystems.FileUtil;
49 import org.openide.util.HelpCtx;
50 import org.openide.util.NbBundle;
51 import org.netbeans.spi.project.AuxiliaryConfiguration;
52 import org.netbeans.spi.project.support.ant.AntProjectHelper;
53 import org.netbeans.spi.project.support.ant.PropertyUtils;
54 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
55 import org.openide.WizardDescriptor;
56 import org.openide.filesystems.FileObject;
57 import org.w3c.dom.Element JavaDoc;
58
59
60 /**
61  *
62  * @author Radko Najman
63  */

64 public class EJBLocationsPanel extends javax.swing.JPanel JavaDoc implements HelpCtx.Provider {
65     
66     /** Original project base folder */
67     private File JavaDoc baseFolder;
68     /** Freeform Project base folder */
69     private File JavaDoc nbProjectFolder;
70     
71     private File JavaDoc srcPackagesLocation;
72     
73     private AntProjectHelper projectHelper;
74     
75     private java.util.List JavaDoc serverIDs;
76     private ChangeListener JavaDoc listener;
77     private DocumentListener JavaDoc documentListener;
78     
79     private BigDecimal JavaDoc ejbJarXmlVersion;
80     
81     private boolean isNew = false;
82     private boolean isNS2 = false;
83     private boolean is15 = false;
84     
85     private boolean updatingLevels = false;
86     
87     private static final String JavaDoc J2EE_SPEC_15_LABEL = NbBundle.getMessage(EJBLocationsPanel.class, "TXT_J2EESpecLevel_2"); //NOI18N
88
private static final String JavaDoc J2EE_SPEC_14_LABEL = NbBundle.getMessage(EJBLocationsPanel.class, "TXT_J2EESpecLevel_0"); //NOI18N
89
private static final String JavaDoc J2EE_SPEC_13_LABEL = NbBundle.getMessage(EJBLocationsPanel.class, "TXT_J2EESpecLevel_1"); //NOI18N
90

91     /** Creates new form EJBLocations */
92     public EJBLocationsPanel(EJBLocationsWizardPanel panel) {
93         this.listener = panel;
94         initComponents();
95         
96         isNew = true;
97         initServerInstances();
98         
99         documentListener = new DocumentListener JavaDoc() {
100             public void insertUpdate(DocumentEvent JavaDoc e) {
101                 update(e);
102             }
103             public void removeUpdate(DocumentEvent JavaDoc e) {
104                 update(e);
105             }
106             public void changedUpdate(DocumentEvent JavaDoc e) {
107                 update(e);
108             }
109         };
110         
111         this.jTextFieldConfigFiles.getDocument().addDocumentListener(documentListener);
112     }
113     
114     public EJBLocationsPanel(EJBLocationsWizardPanel panel, Project project, AntProjectHelper projectHelper, PropertyEvaluator projectEvaluator, AuxiliaryConfiguration aux) {
115         this(panel);
116         this.projectHelper = projectHelper;
117         setFolders(Util.getProjectLocation(projectHelper, projectEvaluator), FileUtil.toFile(projectHelper.getProjectDirectory()));
118         
119         Element JavaDoc ejbData = aux.getConfigurationFragment(EJBProjectNature.EL_EJB, EJBProjectNature.NS_EJB_2, true);
120         if (ejbData != null) {
121             isNS2 = true;
122         }
123                 
124         List JavaDoc ejbModules = EJBProjectGenerator.getEJBmodules(projectHelper, aux);
125         if (ejbModules != null) {
126             EJBProjectGenerator.EJBModule em = (EJBProjectGenerator.EJBModule) ejbModules.get(0);
127             is15 = em.j2eeSpecLevel.equals("1.5") ? true : false;
128         }
129
130         isNew = false;
131         updateJ2eeLevels();
132         
133         // remove Generic server for 1.5 projects from combo
134
if (is15) {
135             serverIDs.remove("GENERIC"); // NOI18N
136
serverTypeComboBox.removeItem(Deployment.getDefault().getServerDisplayName("GENERIC")); // NOI18N
137
}
138         
139         if (ejbModules != null) {
140             EJBProjectGenerator.EJBModule wm = (EJBProjectGenerator.EJBModule)ejbModules.get(0);
141             String JavaDoc configFiles = getLocationDisplayName(projectEvaluator, nbProjectFolder, wm.configFiles);
142             String JavaDoc classpath = getLocationDisplayName(projectEvaluator, nbProjectFolder, wm.classpath);
143             String JavaDoc resourceFiles = getLocationDisplayName(projectEvaluator, nbProjectFolder, projectEvaluator.getProperty(EjbFreeformProperties.RESOURCE_DIR));
144             String JavaDoc serverID = projectEvaluator.getProperty(EjbFreeformProperties.J2EE_SERVER_TYPE);
145             jTextFieldConfigFiles.setText(configFiles);
146             resourcesTextField.setText(resourceFiles);
147             
148             setSrcPackages(classpath);
149             if (wm.j2eeSpecLevel.equals("1.5")) {
150                 j2eeSpecComboBox.setSelectedItem(NbBundle.getMessage(EJBLocationsPanel.class, "TXT_J2EESpecLevel_2"));
151             } else if (wm.j2eeSpecLevel.equals("1.4")) {
152                 j2eeSpecComboBox.setSelectedItem(NbBundle.getMessage(EJBLocationsPanel.class, "TXT_J2EESpecLevel_0"));
153             } else {
154                 j2eeSpecComboBox.setSelectedItem(NbBundle.getMessage(EJBLocationsPanel.class, "TXT_J2EESpecLevel_1"));
155             }
156             
157             if (serverID != null)
158                 selectServerID(serverID);
159         }
160
161     }
162     
163     private void updateJ2eeLevels() {
164         String JavaDoc prevSelectedItem = (String JavaDoc)j2eeSpecComboBox.getSelectedItem();
165         int selectedServerIndex = serverTypeComboBox.getSelectedIndex();
166         if (selectedServerIndex == -1) {
167             return;
168         }
169         String JavaDoc serverID = (String JavaDoc) serverIDs.get(selectedServerIndex);
170         String JavaDoc servInsID = getFirstServerInstanceID(serverID);
171         Set JavaDoc supportedVersions;
172         if (servInsID != null) {
173             J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(servInsID);
174             supportedVersions = j2eePlatform.getSupportedSpecVersions();
175         } else {
176             supportedVersions = new HashSet JavaDoc();
177             supportedVersions.add(J2eeModule.J2EE_13);
178             supportedVersions.add(J2eeModule.J2EE_14);
179         }
180         updatingLevels = true;
181         j2eeSpecComboBox.removeAllItems();
182         
183         if (supportedVersions.contains(J2eeModule.JAVA_EE_5) && (isNew || isNS2)) {
184             j2eeSpecComboBox.addItem(J2EE_SPEC_15_LABEL);
185         }
186         if (supportedVersions.contains(J2eeModule.J2EE_14) && !is15) {
187             j2eeSpecComboBox.addItem(J2EE_SPEC_14_LABEL);
188         }
189         if (supportedVersions.contains(J2eeModule.J2EE_13) && !is15) {
190             j2eeSpecComboBox.addItem(J2EE_SPEC_13_LABEL);
191         }
192         if (prevSelectedItem != null) {
193             j2eeSpecComboBox.setSelectedItem(prevSelectedItem);
194         }
195         updatingLevels = false;
196         if (listener != null) {
197             listener.stateChanged(null);
198         }
199     }
200     
201     private void update(DocumentEvent JavaDoc e) {
202         setEjbJarXmlJ2eeVersion(findEbjJarXml(getAsFile(jTextFieldConfigFiles.getText())));
203         if (listener != null) {
204             listener.stateChanged(null);
205         }
206     }
207
208     /**
209      * Convert given string value (e.g. "${project.dir}/src" to a file
210      * and try to relativize it.
211      */

212     // XXX: copied from java/freeform:SourceFoldersPanel.getLocationDisplayName
213
public static String JavaDoc getLocationDisplayName(PropertyEvaluator evaluator, File JavaDoc base, String JavaDoc val) {
214         if (val == null || val.trim().length() <= 0)
215             return ""; // NOI18N
216
File JavaDoc f = Util.resolveFile(evaluator, base, val);
217         if (f == null) {
218             return val;
219         }
220         String JavaDoc location = f.getAbsolutePath();
221         if (CollocationQuery.areCollocated(base, f)) {
222             location = PropertyUtils.relativizeFile(base, f).replace('/', File.separatorChar); // NOI18N
223
}
224         return location;
225     }
226     
227     
228     public HelpCtx getHelpCtx() {
229         return new HelpCtx( EJBLocationsPanel.class );
230     }
231     
232     /** This method is called from within the constructor to
233      * initialize the form.
234      * WARNING: Do NOT modify this code. The content of this method is
235      * always regenerated by the Form Editor.
236      */

237     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
238
private void initComponents() {
239         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
240
241         jLabel1 = new javax.swing.JLabel JavaDoc();
242         jLabel2 = new javax.swing.JLabel JavaDoc();
243         jTextFieldConfigFiles = new javax.swing.JTextField JavaDoc();
244         jButtonEJB = new javax.swing.JButton JavaDoc();
245         jLabel5 = new javax.swing.JLabel JavaDoc();
246         j2eeSpecComboBox = new javax.swing.JComboBox JavaDoc();
247         jLabel3 = new javax.swing.JLabel JavaDoc();
248         serverTypeComboBox = new javax.swing.JComboBox JavaDoc();
249         jLabel6 = new javax.swing.JLabel JavaDoc();
250         resourcesTextField = new javax.swing.JTextField JavaDoc();
251         jButton1 = new javax.swing.JButton JavaDoc();
252         jPanel1 = new javax.swing.JPanel JavaDoc();
253         warningLabel = new javax.swing.JLabel JavaDoc();
254
255         setLayout(new java.awt.GridBagLayout JavaDoc());
256
257         setPreferredSize(new java.awt.Dimension JavaDoc(375, 135));
258         jLabel1.setText(org.openide.util.NbBundle.getMessage(EJBLocationsPanel.class, "LBL_ConfigFilesPanel_Description")); // NOI18N
259
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
260         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
261         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
262         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
263         add(jLabel1, gridBagConstraints);
264
265         jLabel2.setLabelFor(jTextFieldConfigFiles);
266         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(EJBLocationsPanel.class, "LBL_ConfigFilesPanel_ConfigFilesLocation_Label")); // NOI18N
267
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
268         gridBagConstraints.gridx = 0;
269         gridBagConstraints.gridy = 1;
270         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
271         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 11);
272         add(jLabel2, gridBagConstraints);
273         jLabel2.getAccessibleContext().setAccessibleName(null);
274
275         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
276         gridBagConstraints.gridx = 1;
277         gridBagConstraints.gridy = 1;
278         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
279         gridBagConstraints.weightx = 1.0;
280         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 11);
281         add(jTextFieldConfigFiles, gridBagConstraints);
282         jTextFieldConfigFiles.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(EJBLocationsPanel.class, "ACS_LBL_ConfigFilesPanel_ConfigFilesLocation_A11YDesc")); // NOI18N
283

284         org.openide.awt.Mnemonics.setLocalizedText(jButtonEJB, org.openide.util.NbBundle.getMessage(EJBLocationsPanel.class, "BTN_BasicProjectInfoPanel_browseAntScript")); // NOI18N
285
jButtonEJB.addActionListener(new java.awt.event.ActionListener JavaDoc() {
286             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
287                 jButtonEJBActionPerformed(evt);
288             }
289         });
290
291         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
292         gridBagConstraints.gridx = 2;
293         gridBagConstraints.gridy = 1;
294         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
295         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
296         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
297         add(jButtonEJB, gridBagConstraints);
298         jButtonEJB.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(EJBLocationsPanel.class, "ACS_LBL_ConfigFilesPanel_ConfigFilesLocationBrowse_A11YDesc")); // NOI18N
299

300         jLabel5.setLabelFor(j2eeSpecComboBox);
301         org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(EJBLocationsPanel.class, "LBL_ConfigFilesPanel_J2EESpecLevel_Label")); // NOI18N
302
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
303         gridBagConstraints.gridx = 0;
304         gridBagConstraints.gridy = 4;
305         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
306         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 11);
307         add(jLabel5, gridBagConstraints);
308
309         j2eeSpecComboBox.addActionListener(new java.awt.event.ActionListener JavaDoc() {
310             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
311                 j2eeSpecComboBoxActionPerformed(evt);
312             }
313         });
314
315         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
316         gridBagConstraints.gridx = 1;
317         gridBagConstraints.gridy = 4;
318         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
319         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
320         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 11);
321         add(j2eeSpecComboBox, gridBagConstraints);
322         j2eeSpecComboBox.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(EJBLocationsPanel.class, "ACS_LBL_ConfigFilesPanel_J2EESpecLevel_A11YDesc")); // NOI18N
323

324         jLabel3.setLabelFor(serverTypeComboBox);
325         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(EJBLocationsPanel.class, "LBL_ConfigFilesPanel_ServerType_Label")); // NOI18N
326
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
327         gridBagConstraints.gridx = 0;
328         gridBagConstraints.gridy = 3;
329         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
330         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 11);
331         add(jLabel3, gridBagConstraints);
332
333         serverTypeComboBox.addItemListener(new java.awt.event.ItemListener JavaDoc() {
334             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
335                 serverTypeComboBoxItemStateChanged(evt);
336             }
337         });
338
339         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
340         gridBagConstraints.gridx = 1;
341         gridBagConstraints.gridy = 3;
342         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
343         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
344         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 11);
345         add(serverTypeComboBox, gridBagConstraints);
346         serverTypeComboBox.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(EJBLocationsPanel.class, "ACS_LBL_ConfigFilesPanel_ServerType_A11YDesc")); // NOI18N
347

348         jLabel6.setLabelFor(resourcesTextField);
349         org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(EJBLocationsPanel.class, "LBL_ConfigFilesPanel_Resources_label")); // NOI18N
350
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
351         gridBagConstraints.gridx = 0;
352         gridBagConstraints.gridy = 2;
353         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
354         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 11);
355         add(jLabel6, gridBagConstraints);
356
357         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
358         gridBagConstraints.gridx = 1;
359         gridBagConstraints.gridy = 2;
360         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
361         gridBagConstraints.weightx = 1.0;
362         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 11);
363         add(resourcesTextField, gridBagConstraints);
364         resourcesTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(EJBLocationsPanel.class, "ACS_LBL_ConfigFilesPanel_ResourceFolder_A11YDesc")); // NOI18N
365

366         org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(EJBLocationsPanel.class, "BTN_ConfigFilesPanel_ResourcesBrowse")); // NOI18N
367
jButton1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
368             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
369                 jButton1ActionPerformed(evt);
370             }
371         });
372
373         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
374         gridBagConstraints.gridx = 2;
375         gridBagConstraints.gridy = 2;
376         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
377         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
378         add(jButton1, gridBagConstraints);
379         jButton1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(EJBLocationsPanel.class, "ACS_LBL_ConfigFilesPanel_ResourceFolderBrowse_A11YDesc")); // NOI18N
380

381         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
382         gridBagConstraints.gridx = 0;
383         gridBagConstraints.gridy = 5;
384         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
385         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
386         gridBagConstraints.weightx = 1.0;
387         gridBagConstraints.weighty = 1.0;
388         add(jPanel1, gridBagConstraints);
389
390         warningLabel.setForeground(new java.awt.Color JavaDoc(89, 71, 191));
391         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
392         gridBagConstraints.gridx = 0;
393         gridBagConstraints.gridy = 6;
394         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
395         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
396         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
397         gridBagConstraints.weightx = 1.0;
398         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 0, 0, 0);
399         add(warningLabel, gridBagConstraints);
400
401     }// </editor-fold>//GEN-END:initComponents
402

403     private void j2eeSpecComboBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_j2eeSpecComboBoxActionPerformed
404
String JavaDoc selectedItem = (String JavaDoc)j2eeSpecComboBox.getSelectedItem();
405         String JavaDoc warningMessage = null;
406         
407         if (!updatingLevels && listener != null) {
408             listener.stateChanged(null);
409         }
410         
411         if (J2EE_SPEC_14_LABEL.equals(selectedItem) && new BigDecimal JavaDoc(EjbJar.VERSION_2_0).equals(ejbJarXmlVersion)) {
412             warningMessage = NbBundle.getMessage(EJBLocationsPanel.class, "MSG_EjbJarXmlUpdate");
413         }
414         
415         warningLabel.setText(warningMessage);
416     }//GEN-LAST:event_j2eeSpecComboBoxActionPerformed
417

418     private void serverTypeComboBoxItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_serverTypeComboBoxItemStateChanged
419
updateJ2eeLevels();
420     }//GEN-LAST:event_serverTypeComboBoxItemStateChanged
421

422     private void jButton1ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButton1ActionPerformed
423
File JavaDoc resources = getResourcesLocation();
424         JFileChooser JavaDoc chooser = createChooser(resources != null ? resources.getAbsolutePath() : baseFolder.getAbsolutePath()); // NOI18N
425
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
426             setResources(chooser.getSelectedFile());
427         }
428     }//GEN-LAST:event_jButton1ActionPerformed
429

430     private void jButtonEJBActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonEJBActionPerformed
431
JFileChooser JavaDoc chooser = createChooser(getConfigFilesLocation().getAbsolutePath());
432         if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
433             setConfigFiles(chooser.getSelectedFile());
434         }
435     }//GEN-LAST:event_jButtonEJBActionPerformed
436

437     // Variables declaration - do not modify//GEN-BEGIN:variables
438
private javax.swing.JComboBox JavaDoc j2eeSpecComboBox;
439     private javax.swing.JButton JavaDoc jButton1;
440     private javax.swing.JButton JavaDoc jButtonEJB;
441     private javax.swing.JLabel JavaDoc jLabel1;
442     private javax.swing.JLabel JavaDoc jLabel2;
443     private javax.swing.JLabel JavaDoc jLabel3;
444     private javax.swing.JLabel JavaDoc jLabel5;
445     private javax.swing.JLabel JavaDoc jLabel6;
446     private javax.swing.JPanel JavaDoc jPanel1;
447     private javax.swing.JTextField JavaDoc jTextFieldConfigFiles;
448     private javax.swing.JTextField JavaDoc resourcesTextField;
449     private javax.swing.JComboBox JavaDoc serverTypeComboBox;
450     private javax.swing.JLabel JavaDoc warningLabel;
451     // End of variables declaration//GEN-END:variables
452

453     private static JFileChooser JavaDoc createChooser(String JavaDoc path) {
454         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
455         FileUtil.preventFileChooserSymlinkTraversal(chooser, new File JavaDoc(path));
456         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
457         chooser.setAcceptAllFileFilterUsed(false);
458         
459         return chooser;
460     }
461     
462     protected List JavaDoc getEJBModules() {
463         ArrayList JavaDoc l = new ArrayList JavaDoc();
464         
465         EJBProjectGenerator.EJBModule ejbModule = new EJBProjectGenerator.EJBModule();
466         ejbModule.configFiles = getRelativeLocation(getConfigFilesLocation());
467         
468         String JavaDoc j2eeLevel = (String JavaDoc) j2eeSpecComboBox.getSelectedItem();
469         if (j2eeLevel != null) {
470             if (j2eeLevel.equals(NbBundle.getMessage(EJBLocationsPanel.class, "TXT_J2EESpecLevel_2"))) {
471                 ejbModule.j2eeSpecLevel = "1.5";
472             } else if (j2eeLevel.equals(NbBundle.getMessage(EJBLocationsPanel.class, "TXT_J2EESpecLevel_0"))) {
473                 ejbModule.j2eeSpecLevel = "1.4";
474             } else {
475                 ejbModule.j2eeSpecLevel = "1.3";
476             }
477         }
478         // ejbModule.classpath = getRelativeLocation(getSrcPackagesLocation());
479
// XXX since the contents of the classpath element is set to the (sometimes wrongly)
480
// detected Java source root, better to set it to null for now and let the
481
// Java nature provide the classpath (see EJBModules and issue 67651)
482
ejbModule.classpath = ""; // NOI18N
483
l.add(ejbModule);
484         
485         return l;
486     }
487     
488     protected List JavaDoc getJavaSrcFolder() {
489         ArrayList JavaDoc l = new ArrayList JavaDoc();
490         File JavaDoc sourceLoc = getSrcPackagesLocation();
491         l.add(getRelativeLocation(sourceLoc));
492         l.add(sourceLoc.getName());
493         return l;
494     }
495     
496     /**
497      * @return list of pairs [relative path, display name]
498      */

499     protected List JavaDoc getEJBSrcFolder() {
500         ArrayList JavaDoc l = new ArrayList JavaDoc();
501         final File JavaDoc webLocation = getConfigFilesLocation();
502         l.add(getRelativeLocation(webLocation));
503         l.add(webLocation.getName());
504         return l;
505     }
506     
507     protected List JavaDoc getResourcesFolder() {
508         ArrayList JavaDoc l = new ArrayList JavaDoc();
509         File JavaDoc resourceLoc = getResourcesLocation();
510         l.add(resourceLoc != null ? getRelativeLocation(resourceLoc) : null);
511         l.add(resourceLoc != null ? resourceLoc.getName() : null);
512         return l;
513     }
514     
515     private File JavaDoc getAsFile(String JavaDoc filename) {
516         final File JavaDoc f = new File JavaDoc(filename.trim());
517         return PropertyUtils.resolveFile(nbProjectFolder, filename);
518     }
519     
520     /** Called from WizardDescriptor.Panel and ProjectCustomizer.Panel
521      * to set base folder. Panel will use this for default position of JFileChooser.
522      * @param baseFolder original project base folder
523      * @param nbProjectFolder Freeform Project base folder
524      */

525     public void setFolders(File JavaDoc baseFolder, File JavaDoc nbProjectFolder) {
526         this.baseFolder = baseFolder;
527         this.nbProjectFolder = nbProjectFolder;
528     }
529     
530     protected void setConfigFilesField(String JavaDoc path) {
531         if (path != null && !path.equals("")) {
532             setConfigFiles(new File JavaDoc(path));
533         } else {
534             setConfigFiles(path);
535         }
536     }
537     
538     protected void setConfigFiles(String JavaDoc path) {
539         jTextFieldConfigFiles.setText(path);
540     }
541     
542     protected void setSrcPackages(String JavaDoc path) {
543         setSrcPackages(getAsFile(path));
544     }
545     
546     private void setConfigFiles(final File JavaDoc file) {
547         setConfigFiles(relativizeFile(file));
548     }
549     
550     private void setResources(final File JavaDoc file) {
551         resourcesTextField.setText(relativizeFile(file));
552     }
553     
554     protected File JavaDoc getConfigFilesLocation() {
555         return getAsFile(jTextFieldConfigFiles.getText()).getAbsoluteFile();
556     }
557     
558     protected File JavaDoc getResourcesLocation() {
559         String JavaDoc resources = resourcesTextField.getText().trim();
560         return resources.length() <= 0 ? null : getAsFile(resourcesTextField.getText()).getAbsoluteFile();
561     }
562     
563     private void setSrcPackages(final File JavaDoc file) {
564         srcPackagesLocation = file;
565     }
566     
567     protected File JavaDoc getSrcPackagesLocation() {
568         return srcPackagesLocation;
569     }
570     
571     private String JavaDoc relativizeFile(final File JavaDoc file) {
572         File JavaDoc normalizedFile = FileUtil.normalizeFile(file);
573         if (CollocationQuery.areCollocated(nbProjectFolder, file)) {
574             return PropertyUtils.relativizeFile(nbProjectFolder, normalizedFile);
575         } else {
576             return normalizedFile.getAbsolutePath();
577         }
578     }
579     
580     private String JavaDoc getRelativeLocation(final File JavaDoc location) {
581         final File JavaDoc normalizedLocation = FileUtil.normalizeFile(location);
582         return Util.relativizeLocation(baseFolder, nbProjectFolder, normalizedLocation);
583     }
584     
585     ActionListener JavaDoc getCustomizerOkListener() {
586         return new ActionListener JavaDoc() {
587             public void actionPerformed(ActionEvent JavaDoc arg0) {
588                 
589                 AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(projectHelper);
590                 EJBProjectGenerator.putEJBModules(projectHelper, aux, getEJBModules());
591                 EJBProjectGenerator.putServerID(projectHelper, getSelectedServerID());
592                 EJBProjectGenerator.putResourceFolder(projectHelper, getResourcesFolder());
593                 
594                 String JavaDoc j2eeLevel = ((EJBProjectGenerator.EJBModule)getEJBModules().get(0)).j2eeSpecLevel;
595                 EJBProjectGenerator.putJ2EELevel(projectHelper, j2eeLevel);
596                 
597                 // update the DD to 2.1 if it is 2.0 and the user switched to J2EE 1.4
598
FileObject ejbJarXml = findEbjJarXml(getConfigFilesLocation());
599                 try {
600                     if (j2eeLevel.equals("1.4") && !new BigDecimal JavaDoc(EjbJar.VERSION_2_1).equals(getEjbJarXmlVersion(ejbJarXml))) { // NOI18N
601
EjbJar root = DDProvider.getDefault().getDDRoot(ejbJarXml);
602                         root.setVersion(new BigDecimal JavaDoc(EjbJar.VERSION_2_1));
603                         root.write(ejbJarXml);
604                     }
605                 } catch (IOException JavaDoc e) {
606                     final ErrorManager errorManager = ErrorManager.getDefault();
607                     String JavaDoc message = NbBundle.getMessage(EJBLocationsPanel.class, "MSG_EjbJarXmlCorrupted");
608                     errorManager.notify(errorManager.annotate(e, message));
609                 }
610             }
611         };
612     }
613         
614     
615     private void initServerInstances() {
616         String JavaDoc[] servInstIDs = Deployment.getDefault().getServerInstanceIDs();
617         serverIDs = new ArrayList JavaDoc();
618         for (int i = 0; i < servInstIDs.length; i++) {
619             J2eePlatform j2eePlat = Deployment.getDefault().getJ2eePlatform(servInstIDs[i]);
620             String JavaDoc serverID = Deployment.getDefault().getServerID(servInstIDs[i]);
621             String JavaDoc servDisplayName = Deployment.getDefault().getServerDisplayName(serverID);
622             if (servDisplayName != null && !serverIDs.contains(serverID)
623                 && j2eePlat != null && j2eePlat.getSupportedModuleTypes().contains(J2eeModule.EJB)) {
624                 serverIDs.add(serverID);
625                 serverTypeComboBox.addItem(servDisplayName);
626             }
627         }
628         serverIDs.add("GENERIC"); // NOI18N
629
serverTypeComboBox.addItem(Deployment.getDefault().getServerDisplayName("GENERIC")); // NOI18N
630
if (serverIDs.size() > 0) {
631             serverTypeComboBox.setSelectedIndex(0);
632         } else {
633             serverTypeComboBox.setEnabled(false);
634             j2eeSpecComboBox.setEnabled(false);
635         }
636     }
637
638     /**
639      * Returns ID of first found instance of server with given server ID,
640      * @param serverID ID of server
641      * @return ID of server instance
642      */

643     private String JavaDoc getFirstServerInstanceID(String JavaDoc serverID) {
644         if (serverID == null) {
645             return null;
646         }
647         String JavaDoc[] servInstIDs = Deployment.getDefault().getServerInstanceIDs();
648         for (int i = 0; i < servInstIDs.length; i++) {
649             if (serverID == Deployment.getDefault().getServerID(servInstIDs[i])) {
650                 return (String JavaDoc) servInstIDs[i];
651             }
652         }
653         return null;
654     }
655
656     public String JavaDoc getSelectedServerID() {
657         int idx = serverTypeComboBox.getSelectedIndex();
658         if (idx == -1) {
659             return null;
660         }
661         String JavaDoc serverID = (String JavaDoc) serverIDs.get(idx);
662         return serverID;
663     }
664     
665     private void selectServerID(String JavaDoc serverID) {
666         for (int i = 0; i < serverIDs.size(); i++) {
667             if (serverID.equals(serverIDs.get(i))) {
668                 serverTypeComboBox.setSelectedIndex(i);
669                 break;
670             }
671         }
672     }
673     
674     public String JavaDoc getSelectedJ2eeSpec() {
675         Object JavaDoc item = j2eeSpecComboBox.getSelectedItem();
676         if (item == null) {
677             return null;
678         } else {
679             if (item.equals(J2EE_SPEC_15_LABEL)) {
680                 return J2eeModule.JAVA_EE_5;
681             } else if (item.equals(J2EE_SPEC_14_LABEL)) {
682                 return J2eeModule.J2EE_14;
683             } else {
684                 return J2eeModule.J2EE_13;
685             }
686         }
687     }
688     
689     public boolean valid(WizardDescriptor wizardDescriptor) {
690         File JavaDoc cfLoc = getConfigFilesLocation();
691         if (jTextFieldConfigFiles.getText().equals("") || !cfLoc.isDirectory()) {
692             wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(EJBLocationsPanel.class,"MSG_NoConfFolder")); // NOI18N
693
return false;
694         }
695         
696         if (is15 && getSelectedServerID().equals("GENERIC")) {
697             wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(EJBLocationsPanel.class,"MSG_NoGENERICServer")); // NOI18N
698
return false;
699         }
700         
701         File JavaDoc[] dds = getConfigFilesLocation().listFiles(new FilenameFilter JavaDoc() {
702             public boolean accept(File JavaDoc dir, String JavaDoc name) {
703                 return name.equals("ejb-jar.xml");
704             }
705         });
706         
707         if (dds.length == 0 && !getSelectedJ2eeSpec().equals(J2eeModule.JAVA_EE_5)) {
708             wizardDescriptor.putProperty("WizardPanel_errorMessage", NbBundle.getMessage(EJBLocationsPanel.class,"MSG_NoEjbJarXml")); //NOI18N
709
return false;
710         }
711
712         wizardDescriptor.putProperty("WizardPanel_errorMessage", ""); //NOI18N
713
return true;
714     }
715     
716     public static FileObject findEbjJarXml(File JavaDoc configFolder) {
717         FileObject confFolderFo = FileUtil.toFileObject(configFolder);
718         FileObject ejbJarXml = null;
719         if (confFolderFo != null) {
720              ejbJarXml = confFolderFo.getFileObject("ejb-jar.xml"); // NOI18N
721
}
722         return ejbJarXml;
723     }
724     
725     public static BigDecimal JavaDoc getEjbJarXmlVersion(FileObject ejbJarXml) throws IOException JavaDoc {
726         if (ejbJarXml != null) {
727             return DDProvider.getDefault().getDDRoot(ejbJarXml).getVersion();
728         } else {
729             return null;
730         }
731     }
732     
733     private void setEjbJarXmlJ2eeVersion(FileObject ejbJarXml) {
734         try {
735             BigDecimal JavaDoc version = getEjbJarXmlVersion(ejbJarXml);
736             ejbJarXmlVersion = version;
737             if (version == null)
738                 return;
739             
740             if (new BigDecimal JavaDoc(EjbJar.VERSION_2_0).equals(version)) {
741                 j2eeSpecComboBox.setSelectedItem(J2EE_SPEC_13_LABEL);
742             } else if (new BigDecimal JavaDoc(EjbJar.VERSION_2_1).equals(version)) {
743                 j2eeSpecComboBox.setSelectedItem(J2EE_SPEC_14_LABEL);
744             } else if (new BigDecimal JavaDoc(EjbJar.VERSION_3_0).equals(version)) {
745                 j2eeSpecComboBox.setSelectedItem(J2EE_SPEC_15_LABEL);
746             }
747             
748         } catch (IOException JavaDoc e) {
749             final ErrorManager errorManager = ErrorManager.getDefault();
750             String JavaDoc message = NbBundle.getMessage(EJBLocationsPanel.class, "MSG_EjbJarXmlCorrupted");
751             errorManager.notify(errorManager.annotate(e, message));
752         }
753     }
754 }
755
Popular Tags