KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > freeform > ui > WebLocationsPanel


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.web.freeform.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.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.JFileChooser JavaDoc;
28 import org.netbeans.api.queries.CollocationQuery;
29 import org.netbeans.modules.ant.freeform.spi.support.Util;
30 import org.netbeans.modules.web.freeform.WebProjectGenerator;
31 import org.openide.filesystems.FileUtil;
32 import org.openide.util.HelpCtx;
33 import org.openide.util.NbBundle;
34 import org.netbeans.spi.project.AuxiliaryConfiguration;
35 import org.netbeans.spi.project.support.ant.AntProjectHelper;
36 import org.netbeans.spi.project.support.ant.PropertyUtils;
37 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
38 import org.netbeans.spi.project.ui.support.ProjectChooser;
39 import org.netbeans.spi.project.ui.templates.support.Templates;
40 import org.openide.WizardDescriptor;
41 import org.openide.filesystems.FileObject;
42
43
44 /**
45  *
46  * @author Radko Najman
47  */

48 public class WebLocationsPanel extends javax.swing.JPanel JavaDoc implements HelpCtx.Provider {
49     
50     private static String JavaDoc J2EE_SPEC_5 = "1.5"; //NOI18N
51
private static String JavaDoc J2EE_SPEC_1_4 = "1.4"; //NOI18N
52
private static String JavaDoc J2EE_SPEC_1_3 = "1.3"; //NOI18N
53

54     /** Original project base folder */
55     private File JavaDoc baseFolder;
56     /** Freeform Project base folder */
57     private File JavaDoc nbProjectFolder;
58
59     private AntProjectHelper projectHelper;
60     
61     private File JavaDoc srcPackagesLocation;
62     private String JavaDoc classpath;
63     
64     private WizardDescriptor wizardDescriptor;
65     
66     /** Creates new form WebLocations */
67     public WebLocationsPanel(WizardDescriptor wizardDescriptor) {
68         initComponents();
69         this.wizardDescriptor = wizardDescriptor;
70         jComboBoxJ2eeLevel.addItem(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_5")); //NOI18N
71
jComboBoxJ2eeLevel.addItem(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_0")); //NOI18N
72
jComboBoxJ2eeLevel.addItem(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_1")); //NOI18N
73
jComboBoxJ2eeLevel.setSelectedIndex(0);
74     }
75     
76     public WebLocationsPanel(AntProjectHelper projectHelper, PropertyEvaluator projectEvaluator, AuxiliaryConfiguration aux) {
77         this(null);
78         this.projectHelper = projectHelper;
79         setFolders(Util.getProjectLocation(projectHelper, projectEvaluator), FileUtil.toFile(projectHelper.getProjectDirectory()));
80         
81         List JavaDoc l = WebProjectGenerator.getWebmodules(projectHelper, aux);
82         if (l != null) {
83             WebProjectGenerator.WebModule wm = (WebProjectGenerator.WebModule)l.get(0);
84             String JavaDoc docroot = getLocationDisplayName(projectEvaluator, nbProjectFolder, wm.docRoot);
85             classpath = wm.classpath;
86             jTextFieldWeb.setText(docroot);
87             
88             jTextFieldContextPath.setText(wm.contextPath);
89             if (wm.j2eeSpecLevel.equals(J2EE_SPEC_5))
90                 jComboBoxJ2eeLevel.setSelectedItem(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_5"));
91             else if (wm.j2eeSpecLevel.equals(J2EE_SPEC_1_4))
92                 jComboBoxJ2eeLevel.setSelectedItem(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_0"));
93             else
94                 jComboBoxJ2eeLevel.setSelectedItem(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_1"));
95         }
96     }
97
98     /**
99      * Convert given string value (e.g. "${project.dir}/src" to a file
100      * and try to relativize it.
101      */

102     // XXX: copied from java/freeform:SourceFoldersPanel.getLocationDisplayName
103
public static String JavaDoc getLocationDisplayName(PropertyEvaluator evaluator, File JavaDoc base, String JavaDoc val) {
104         File JavaDoc f = Util.resolveFile(evaluator, base, val);
105         if (f == null) {
106             return val;
107         }
108         String JavaDoc location = f.getAbsolutePath();
109         if (CollocationQuery.areCollocated(base, f)) {
110             location = PropertyUtils.relativizeFile(base, f).replace('/', File.separatorChar); // NOI18N
111
}
112         return location;
113     }
114     
115     public HelpCtx getHelpCtx() {
116         return new HelpCtx( WebLocationsPanel.class );
117     }
118     
119     /** This method is called from within the constructor to
120      * initialize the form.
121      * WARNING: Do NOT modify this code. The content of this method is
122      * always regenerated by the Form Editor.
123      */

124     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
125
private void initComponents() {
126         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
127
128         jLabel1 = new javax.swing.JLabel JavaDoc();
129         jLabel2 = new javax.swing.JLabel JavaDoc();
130         jTextFieldWeb = new javax.swing.JTextField JavaDoc();
131         jButtonWeb = new javax.swing.JButton JavaDoc();
132         jLabel4 = new javax.swing.JLabel JavaDoc();
133         jTextFieldContextPath = new javax.swing.JTextField JavaDoc();
134         jLabel5 = new javax.swing.JLabel JavaDoc();
135         jComboBoxJ2eeLevel = new javax.swing.JComboBox JavaDoc();
136
137         setLayout(new java.awt.GridBagLayout JavaDoc());
138
139         jLabel1.setText(org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "LBL_WebPagesPanel_Description"));
140         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
141         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
142         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
143         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
144         add(jLabel1, gridBagConstraints);
145
146         jLabel2.setLabelFor(jTextFieldWeb);
147         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "LBL_WebPagesPanel_WebPagesLocation_Label"));
148         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
149         gridBagConstraints.gridx = 0;
150         gridBagConstraints.gridy = 1;
151         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
152         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 11);
153         add(jLabel2, gridBagConstraints);
154
155         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
156         gridBagConstraints.gridx = 1;
157         gridBagConstraints.gridy = 1;
158         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
159         gridBagConstraints.weightx = 1.0;
160         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 11);
161         add(jTextFieldWeb, gridBagConstraints);
162         jTextFieldWeb.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "ACS_LBL_WebPagesPanel_WebPagesLocation_A11YDesc"));
163
164         org.openide.awt.Mnemonics.setLocalizedText(jButtonWeb, org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "BTN_BasicProjectInfoPanel_browseAntScript"));
165         jButtonWeb.addActionListener(new java.awt.event.ActionListener JavaDoc() {
166             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
167                 jButtonWebActionPerformed(evt);
168             }
169         });
170
171         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
172         gridBagConstraints.gridx = 2;
173         gridBagConstraints.gridy = 1;
174         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
175         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
176         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
177         add(jButtonWeb, gridBagConstraints);
178         jButtonWeb.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "ACS_LBL_WebPagesPanel_WebPagesLocationBrowse_A11YDesc"));
179
180         jLabel4.setLabelFor(jTextFieldContextPath);
181         org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "LBL_WebPagesPanel_ContextPath_Label"));
182         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
183         gridBagConstraints.gridx = 0;
184         gridBagConstraints.gridy = 3;
185         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
186         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 11);
187         add(jLabel4, gridBagConstraints);
188
189         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
190         gridBagConstraints.gridx = 1;
191         gridBagConstraints.gridy = 3;
192         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
193         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
194         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
195         add(jTextFieldContextPath, gridBagConstraints);
196         jTextFieldContextPath.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "ACS_LBL_WebPagesPanel_ContextPath_A11YDesc"));
197
198         jLabel5.setLabelFor(jComboBoxJ2eeLevel);
199         org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "LBL_WebPagesPanel_J2EESpecLevel_Label"));
200         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
201         gridBagConstraints.gridx = 0;
202         gridBagConstraints.gridy = 4;
203         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
204         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
205         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 0, 0, 11);
206         add(jLabel5, gridBagConstraints);
207
208         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
209         gridBagConstraints.gridx = 1;
210         gridBagConstraints.gridy = 4;
211         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
212         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
213         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
214         gridBagConstraints.weighty = 1.0;
215         add(jComboBoxJ2eeLevel, gridBagConstraints);
216         jComboBoxJ2eeLevel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WebLocationsPanel.class, "ACS_LBL_WebPagesPanel_J2EESpecLevel_A11YDesc"));
217
218     }// </editor-fold>//GEN-END:initComponents
219

