KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > 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.web.project.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.web.project.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
36 /**
37  *
38  * @author Petr Hrebejk
39  */

40
41 public class PanelProjectLocationVisual extends SettingsPanel implements DocumentListener JavaDoc {
42     
43     public static final String JavaDoc PROP_PROJECT_NAME = "projectName"; //NOI18N
44
private static final String JavaDoc PROJECT_NAME_FORMATER = NbBundle.getMessage(PanelProjectLocationVisual.class,"LBL_NPW1_DefaultProjectName"); //NOI18N
45

46     private String JavaDoc generatedProjectName = "";
47     private int generatedProjectNameIndex = 0;
48
49     private PanelConfigureProject panel;
50         
51     /** Creates new form PanelProjectLocationVisual */
52     public PanelProjectLocationVisual( PanelConfigureProject panel ) {
53         initComponents();
54         this.panel = panel;
55
56         // Register listener on the textFields to make the automatic updates
57
projectNameTextField.getDocument().addDocumentListener( this );
58         projectLocationTextField.getDocument().addDocumentListener( this );
59     }
60     
61     protected int computeHeight() {
62         return createdFolderTextField.getFontMetrics(createdFolderTextField.getFont()).getHeight() * 3 + 40;
63     }
64     
65     public String JavaDoc getProjectName () {
66         return this.projectNameTextField.getText ();
67     }
68     
69     /** This method is called from within the constructor to
70      * initialize the form.
71      * WARNING: Do NOT modify this code. The content of this method is
72      * always regenerated by the Form Editor.
73      */

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

166     private void browseLocationAction(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseLocationAction
167
String JavaDoc command = evt.getActionCommand();
168         if ( "BROWSE".equals( command ) ) { // NOI18N
169
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc ();
170             FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
171             chooser.setDialogTitle(NbBundle.getMessage(PanelProjectLocationVisual.class,"LBL_NWP1_SelectProjectLocation"));
172             chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
173             String JavaDoc path = this.projectLocationTextField.getText();
174             if (path.length() > 0) {
175                 File JavaDoc f = new File JavaDoc (path);
176                 if (f.exists ()) {
177                     chooser.setSelectedFile(f);
178                 }
179             }
180             if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { //NOI18N
181
File JavaDoc projectDir = chooser.getSelectedFile();
182                 projectLocationTextField.setText( projectDir.getAbsolutePath() );
183             }
184             panel.fireChangeEvent();
185         }
186     }//GEN-LAST:event_browseLocationAction
187

188     
189     public void addNotify() {
190         super.addNotify();
191         //same problem as in 31086, initial focus on Cancel button
192
projectNameTextField.requestFocus();
193     }
194     
195     boolean valid( WizardDescriptor wizardDescriptor ) {
196         
197         if ( projectNameTextField.getText().length() == 0 ) {
198             wizardDescriptor.putProperty( "WizardPanel_errorMessage", // NOI18N
199
NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_IllegalProjectName"));
200             return false; // Display name not specified
201
}
202         File JavaDoc f = new File JavaDoc (projectLocationTextField.getText()).getAbsoluteFile();
203         if (getCanonicalFile (f)==null) {
204             String JavaDoc message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation");
205             wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
206             return false;
207         }
208         final File JavaDoc destFolder = getCanonicalFile(new File JavaDoc( createdFolderTextField.getText() ).getAbsoluteFile());
209         if (destFolder == null) {
210             String JavaDoc message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectName");
211             wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
212             return false;
213         }
214
215         File JavaDoc projLoc = destFolder;
216         while (projLoc != null && !projLoc.exists()) {
217             projLoc = projLoc.getParentFile();
218         }
219         if (projLoc == null || !projLoc.canWrite()) {
220             wizardDescriptor.putProperty( "WizardPanel_errorMessage", // NOI18N
221
NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderReadOnly"));
222             return false;
223         }
224         
225         if (FileUtil.toFileObject(projLoc) == null) {
226             String JavaDoc message = NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation");
227             wizardDescriptor.putProperty("WizardPanel_errorMessage", message);
228             return false;
229         }
230         
231         File JavaDoc[] kids = destFolder.listFiles();
232         if ( destFolder.exists() && kids != null && kids.length > 0) {
233             // Folder exists and is not empty
234
wizardDescriptor.putProperty( "WizardPanel_errorMessage", // NOI18N
235
NbBundle.getMessage(PanelProjectLocationVisual.class,"MSG_ProjectFolderExists"));
236             return false;
237         }
238         
239         wizardDescriptor.putProperty( "WizardPanel_errorMessage", "");
240         return true;
241     }
242     
243     void store( WizardDescriptor d ) {
244         String JavaDoc name = projectNameTextField.getText().trim();
245         String JavaDoc folder = createdFolderTextField.getText().trim();
246         
247         d.putProperty( /*XXX Define somewhere */ "projdir", new File JavaDoc( folder )); // NOI18N
248
d.putProperty( /*XXX Define somewhere */ "name", name ); // NOI18N
249

250         final Integer JavaDoc nameIndex = projectNameTextField.getText().equals(generatedProjectName) ? Integer.valueOf(generatedProjectNameIndex) : null;
251         d.putProperty(NewWebProjectWizardIterator.PROP_NAME_INDEX, nameIndex);
252     }
253     
254     void read (WizardDescriptor settings) {
255         File JavaDoc projectLocation = (File JavaDoc) settings.getProperty ("projdir"); //NOI18N
256
if (projectLocation == null || projectLocation.getParentFile() == null || !projectLocation.getParentFile().isDirectory ()) {
257             projectLocation = ProjectChooser.getProjectsFolder();
258         }
259         else {
260             projectLocation = projectLocation.getParentFile();
261         }
262         this.projectLocationTextField.setText (projectLocation.getAbsolutePath());
263         
264         String JavaDoc projectName = (String JavaDoc) settings.getProperty ("name"); //NOI18N
265
if (projectName == null) {
266                 int baseCount = FoldersListSettings.getDefault().getNewProjectCount() + 1;
267                 String JavaDoc formater = NbBundle.getMessage(PanelProjectLocationVisual.class,"LBL_NPW1_DefaultProjectName");
268                 while ((projectName=validFreeProjectName(projectLocation, formater, baseCount))==null)
269                     baseCount++;
270                 settings.putProperty (NewWebProjectWizardIterator.PROP_NAME_INDEX, Integer.valueOf(baseCount));
271         }
272         this.projectNameTextField.setText (projectName);
273         this.projectNameTextField.selectAll();
274     }
275         
276     void validate (WizardDescriptor d) throws WizardValidationException {
277         // nothing to validate
278
}
279     
280     // Variables declaration - do not modify//GEN-BEGIN:variables
281
private javax.swing.JButton JavaDoc browseButton;
282     private javax.swing.JLabel JavaDoc createdFolderLabel;
283     private javax.swing.JTextField JavaDoc createdFolderTextField;
284     private javax.swing.JLabel JavaDoc projectLocationLabel;
285     private javax.swing.JTextField JavaDoc projectLocationTextField;
286     private javax.swing.JLabel JavaDoc projectNameLabel;
287     protected javax.swing.JTextField JavaDoc projectNameTextField;
288     // End of variables declaration//GEN-END:variables
289

290     
291     // Private methods ---------------------------------------------------------
292

293     private String JavaDoc validFreeProjectName (final File JavaDoc parentFolder, final String JavaDoc formater, final int index) {
294         String JavaDoc name = MessageFormat.format (formater, new Object JavaDoc[]{Integer.valueOf(index)});
295         File JavaDoc file = new File JavaDoc (parentFolder, name);
296         return file.exists() ? null : name;
297     }
298
299     // Implementation of DocumentListener --------------------------------------
300

301     public void changedUpdate( DocumentEvent JavaDoc e ) {
302         updateTexts( e );
303         if (this.projectNameTextField.getDocument() == e.getDocument()) {
304             firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
305         }
306     }
307     
308     public void insertUpdate( DocumentEvent JavaDoc e ) {
309         updateTexts( e );
310         if (this.projectNameTextField.getDocument() == e.getDocument()) {
311             firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
312         }
313     }
314     
315     public void removeUpdate( DocumentEvent JavaDoc e ) {
316         updateTexts( e );
317         if (this.projectNameTextField.getDocument() == e.getDocument()) {
318             firePropertyChange (PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
319         }
320     }
321     
322     /** Handles changes in the Project name and project directory
323      */

324     private void updateTexts( DocumentEvent JavaDoc e ) {
325         Document JavaDoc doc = e.getDocument();
326         if (doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument()) {
327             String JavaDoc projectName = projectNameTextField.getText();
328             
329             if (doc == projectLocationTextField.getDocument()) {
330                 if (projectName.equals(generatedProjectName)) {
331                     File JavaDoc f = new File JavaDoc(projectLocationTextField.getText().trim());
332                     generatedProjectNameIndex = getValidProjectNameIndex(generatedProjectNameIndex, f);
333                 } else {
334                     generatedProjectNameIndex = 0;
335                 }
336                 generatedProjectName = generatedProjectNameIndex > 0 ? getProjectName(generatedProjectNameIndex) : null;
337                 if(generatedProjectNameIndex > 0) {
338                     projectName = generatedProjectName;
339                     projectNameTextField.setText(generatedProjectName);
340                     projectNameTextField.selectAll();
341                 }
342             }
343             
344             String JavaDoc projectFolder = projectLocationTextField.getText();
345             createdFolderTextField.setText(projectFolder + File.separatorChar + projectName);
346         }
347         panel.fireChangeEvent(); // Notify that the panel changed
348
}
349
350     static File JavaDoc getCanonicalFile(File JavaDoc file) {
351         try {
352             return file.getCanonicalFile();
353         } catch (IOException JavaDoc e) {
354             return null;
355         }
356     }
357
358     public static String JavaDoc getProjectName(int index) {
359         return MessageFormat.format(PROJECT_NAME_FORMATER, new Object JavaDoc[] {String.valueOf(index)});
360     }
361
362     private static int getValidProjectNameIndex(int currentIndex, File JavaDoc projectLocation) {
363         int index = currentIndex > 0 ? currentIndex : FoldersListSettings.getDefault().getNewProjectCount() + 1;
364         if(projectLocation != null) {
365             while (new File JavaDoc(projectLocation, getProjectName(index)).exists()) {
366                 index++;
367             }
368         }
369         return index;
370     }
371
372 }
373
Popular Tags