KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > 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.rubyproject.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.ruby.rubyproject.ui.FoldersListSettings;
30 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyUtils;
31 import org.netbeans.spi.project.ui.support.ProjectChooser;
32 import org.openide.WizardDescriptor;
33 import org.openide.WizardValidationException;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.util.NbBundle;
36 import org.openide.util.Utilities;
37
38 /**
39  *
40  * @author Petr Hrebejk
41  */

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

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

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

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

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

297     
298     // Private methods ---------------------------------------------------------------------
299

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

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

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