220     private void jButtonWebActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonWebActionPerformed
221
JFileChooser JavaDoc chooser = createChooser(getWebPagesLocation(), wizardDescriptor);
222         if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
223             setWebPages(chooser.getSelectedFile());
224         }
225     }//GEN-LAST:event_jButtonWebActionPerformed
226

227     // Variables declaration - do not modify//GEN-BEGIN:variables
228
private javax.swing.JButton JavaDoc jButtonWeb;
229     private javax.swing.JComboBox JavaDoc jComboBoxJ2eeLevel;
230     private javax.swing.JLabel JavaDoc jLabel1;
231     private javax.swing.JLabel JavaDoc jLabel2;
232     private javax.swing.JLabel JavaDoc jLabel4;
233     private javax.swing.JLabel JavaDoc jLabel5;
234     private javax.swing.JTextField JavaDoc jTextFieldContextPath;
235     private javax.swing.JTextField JavaDoc jTextFieldWeb;
236     // End of variables declaration//GEN-END:variables
237

238     private static JFileChooser JavaDoc createChooser(File JavaDoc webPagesLoc, WizardDescriptor wizardDescriptor) {
239     String JavaDoc path = webPagesLoc.getAbsolutePath();
240         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
241         FileUtil.preventFileChooserSymlinkTraversal(chooser, new File JavaDoc(path));
242         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
243         chooser.setAcceptAllFileFilterUsed(false);
244         
245         if (path.length() > 0 && webPagesLoc.exists()) {
246             chooser.setSelectedFile(webPagesLoc);
247         } else {
248         if (wizardDescriptor != null) {
249         // honor the contract in issue 58987
250
File JavaDoc currentDirectory = null;
251         FileObject existingSourcesFO = Templates.getExistingSourcesFolder(wizardDescriptor);
252         if (existingSourcesFO != null) {
253             File JavaDoc existingSourcesFile = FileUtil.toFile(existingSourcesFO);
254             if (existingSourcesFile != null && existingSourcesFile.isDirectory()) {
255             currentDirectory = existingSourcesFile;
256             }
257         }
258         if (currentDirectory != null) {
259             chooser.setCurrentDirectory(currentDirectory);
260         } else {
261             chooser.setSelectedFile(ProjectChooser.getProjectsFolder());
262         }
263         } else {
264         chooser.setSelectedFile(ProjectChooser.getProjectsFolder());
265         }
266         }
267     
268         return chooser;
269     }
270
271     protected List JavaDoc getWebModules() {
272         ArrayList JavaDoc l = new ArrayList JavaDoc();
273
274         WebProjectGenerator.WebModule wm = new WebProjectGenerator.WebModule ();
275         wm.docRoot = getRelativeLocation(getWebPagesLocation());
276         wm.contextPath = jTextFieldContextPath.getText().trim();
277         
278         String JavaDoc j2eeLevel = (String JavaDoc) jComboBoxJ2eeLevel.getSelectedItem();
279         if (j2eeLevel.equals(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_5")))
280             wm.j2eeSpecLevel = J2EE_SPEC_5;
281         else if (j2eeLevel.equals(NbBundle.getMessage(WebLocationsPanel.class, "TXT_J2EESpecLevel_0")))
282             wm.j2eeSpecLevel = J2EE_SPEC_1_4;
283         else
284             wm.j2eeSpecLevel = J2EE_SPEC_1_3;
285         
286         wm.classpath = classpath;
287         l.add (wm);
288         return l;
289     }
290
291     protected List JavaDoc getJavaSrcFolder() {
292         ArrayList JavaDoc l = new ArrayList JavaDoc();
293         File JavaDoc sourceLoc = getSrcPackagesLocation();
294         l.add(getRelativeLocation(sourceLoc));
295         l.add(sourceLoc.getName());
296         return l;
297     }
298
299     /**
300      * @return list of pairs [relative path, display name]
301      */

302     protected List JavaDoc getWebSrcFolder() {
303         ArrayList JavaDoc l = new ArrayList JavaDoc();
304         final File JavaDoc webLocation = getWebPagesLocation();
305         l.add(getRelativeLocation(webLocation));
306         l.add(webLocation.getName());
307         return l;
308     }
309
310     private File JavaDoc getAsFile(String JavaDoc filename) {
311         return PropertyUtils.resolveFile(nbProjectFolder, filename);
312     }
313
314     /** Called from WizardDescriptor.Panel and ProjectCustomizer.Panel
315      * to set base folder. Panel will use this for default position of JFileChooser.
316      * @param baseFolder original project base folder
317      * @param nbProjectFolder Freeform Project base folder
318      */

319     public void setFolders(File JavaDoc baseFolder, File JavaDoc nbProjectFolder) {
320         this.baseFolder = baseFolder;
321         this.nbProjectFolder = nbProjectFolder;
322     }
323     
324     protected void setWebPages(String JavaDoc path) {
325         jTextFieldWeb.setText(path);
326     }
327
328     protected void setSrcPackages(String JavaDoc path) {
329         setSrcPackages(getAsFile(path));
330     }
331
332     private void setWebPages(final File JavaDoc file) {
333         setWebPages(relativizeFile(file));
334     }
335
336     protected File JavaDoc getWebPagesLocation() {
337         return getAsFile(jTextFieldWeb.getText()).getAbsoluteFile();
338
339     }
340
341     private void setSrcPackages(final File JavaDoc file) {
342         srcPackagesLocation = file;
343     }
344
345     protected File JavaDoc getSrcPackagesLocation() {
346         return srcPackagesLocation;
347     }
348
349     private String JavaDoc relativizeFile(final File JavaDoc file) {
350         File JavaDoc normalizedFile = FileUtil.normalizeFile(file);
351         if (CollocationQuery.areCollocated(nbProjectFolder, file)) {
352             return PropertyUtils.relativizeFile(nbProjectFolder, normalizedFile);
353         } else {
354             return normalizedFile.getAbsolutePath();
355         }
356     }
357
358     private String JavaDoc getRelativeLocation(final File JavaDoc location) {
359         final File JavaDoc normalizedLocation = FileUtil.normalizeFile(location);
360         return Util.relativizeLocation(baseFolder, nbProjectFolder, normalizedLocation);
361     }
362
363     ActionListener JavaDoc getCustomizerOkListener() {
364         return new ActionListener JavaDoc() {
365             public void actionPerformed(ActionEvent JavaDoc arg0) {
366                 AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(projectHelper);
367                 WebProjectGenerator.putWebSourceFolder(projectHelper, getWebSrcFolder());
368                 WebProjectGenerator.putWebModules(projectHelper, aux, getWebModules());
369             }
370         };
371     }
372     
373 }
374
Popular Tags