KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import javax.swing.JFileChooser JavaDoc;
26 import javax.swing.event.DocumentEvent JavaDoc;
27 import javax.swing.event.DocumentListener JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import org.netbeans.modules.java.j2seproject.ui.FoldersListSettings;
30 import org.netbeans.spi.project.ui.support.ProjectChooser;
31 import org.openide.WizardDescriptor;
32 import org.openide.WizardValidationException;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.util.NbBundle;
35 import org.openide.util.Utilities;
36
37 /**
38  *
39  * @author Petr Hrebejk
40  */

41
42 public class PanelProjectLocationVisual extends SettingsPanel implements DocumentListener JavaDoc {
43     
44     public static final String JavaDoc PROP_PROJECT_NAME = "projectName"; //NOI18N
45

46     private PanelConfigureProject panel;
47     private NewJ2SEProjectWizardIterator.WizardType type;
48         
49     public PanelProjectLocationVisual(PanelConfigureProject panel, NewJ2SEProjectWizardIterator.WizardType type) {
50         initComponents();
51         this.panel = panel;
52         this.type = type;
53         // Register listener on the textFields to make the automatic updates
54
projectNameTextField.getDocument().addDocumentListener( this );
55         projectLocationTextField.getDocument().addDocumentListener( this );
56     }
57     
58     
59     public String JavaDoc getProjectName () {
60         return this.projectNameTextField.getText ();
61     }
62     
63     /** This method is called from within the constructor to
64      * initialize the form.
65      * WARNING: Do NOT modify this code. The content of this method is
66      * always regenerated by the Form Editor.
67      */

68     private void initComponents() {//GEN-BEGIN:initComponents
69
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
70
71         projectNameLabel = new javax.swing.JLabel JavaDoc();
72         projectNameTextField = new javax.swing.JTextField JavaDoc();
73         projectLocationLabel = new javax.swing.JLabel JavaDoc();
74         projectLocationTextField = new javax.swing.JTextField JavaDoc();
75         browseButton = new javax.swing.JButton JavaDoc();
76         createdFolderLabel = new javax.swing.JLabel JavaDoc();
77         createdFolderTextField = new javax.swing.JTextField JavaDoc();
78
79         setLayout(new java.awt.GridBagLayout JavaDoc());
80
81         getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACSN_PanelProjectLocationVisual"));
82         getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACSD_PanelProjectLocationVisual"));
83         projectNameLabel.setLabelFor(projectNameTextField);
84         org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectName_Label"));
85         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
86         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
87         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
88         add(projectNameLabel, gridBagConstraints);
89         projectNameLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_projectNameLabel"));
90         projectNameLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_projectNameLabel"));
91
92         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
93         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
94         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
95         gridBagConstraints.weightx = 1.0;
96         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 12, 0);
97         add(projectNameTextField, gridBagConstraints);
98
99         projectLocationLabel.setLabelFor(projectLocationTextField);
100         org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectLocation_Label"));
101         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
102         gridBagConstraints.gridy = 1;
103         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
104         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
105         add(projectLocationLabel, gridBagConstraints);
106         projectLocationLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_projectLocationLabel"));
107         projectLocationLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_projectLocationLabel"));
108
109         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
110         gridBagConstraints.gridy = 1;
111         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
112         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
113         gridBagConstraints.weightx = 1.0;
114         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 5, 0);
115         add(projectLocationTextField, gridBagConstraints);
116
117         org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_BrowseLocation_Button"));
118         browseButton.setActionCommand("BROWSE");
119         browseButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
120             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
121                 browseLocationAction(evt);
122             }
123         });
124
125         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
126         gridBagConstraints.gridy = 1;
127         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
128         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 5, 0);
129         add(browseButton, gridBagConstraints);
130         browseButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_browseButton"));
131         browseButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_browseButton"));
132
133         createdFolderLabel.setLabelFor(createdFolderTextField);
134         org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_CreatedProjectFolder_Lablel"));
135         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
136         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
137         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
138         add(createdFolderLabel, gridBagConstraints);
139         createdFolderLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_createdFolderLabel"));
140         createdFolderLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_createdFolderLabel"));
141
142         createdFolderTextField.setEditable(false);
143         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
144         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
145         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
146         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
147         gridBagConstraints.weightx = 1.0;
148         gridBagConstraints.weighty = 1.0;
149         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 0);
150         add(createdFolderTextField, gridBagConstraints);
151
152     }//GEN-END:initComponents
153

