KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > ui > wizards > PanelProjectLocationExtSrc


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.java.j2seproject.ui.wizards;
21
22 import java.awt.Dialog JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import javax.swing.JFileChooser JavaDoc;
28 import javax.swing.event.DocumentEvent JavaDoc;
29 import javax.swing.event.DocumentListener JavaDoc;
30 import org.openide.WizardDescriptor;
31 import org.openide.WizardValidationException;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.util.NbBundle;
35 import org.netbeans.api.project.ProjectManager;
36 import org.netbeans.modules.java.j2seproject.ui.FoldersListSettings;
37 import org.netbeans.spi.project.ui.support.ProjectChooser;
38
39
40 /**
41  * Sets up source and test folders for new Java project from existing sources.
42  * @author Tomas Zezula et al.
43  */

44 public class PanelProjectLocationExtSrc extends SettingsPanel {
45
46     private PanelConfigureProject firer;
47     private WizardDescriptor wizardDescriptor;
48     private boolean calculatePF;
49
50     /** Creates new form PanelSourceFolders */
51     public PanelProjectLocationExtSrc (PanelConfigureProject panel) {
52         this.firer = panel;
53         initComponents();
54         this.projectName.getDocument().addDocumentListener (new DocumentListener JavaDoc (){
55             public void changedUpdate(DocumentEvent JavaDoc e) {
56                 calculateProjectFolder ();
57                 dataChanged ();
58             }
59
60             public void insertUpdate(DocumentEvent JavaDoc e) {
61                 calculateProjectFolder ();
62                 dataChanged ();
63             }
64
65             public void removeUpdate(DocumentEvent JavaDoc e) {
66                 calculateProjectFolder ();
67                 dataChanged ();
68             }
69         });
70         this.projectLocation.getDocument().addDocumentListener(new DocumentListener JavaDoc () {
71             public void changedUpdate(DocumentEvent JavaDoc e) {
72                 setCalculateProjectFolder (false);
73                 dataChanged ();
74             }
75
76             public void insertUpdate(DocumentEvent JavaDoc e) {
77                 setCalculateProjectFolder (false);
78                 dataChanged ();
79             }
80
81             public void removeUpdate(DocumentEvent JavaDoc e) {
82                 setCalculateProjectFolder (false);
83                 dataChanged ();
84             }
85         });
86     }
87
88     private synchronized void calculateProjectFolder () {
89         if (this.calculatePF) {
90             File JavaDoc f = ProjectChooser.getProjectsFolder();
91             this.projectLocation.setText (f.getAbsolutePath() + File.separator + this.projectName.getText());
92             this.calculatePF = true;
93         }
94     }
95     
96     private synchronized void setCalculateProjectFolder (boolean value) {
97         this.calculatePF = value;
98     }
99
100     private void dataChanged () {
101         this.firer.fireChangeEvent();
102     }
103
104
105     void read (WizardDescriptor settings) {
106         this.wizardDescriptor = settings;
107         String JavaDoc path = null;
108         String JavaDoc projectName = null;
109         File JavaDoc projectLocation = (File JavaDoc) settings.getProperty ("projdir"); //NOI18N
110
if (projectLocation == null) {
111             projectLocation = ProjectChooser.getProjectsFolder();
112             int index = FoldersListSettings.getDefault().getNewProjectCount();
113             String JavaDoc formater = NbBundle.getMessage(PanelSourceFolders.class,"TXT_JavaProject");
114             File JavaDoc file;
115             do {
116                 index++;
117                 projectName = MessageFormat.format (formater, new Object JavaDoc[]{new Integer JavaDoc (index)});
118                 file = new File JavaDoc (projectLocation, projectName);
119             } while (file.exists());
120             settings.putProperty (NewJ2SEProjectWizardIterator.PROP_NAME_INDEX, new Integer JavaDoc(index));
121             this.projectLocation.setText (projectLocation.getAbsolutePath());
122             this.setCalculateProjectFolder(true);
123         }
124         else {
125             projectName = (String JavaDoc) settings.getProperty ("name"); //NOI18N
126
boolean tmpFlag = this.calculatePF;
127             this.projectLocation.setText (projectLocation.getAbsolutePath());
128             this.setCalculateProjectFolder(tmpFlag);
129         }
130         this.projectName.setText (projectName);
131         this.projectName.selectAll();
132     }
133
134     void store (WizardDescriptor settings) {
135         settings.putProperty ("name",this.projectName.getText()); // NOI18N
136
File JavaDoc projectsDir = new File JavaDoc(this.projectLocation.getText());
137         settings.putProperty ("projdir", projectsDir); // NOI18N
138
}
139     
140     boolean valid (WizardDescriptor settings) {
141         String JavaDoc result = checkValidity (this.projectName.getText(), this.projectLocation.getText());
142         if (result == null) {
143             wizardDescriptor.putProperty( "WizardPanel_errorMessage",""); //NOI18N
144
return true;
145         }
146         else {
147             wizardDescriptor.putProperty( "WizardPanel_errorMessage",result); //NOI18N
148
return false;
149         }
150     }
151
152     static String JavaDoc checkValidity (final String JavaDoc projectName, final String JavaDoc projectLocation) {
153         if ( projectName.length() == 0
154              || projectName.indexOf('/') > 0 //NOI18N
155
|| projectName.indexOf('\\') > 0 //NOI18N
156
|| projectName.indexOf(':') > 0) { //NOI18N
157
// Display name not specified
158
return NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalProjectName");
159         }
160
161         File JavaDoc projLoc = new File JavaDoc (projectLocation).getAbsoluteFile();
162
163         if (PanelProjectLocationVisual.getCanonicalFile(projLoc) == null) {
164             return NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation");
165         }
166
167         while (projLoc != null && !projLoc.exists()) {
168             projLoc = projLoc.getParentFile();
169         }
170         if (projLoc == null || !projLoc.canWrite()) {
171             return NbBundle.getMessage(PanelSourceFolders.class,"MSG_ProjectFolderReadOnly");
172         }
173
174         File JavaDoc destFolder = FileUtil.normalizeFile(new File JavaDoc( projectLocation ));
175         File JavaDoc[] kids = destFolder.listFiles();
176         if ( destFolder.exists() && kids != null && kids.length > 0) {
177             String JavaDoc file = null;
178             for (int i=0; i< kids.length; i++) {
179                 String JavaDoc childName = kids[i].getName();
180                 if ("nbproject".equals(childName)) { //NOI18N
181
file = NbBundle.getMessage (PanelSourceFolders.class,"TXT_NetBeansProject");
182                 }
183                 else if ("build".equals(childName)) { //NOI18N
184
file = NbBundle.getMessage (PanelSourceFolders.class,"TXT_BuildFolder");
185                 }
186                 else if ("dist".equals(childName)) { //NOI18N
187
file = NbBundle.getMessage (PanelSourceFolders.class,"TXT_DistFolder");
188                 }
189                 else if ("build.xml".equals(childName)) { //NOI18N
190
file = NbBundle.getMessage (PanelSourceFolders.class,"TXT_BuildXML");
191                 }
192                 else if ("manifest.mf".equals(childName)) { //NOI18N
193
file = NbBundle.getMessage (PanelSourceFolders.class,"TXT_Manifest");
194                 }
195                 if (file != null) {
196                     String JavaDoc format = NbBundle.getMessage (PanelSourceFolders.class,"MSG_ProjectFolderInvalid");
197                     return MessageFormat.format(format, new Object JavaDoc[] {file});
198                 }
199             }
200         }
201
202         // #47611: if there is a live project still residing here, forbid project creation.
203
if (destFolder.isDirectory()) {
204             FileObject destFO = FileUtil.toFileObject(destFolder);
205             assert destFO != null : "No FileObject for " + destFolder;
206             boolean clear = false;
207             try {
208                 clear = ProjectManager.getDefault().findProject(destFO) == null;
209             } catch (IOException JavaDoc e) {
210                 // need not report here; clear remains false -> error
211
}
212             if (!clear) {
213                 return NbBundle.getMessage(PanelSourceFolders.class, "MSG_ProjectFolderHasDeletedProject");
214             }
215         }
216         return null;
217     }
218
219     void validate(WizardDescriptor settings) throws WizardValidationException {
220     }
221     
222     /** This method is called from within the constructor to
223      * initialize the form.
224      * WARNING: Do NOT modify this code. The content of this method is
225      * always regenerated by the Form Editor.
226      */

227     private void initComponents() {//GEN-BEGIN:initComponents
228
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
229
230         jPanel2 = new javax.swing.JPanel JavaDoc();
231         jLabel4 = new javax.swing.JLabel JavaDoc();
232         jLabel5 = new javax.swing.JLabel JavaDoc();
233         projectName = new javax.swing.JTextField JavaDoc();
234         jLabel6 = new javax.swing.JLabel JavaDoc();
235         projectLocation = new javax.swing.JTextField JavaDoc();
236         jButton3 = new javax.swing.JButton JavaDoc();
237         jPanel1 = new javax.swing.JPanel JavaDoc();
238
239         setLayout(new java.awt.GridBagLayout JavaDoc());
240
241         getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_PanelSourceFolders"));
242         getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_PanelSourceFolders"));
243         jPanel2.setLayout(new java.awt.GridBagLayout JavaDoc());
244
245         jLabel4.setLabelFor(jPanel2);
246         org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_ProjectNameAndLocationLabel"));
247         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
248         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
249         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
250         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
251         gridBagConstraints.weightx = 1.0;
252         jPanel2.add(jLabel4, gridBagConstraints);
253         jLabel4.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_jLabel4"));
254         jLabel4.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_jLabel4"));
255
256         jLabel5.setLabelFor(projectName);
257         org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_NWP1_ProjectName_Label"));
258         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
259         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
260         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
261         jPanel2.add(jLabel5, gridBagConstraints);
262         jLabel5.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_projectNameLabel"));
263         jLabel5.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_projectNameLabel"));
264
265         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
266         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
267         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
268         gridBagConstraints.weightx = 1.0;
269         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 6, 0, 0);
270         jPanel2.add(projectName, gridBagConstraints);
271
272         jLabel6.setDisplayedMnemonic(org.openide.util.NbBundle.getBundle(PanelProjectLocationExtSrc.class).getString("LBL_NWP1_CreatedProjectFolder_LablelMnemonic").charAt(0));
273         jLabel6.setLabelFor(projectLocation);
274         org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_NWP1_CreatedProjectFolder_Lablel"));
275         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
276         gridBagConstraints.gridx = 0;
277         gridBagConstraints.gridy = 2;
278         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
279         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
280         jPanel2.add(jLabel6, gridBagConstraints);
281         jLabel6.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_projectLocationLabel"));
282         jLabel6.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_projectLocationLabel"));
283
284         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
285         gridBagConstraints.gridy = 2;
286         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
287         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
288         gridBagConstraints.weightx = 1.0;
289         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 6, 0, 0);
290         jPanel2.add(projectLocation, gridBagConstraints);
291
292         org.openide.awt.Mnemonics.setLocalizedText(jButton3, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_NWP1_BrowseLocation_Button3"));
293         jButton3.addActionListener(new java.awt.event.ActionListener JavaDoc() {
294             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
295                 browseProjectLocation(evt);
296             }
297         });
298
299         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
300         gridBagConstraints.gridy = 2;
301         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
302         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
303         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 6, 0, 0);
304         jPanel2.add(jButton3, gridBagConstraints);
305         jButton3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_browseButton"));
306         jButton3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_browseButton"));
307
308         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
309         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
310         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
311         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
312         gridBagConstraints.weightx = 1.0;
313         add(jPanel2, gridBagConstraints);
314
315         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
316
317         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
318         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
319         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
320         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
321         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
322         gridBagConstraints.weightx = 1.0;
323         gridBagConstraints.weighty = 1.0;
324         add(jPanel1, gridBagConstraints);
325         jPanel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_jPanel1"));
326         jPanel1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_jPanel1"));
327
328     }//GEN-END:initComponents
329

