KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > 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.j2ee.clientproject.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.j2ee.clientproject.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 public class PanelProjectLocationVisual extends SettingsPanel implements DocumentListener JavaDoc {
41     
42     public static final String JavaDoc PROP_PROJECT_NAME = "projectName"; //NOI18N
43

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

65     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
66
private void initComponents() {
67         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
68
69         projectNameLabel = new javax.swing.JLabel JavaDoc();
70         projectNameTextField = new javax.swing.JTextField JavaDoc();
71         projectLocationLabel = new javax.swing.JLabel JavaDoc();
72         projectLocationTextField = new javax.swing.JTextField JavaDoc();
73         browseButton = new javax.swing.JButton JavaDoc();
74         createdFolderLabel = new javax.swing.JLabel JavaDoc();
75         createdFolderTextField = new javax.swing.JTextField JavaDoc();
76
77         setLayout(new java.awt.GridBagLayout JavaDoc());
78
79         projectNameLabel.setLabelFor(projectNameTextField);
80         org.openide.awt.Mnemonics.setLocalizedText(projectNameLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectName_Label")); // NOI18N
81
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
82         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
83         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
84         add(projectNameLabel, gridBagConstraints);
85         projectNameLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_projectNameLabel")); // NOI18N
86
projectNameLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_projectNameLabel")); // NOI18N
87

88         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
89         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
90         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
91         gridBagConstraints.weightx = 1.0;
92         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 12, 0);
93         add(projectNameTextField, gridBagConstraints);
94
95         projectLocationLabel.setLabelFor(projectLocationTextField);
96         org.openide.awt.Mnemonics.setLocalizedText(projectLocationLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_ProjectLocation_Label")); // NOI18N
97
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
98         gridBagConstraints.gridy = 1;
99         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
100         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
101         add(projectLocationLabel, gridBagConstraints);
102         projectLocationLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_projectLocationLabel")); // NOI18N
103
projectLocationLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_projectLocationLabel")); // NOI18N
104

105         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
106         gridBagConstraints.gridy = 1;
107         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
108         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
109         gridBagConstraints.weightx = 1.0;
110         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 5, 0);
111         add(projectLocationTextField, gridBagConstraints);
112
113         org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_BrowseLocation_Button")); // NOI18N
114
browseButton.setActionCommand("BROWSE");
115         browseButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
116             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
117                 browseLocationAction(evt);
118             }
119         });
120         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
121         gridBagConstraints.gridy = 1;
122         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
123         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 5, 0);
124         add(browseButton, gridBagConstraints);
125         browseButton.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_browseButton")); // NOI18N
126
browseButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_browseButton")); // NOI18N
127

128         createdFolderLabel.setLabelFor(createdFolderTextField);
129         org.openide.awt.Mnemonics.setLocalizedText(createdFolderLabel, org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "LBL_NWP1_CreatedProjectFolder_Label")); // NOI18N
130
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
131         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
132         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
133         add(createdFolderLabel, gridBagConstraints);
134         createdFolderLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSN_createdFolderLabel")); // NOI18N
135
createdFolderLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelProjectLocationVisual.class).getString("ACSD_createdFolderLabel")); // NOI18N
136

137         createdFolderTextField.setEditable(false);
138         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
139         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
140         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
141         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
142         gridBagConstraints.weightx = 1.0;
143         gridBagConstraints.weighty = 1.0;
144         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 0);
145         add(createdFolderTextField, gridBagConstraints);
146
147         getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACSN_PanelProjectLocationVisual")); // NOI18N
148
getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationVisual.class, "ACSD_PanelProjectLocationVisual")); // NOI18N
149
}// </editor-fold>//GEN-END:initComponents
150

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

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

274     
275     // Private methods ---------------------------------------------------------
276

277     private static JFileChooser JavaDoc createChooser() {
278         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
279         FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
280         chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
281         chooser.setAcceptAllFileFilterUsed( false );
282         chooser.setName( "Select Project Directory" ); // XXX // NOI18N
283
return chooser;
284     }
285     
286     private String JavaDoc validFreeProjectName(final File JavaDoc parentFolder, final String JavaDoc formater, final int index) {
287         String JavaDoc name = MessageFormat.format(formater, new Object JavaDoc[]{new Integer JavaDoc(index)});
288         File JavaDoc file = new File JavaDoc(parentFolder, name);
289         return file.exists() ? null : name;
290     }
291     
292     // Implementation of DocumentListener --------------------------------------
293

294     public void changedUpdate( DocumentEvent JavaDoc e ) {
295         updateTexts( e );
296         if (this.projectNameTextField.getDocument() == e.getDocument()) {
297             firePropertyChange(PROP_PROJECT_NAME,null,this.projectNameTextField.getText());
298         }
299     }
300     
301     public void insertUpdate( 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 removeUpdate( 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     
316     /** Handles changes in the Project name and project directory
317      */

318     private void updateTexts( DocumentEvent JavaDoc e ) {
319         
320         Document JavaDoc doc = e.getDocument();
321         
322         if ( doc == projectNameTextField.getDocument() || doc == projectLocationTextField.getDocument() ) {
323             // Change in the project name
324

325             String JavaDoc projectName = projectNameTextField.getText();
326             String JavaDoc projectFolder = projectLocationTextField.getText();
327             
328             //if ( projectFolder.trim().length() == 0 || projectFolder.equals( oldName ) ) {
329
createdFolderTextField.setText( projectFolder + File.separatorChar + projectName );
330             //}
331

332         }
333         panel.fireChangeEvent(); // Notify that the panel changed
334
}
335     
336     static File JavaDoc getCanonicalFile(File JavaDoc file) {
337         try {
338             return file.getCanonicalFile();
339         } catch (IOException JavaDoc e) {
340             return null;
341         }
342     }
343 }
344
Popular Tags