154     private void browseLocationAction(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseLocationAction
155
String JavaDoc command = evt.getActionCommand();
156         if ( "BROWSE".equals( command ) ) { // NOI18N
157
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc ();
158             FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
159             chooser.setDialogTitle(NbBundle.getMessage(PanelSourceFolders.class,"LBL_NWP1_SelectProjectLocation"));
160             chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
161             String JavaDoc path = this.projectLocationTextField.getText();
162             if (path.length() > 0) {
163                 File JavaDoc f = new File JavaDoc (path);
164                 if (f.exists ()) {
165                     chooser.setSelectedFile(f);
166                 }
167             }
168             if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { //NOI18N
169
File JavaDoc projectDir = chooser.getSelectedFile();
170                 projectLocationTextField.setText( FileUtil.normalizeFile(projectDir).getAbsolutePath() );
171             }
172             panel.fireChangeEvent();
173         }
174     }//GEN-LAST:event_browseLocationAction
175

176     
177     public void addNotify() {
178         super.addNotify();
179         //same problem as in 31086, initial focus on Cancel button
180
projectNameTextField.requestFocus();
181     }
182     
183     boolean valid( WizardDescriptor wizardDescriptor ) {
184         
185         if ( projectNameTextField.getText().length() == 0
186             || projectNameTextField.getText().indexOf('/') > 0 //NOI18N
187
|| projectNameTextField.getText().indexOf('\\') > 0 //NOI18N
188
|| projectNameTextField.getText().indexOf(':') > 0) { //NOI18N
189
wizardDescriptor.putProperty( "WizardPanel_errorMessage", // NOI18N
190
NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_IllegalProjectName"));
191             return false; // Display name not specified
192
}
193         File JavaDoc f = new File JavaDoc (projectLocationTextField.getText()).getAbsoluteFile();
194         if (getCanonicalFile (f)==null) {
195             String JavaDoc message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation");
196             wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
197             return false;
198         }
199         // not allow to create project on unix root folder, see #82339
200
File JavaDoc cfl = getCanonicalFile(new File JavaDoc(createdFolderTextField.getText()));
201         if (Utilities.isUnix() && cfl != null && cfl.getParentFile().getParent() == null) {
202             String JavaDoc message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_ProjectInRootNotSupported");
203             wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
204             return false;
205         }
206         
207         final File JavaDoc destFolder = new File JavaDoc( createdFolderTextField.getText() ).getAbsoluteFile();
208         if (getCanonicalFile (destFolder) == null) {
209             String JavaDoc message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation");
210             wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
211             return false;
212         }
213
214         File JavaDoc projLoc = FileUtil.normalizeFile(destFolder);
215         while (projLoc != null && !projLoc.exists()) {
216             projLoc = projLoc.getParentFile();
217         }
218         if (projLoc == null || !projLoc.canWrite()) {
219             wizardDescriptor.putProperty( "WizardPanel_errorMessage", // NOI18N
220
NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderReadOnly"));
221             return false;
222         }
223         
224         if (FileUtil.toFileObject(projLoc) == null) {
225             String JavaDoc message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation");
226             wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
227             return false;
228         }
229         
230         File JavaDoc[] kids = destFolder.listFiles();
231         if ( destFolder.exists() && kids != null && kids.length > 0) {
232             // Folder exists and is not empty
233
wizardDescriptor.putProperty( "WizardPanel_errorMessage", // NOI18N
234
NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderExists"));
235             return false;
236         }
237         return true;
238     }
239     
240     void store( WizardDescriptor d ) {
241         
242         String JavaDoc name = projectNameTextField.getText().trim();
243         String JavaDoc location = projectLocationTextField.getText().trim();
244         String JavaDoc folder = createdFolderTextField.getText().trim();
245         
246         d.putProperty( /*XXX Define somewhere */ "projdir", new File JavaDoc( folder )); // NOI18N
247
d.putProperty( /*XXX Define somewhere */ "name", name ); // NOI18N
248
}
249     
250     void read (WizardDescriptor settings) {
251         File JavaDoc projectLocation = (File JavaDoc) settings.getProperty ("projdir"); //NOI18N
252
if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory ()) {
253             projectLocation = ProjectChooser.getProjectsFolder();
254         }
255         else {
256             projectLocation = projectLocation.getParentFile();
257         }
258         this.projectLocationTextField.setText (projectLocation.getAbsolutePath());
259         
260         String JavaDoc projectName = (String JavaDoc) settings.getProperty ("name"); //NOI18N
261
if (projectName == null) {
262             switch (type) {
263             case APP:
264                 int baseCount = FoldersListSettings.getDefault().getNewApplicationCount() + 1;
265                 String JavaDoc formatter = NbBundle.getMessage(PanelSourceFolders.class,"TXT_JavaApplication");
266                 while ((projectName=validFreeProjectName(projectLocation, formatter, baseCount))==null)
267                     baseCount++;
268                 settings.putProperty (NewJ2SEProjectWizardIterator.PROP_NAME_INDEX, new Integer JavaDoc(baseCount));
269                 break;
270             default:
271                 baseCount = FoldersListSettings.getDefault().getNewLibraryCount() + 1;
272                 formatter = NbBundle.getMessage(PanelSourceFolders.class,"TXT_JavaLibrary");
273                 while ((projectName=validFreeProjectName(projectLocation, formatter, baseCount))==null)
274                     baseCount++;
275                 settings.putProperty (NewJ2SEProjectWizardIterator.PROP_NAME_INDEX, new Integer JavaDoc(baseCount));
276             }
277         }
278         this.projectNameTextField.setText (projectName);
279         this.projectNameTextField.selectAll();
280     }
281         
282     void validate (WizardDescriptor d) throws WizardValidationException {
283         // nothing to validate
284
}
285     
286     // Variables declaration - do not modify//GEN-BEGIN:variables
287
private javax.swing.JButton JavaDoc browseButton;
288     private javax.swing.JLabel JavaDoc createdFolderLabel;
289     private javax.swing.JTextField JavaDoc createdFolderTextField;
290     private javax.swing.JLabel JavaDoc projectLocationLabel;
291     private javax.swing.JTextField JavaDoc projectLocationTextField;
292     private javax.swing.JLabel JavaDoc projectNameLabel;
293     private javax.swing.JTextField JavaDoc projectNameTextField;
294     // End of variables declaration//GEN-END:variables
295

296     
297     // Private methods ---------------------------------------------------------
298

299     private static JFileChooser JavaDoc createChooser() {
300         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
301         FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
302         chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
303         chooser.setAcceptAllFileFilterUsed( false );
304         chooser.setName( "Select Project Directory" ); // XXX // NOI18N
305
return chooser;
306     }
307     
308     private String JavaDoc validFreeProjectName (final File JavaDoc parentFolder, final String JavaDoc formater, final int index) {
309         String JavaDoc name = MessageFormat.format (formater, new Object JavaDoc[]{new Integer JavaDoc (index)});
310         File JavaDoc file = new File JavaDoc (parentFolder, name);
311         return file.exists() ? null : name;
312     }
313
314     // Implementation of DocumentListener --------------------------------------
315

316     public void changedUpdate( DocumentEvent JavaDoc e ) {
317         updateTexts( e );
318         if (this.projectNameTextField.getDocument() == e.getDocument()) {
319             firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
320         }
321     }
322     
323     public void insertUpdate( DocumentEvent JavaDoc e ) {
324         updateTexts( e );
325         if (this.projectNameTextField.getDocument() == e.getDocument()) {
326             firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
327         }
328     }
329     
330     public void removeUpdate( DocumentEvent JavaDoc e ) {
331         updateTexts( e );
332         if (this.projectNameTextField.getDocument() == e.getDocument()) {
333             firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
334         }
335     }
336     
337     
338     /** Handles changes in the Project name and project directory
339      */

340     private void updateTexts( DocumentEvent JavaDoc e ) {
341         Document JavaDoc doc = e.getDocument();
342         if ( doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument() ) {
343             // Change in the project name
344
String JavaDoc projectName = projectNameTextField.getText();
345             String JavaDoc projectFolder = projectLocationTextField.getText();
346             String JavaDoc projFolderPath = FileUtil.normalizeFile(new File JavaDoc(projectFolder)).getAbsolutePath();
347             if (projFolderPath.endsWith(File.separator)) {
348                 createdFolderTextField.setText(projFolderPath + projectName);
349             } else {
350                 createdFolderTextField.setText(projFolderPath + File.separator + projectName);
351             }
352         }
353         panel.fireChangeEvent(); // Notify that the panel changed
354
}
355
356     static File JavaDoc getCanonicalFile(File JavaDoc file) {
357         try {
358             return file.getCanonicalFile();
359         } catch (IOException JavaDoc e) {
360             return null;
361         }
362     }
363
364 }
365
Popular Tags