330     private void browseProjectLocation(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseProjectLocation
331
// TODO add your handling code here:
332
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
333         FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
334         chooser.setDialogTitle(NbBundle.getMessage(PanelSourceFolders.class,"LBL_NWP1_SelectProjectLocation"));
335         chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
336         String JavaDoc path = this.projectLocation.getText();
337         if (path.length() > 0) {
338             File JavaDoc f = new File JavaDoc (path);
339             if (f.exists()) {
340                 chooser.setSelectedFile (f);
341             }
342         }
343         if (chooser.showOpenDialog(this)== JFileChooser.APPROVE_OPTION) {
344             File JavaDoc file = chooser.getSelectedFile();
345             if (file != null) {
346                 this.projectLocation.setText (FileUtil.normalizeFile(file).getAbsolutePath());
347             }
348         }
349     }//GEN-LAST:event_browseProjectLocation
350

351     
352     
353     // Variables declaration - do not modify//GEN-BEGIN:variables
354
private javax.swing.JButton JavaDoc jButton3;
355     private javax.swing.JLabel JavaDoc jLabel4;
356     private javax.swing.JLabel JavaDoc jLabel5;
357     private javax.swing.JLabel JavaDoc jLabel6;
358     private javax.swing.JPanel JavaDoc jPanel1;
359     private javax.swing.JPanel JavaDoc jPanel2;
360     private javax.swing.JTextField JavaDoc projectLocation;
361     private javax.swing.JTextField JavaDoc projectName;
362     // End of variables declaration//GEN-END:variables
363

364
365 }
366
Popular Tags