KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > 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.ruby.railsprojects.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.JPanel JavaDoc;
27 import javax.swing.event.DocumentEvent JavaDoc;
28 import javax.swing.event.DocumentListener JavaDoc;
29 import javax.swing.text.Document JavaDoc;
30 import org.netbeans.modules.ruby.railsprojects.ui.FoldersListSettings;
31 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyUtils;
32 import org.netbeans.spi.project.ui.support.ProjectChooser;
33 import org.openide.WizardDescriptor;
34 import org.openide.WizardValidationException;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.util.NbBundle;
37 import org.openide.util.Utilities;
38
39 /**
40  *
41  * @author Petr Hrebejk
42  */

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

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

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

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

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

298     
299     // Private methods ---------------------------------------------------------------------
300

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

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

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