KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > ui > wizards > PanelProjectLocationExtSrc


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.awt.Dialog JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import javax.swing.JFileChooser JavaDoc;
28 import javax.swing.event.DocumentEvent JavaDoc;
29 import javax.swing.event.DocumentListener JavaDoc;
30 import org.openide.WizardDescriptor;
31 import org.openide.WizardValidationException;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.util.NbBundle;
35 import org.netbeans.api.project.ProjectManager;
36 import org.netbeans.modules.ruby.railsprojects.ui.FoldersListSettings;
37 import org.netbeans.spi.project.ui.support.ProjectChooser;
38 import org.openide.util.Exceptions;
39
40
41 /**
42  * Sets up source and test folders for new Java project from existing sources.
43  * @author Tomas Zezula et al.
44  */

45 public class PanelProjectLocationExtSrc extends SettingsPanel {
46
47     private PanelConfigureProject firer;
48     private WizardDescriptor wizardDescriptor;
49     private boolean calculateName;
50
51     /** Creates new form PanelSourceFolders */
52     public PanelProjectLocationExtSrc (PanelConfigureProject panel) {
53         this.firer = panel;
54         initComponents();
55
56 // this.projectName.getDocument().addDocumentListener (new DocumentListener (){
57
// public void changedUpdate(DocumentEvent e) {
58
// calculateProjectFolder ();
59
// dataChanged ();
60
// }
61
//
62
// public void insertUpdate(DocumentEvent e) {
63
// calculateProjectFolder ();
64
// dataChanged ();
65
// }
66
//
67
// public void removeUpdate(DocumentEvent e) {
68
// calculateProjectFolder ();
69
// dataChanged ();
70
// }
71
// });
72
// this.projectLocation.getDocument().addDocumentListener(new DocumentListener () {
73
// public void changedUpdate(DocumentEvent e) {
74
// setCalculateProjectFolder (false);
75
// dataChanged ();
76
// }
77
//
78
// public void insertUpdate(DocumentEvent e) {
79
// setCalculateProjectFolder (false);
80
// dataChanged ();
81
// }
82
//
83
// public void removeUpdate(DocumentEvent e) {
84
// setCalculateProjectFolder (false);
85
// dataChanged ();
86
// }
87
// });
88

89     
90         this.projectName.getDocument().addDocumentListener (new DocumentListener JavaDoc (){
91             public void changedUpdate(DocumentEvent JavaDoc e) {
92                 setCalculateProjectName (false);
93                 dataChanged ();
94             }
95
96             public void insertUpdate(DocumentEvent JavaDoc e) {
97                 setCalculateProjectName (false);
98                 dataChanged ();
99             }
100
101             public void removeUpdate(DocumentEvent JavaDoc e) {
102                 setCalculateProjectName (false);
103                 dataChanged ();
104             }
105         });
106         this.projectLocation.getDocument().addDocumentListener(new DocumentListener JavaDoc () {
107             public void changedUpdate(DocumentEvent JavaDoc e) {
108                 calculateProjectName ();
109                 dataChanged ();
110             }
111
112             public void insertUpdate(DocumentEvent JavaDoc e) {
113                 calculateProjectName ();
114                 dataChanged ();
115             }
116
117             public void removeUpdate(DocumentEvent JavaDoc e) {
118                 calculateProjectName ();
119                 dataChanged ();
120             }
121         });
122     
123     }
124
125     private synchronized void calculateProjectName () {
126         if (this.calculateName) {
127             String JavaDoc path = this.projectLocation.getText().trim();
128             if (path.length() > 0) {
129                 File JavaDoc f = new File JavaDoc(path);
130                 try {
131                     File JavaDoc g = f.getCanonicalFile();
132                     if (g != null) {
133                         f = g;
134                     }
135                 } catch (IOException JavaDoc ioe) {
136                     Exceptions.printStackTrace(ioe);
137                 }
138                 this.projectName.setText(f.getName());
139             }
140             File JavaDoc f = new File JavaDoc(this.projectLocation.getText());
141             this.calculateName = true;
142         }
143     }
144
145     private synchronized void setCalculateProjectName (boolean value) {
146         this.calculateName = value;
147     }
148     
149     
150     private void dataChanged () {
151         this.firer.fireChangeEvent();
152     }
153
154
155     void read (WizardDescriptor settings) {
156         this.wizardDescriptor = settings;
157         String JavaDoc path = null;
158         String JavaDoc projectName = null;
159         File JavaDoc projectLocation = (File JavaDoc) settings.getProperty ("projdir"); //NOI18N
160
if (projectLocation == null) {
161             projectLocation = ProjectChooser.getProjectsFolder();
162 // int index = FoldersListSettings.getDefault().getNewProjectCount();
163
// String formater = NbBundle.getMessage(PanelProjectLocationExtSrc.class,"TXT_JavaApplication");
164
// File file;
165
// do {
166
// index++;
167
// projectName = MessageFormat.format (formater, new Object[]{new Integer (index)});
168
// file = new File (projectLocation, projectName);
169
// } while (file.exists());
170
// settings.putProperty (NewRailsProjectWizardIterator.PROP_NAME_INDEX, new Integer(index));
171
this.projectLocation.setText (projectLocation.getAbsolutePath());
172             this.setCalculateProjectName(true);
173             this.calculateProjectName();
174             this.setCalculateProjectName(true);
175             projectName = this.projectName.getText();
176         }
177         else {
178             projectName = (String JavaDoc) settings.getProperty ("name"); //NOI18N
179
boolean tmpFlag = this.calculateName;
180             this.projectLocation.setText (projectLocation.getAbsolutePath());
181             this.setCalculateProjectName(tmpFlag);
182         }
183 // this.projectName.setText (projectName);
184
// this.projectName.selectAll();
185
this.projectLocation.selectAll();
186     }
187
188     void store (WizardDescriptor settings) {
189         settings.putProperty ("name",this.projectName.getText()); // NOI18N
190
File JavaDoc projectsDir = new File JavaDoc(this.projectLocation.getText());
191         settings.putProperty ("projdir", projectsDir); // NOI18N
192
}
193     
194     boolean valid (WizardDescriptor settings) {
195         String JavaDoc result = checkValidity (this.projectName.getText(), this.projectLocation.getText());
196         if (result == null) {
197             wizardDescriptor.putProperty( "WizardPanel_errorMessage",""); //NOI18N
198
return true;
199         }
200         else {
201             wizardDescriptor.putProperty( "WizardPanel_errorMessage",result); //NOI18N
202
return false;
203         }
204     }
205
206     static String JavaDoc checkValidity (final String JavaDoc projectName, final String JavaDoc projectLocation) {
207         if ( projectName.length() == 0
208              || projectName.indexOf('/') > 0 //NOI18N
209
|| projectName.indexOf('\\') > 0 //NOI18N
210
|| projectName.indexOf(':') > 0) { //NOI18N
211
// Display name not specified
212
return NbBundle.getMessage(PanelProjectLocationExtSrc.class,"MSG_IllegalProjectName");
213         }
214
215         File JavaDoc projLoc = new File JavaDoc (projectLocation).getAbsoluteFile();
216
217         if (PanelProjectLocationVisual.getCanonicalFile(projLoc) == null) {
218             return NbBundle.getMessage (PanelProjectLocationVisual.class,"MSG_IllegalProjectLocation");
219         }
220
221         while (projLoc != null && !projLoc.exists()) {
222             projLoc = projLoc.getParentFile();
223         }
224         if (projLoc == null || !projLoc.canWrite()) {
225             return NbBundle.getMessage(PanelProjectLocationExtSrc.class,"MSG_ProjectFolderReadOnly");
226         }
227
228         File JavaDoc destFolder = FileUtil.normalizeFile(new File JavaDoc( projectLocation ));
229         File JavaDoc[] kids = destFolder.listFiles();
230         boolean foundRails = false;
231         if ( destFolder.exists() && kids != null && kids.length > 0) {
232             String JavaDoc file = null;
233             for (int i=0; i< kids.length; i++) {
234                 String JavaDoc childName = kids[i].getName();
235                 if ("nbproject".equals(childName)) { //NOI18N
236
file = NbBundle.getMessage (PanelProjectLocationExtSrc.class,"TXT_NetBeansProject");
237                 }
238                 
239                 else if ("config".equals(childName)) {
240                     foundRails = true;
241                 }
242                 // Only the nbproject file will be rewritten by Rails project - other files can conflict
243
// else if ("build".equals(childName)) { //NOI18N
244
// file = NbBundle.getMessage (PanelProjectLocationExtSrc.class,"TXT_BuildFolder");
245
// }
246
// else if ("dist".equals(childName)) { //NOI18N
247
// file = NbBundle.getMessage (PanelProjectLocationExtSrc.class,"TXT_DistFolder");
248
// }
249
// else if ("build.xml".equals(childName)) { //NOI18N
250
// file = NbBundle.getMessage (PanelProjectLocationExtSrc.class,"TXT_BuildXML");
251
// }
252
// else if ("manifest.mf".equals(childName)) { //NOI18N
253
// file = NbBundle.getMessage (PanelProjectLocationExtSrc.class,"TXT_Manifest");
254
// }
255

256                 
257                 
258                 if (file != null) {
259                     String JavaDoc format = NbBundle.getMessage (PanelProjectLocationExtSrc.class,"MSG_ProjectFolderInvalid");
260                     return MessageFormat.format(format, new Object JavaDoc[] {file});
261                 }
262             }
263         }
264         
265         if (!foundRails) {
266             return NbBundle.getMessage(PanelProjectLocationExtSrc.class, "TXT_NoRails");
267         }
268
269         // For now I'm creating NetBeans Rails application in place
270
// // #47611: if there is a live project still residing here, forbid project creation.
271
// if (destFolder.isDirectory()) {
272
// FileObject destFO = FileUtil.toFileObject(destFolder);
273
// assert destFO != null : "No FileObject for " + destFolder;
274
// boolean clear = false;
275
// try {
276
// clear = ProjectManager.getDefault().findProject(destFO) == null;
277
// } catch (IOException e) {
278
// // need not report here; clear remains false -> error
279
// }
280
// if (!clear) {
281
// return NbBundle.getMessage(PanelProjectLocationExtSrc.class, "MSG_ProjectFolderHasDeletedProject");
282
// }
283
// }
284
return null;
285     }
286
287     void validate(WizardDescriptor settings) throws WizardValidationException {
288     }
289     
290     /** This method is called from within the constructor to
291      * initialize the form.
292      * WARNING: Do NOT modify this code. The content of this method is
293      * always regenerated by the Form Editor.
294      */

295     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
296
private void initComponents() {
297         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
298
299         jPanel2 = new javax.swing.JPanel JavaDoc();
300         jLabel4 = new javax.swing.JLabel JavaDoc();
301         jLabel5 = new javax.swing.JLabel JavaDoc();
302         projectName = new javax.swing.JTextField JavaDoc();
303         jLabel6 = new javax.swing.JLabel JavaDoc();
304         projectLocation = new javax.swing.JTextField JavaDoc();
305         jButton3 = new javax.swing.JButton JavaDoc();
306         jPanel1 = new javax.swing.JPanel JavaDoc();
307
308         setLayout(new java.awt.GridBagLayout JavaDoc());
309
310         jLabel4.setLabelFor(jPanel2);
311         org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_ProjectNameAndLocationLabel")); // NOI18N
312

313         jLabel5.setLabelFor(projectName);
314         org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_NWP1_ProjectName_Label")); // NOI18N
315

316         jLabel6.setDisplayedMnemonic(org.openide.util.NbBundle.getBundle(PanelProjectLocationExtSrc.class).getString("LBL_NWP1_CreatedProjectFolder_LablelMnemonic").charAt(0));
317         jLabel6.setLabelFor(projectLocation);
318         org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_NWP1_CreatedProjectFolder_Lablel")); // NOI18N
319

320         org.openide.awt.Mnemonics.setLocalizedText(jButton3, org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "LBL_NWP1_BrowseLocation_Button3")); // NOI18N
321
jButton3.addActionListener(new java.awt.event.ActionListener JavaDoc() {
322             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
323                 browseProjectLocation(evt);
324             }
325         });
326
327         org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
328         jPanel2.setLayout(jPanel2Layout);
329         jPanel2Layout.setHorizontalGroup(
330             jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
331             .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 400, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
332             .add(jPanel2Layout.createSequentialGroup()
333                 .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
334                     .add(jLabel6)
335                     .add(jLabel5))
336                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
337                 .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
338                     .add(projectName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
339                     .add(projectLocation, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE))
340                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
341                 .add(jButton3))
342         );
343         jPanel2Layout.setVerticalGroup(
344             jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
345             .add(jPanel2Layout.createSequentialGroup()
346                 .add(jLabel4)
347                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
348                 .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
349                     .add(jLabel6)
350                     .add(jButton3)
351                     .add(projectLocation, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
352                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
353                 .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
354                     .add(jLabel5)
355                     .add(projectName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
356                 .addContainerGap(40, Short.MAX_VALUE))
357         );
358
359         jLabel4.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_jLabel4")); // NOI18N
360
jLabel4.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_jLabel4")); // NOI18N
361
jLabel5.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_projectNameLabel")); // NOI18N
362
jLabel5.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_projectNameLabel")); // NOI18N
363
jLabel6.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_projectLocationLabel")); // NOI18N
364
jLabel6.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_projectLocationLabel")); // NOI18N
365
jButton3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_browseButton")); // NOI18N
366
jButton3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_browseButton")); // NOI18N
367

368         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
369         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
370         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
371         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
372         gridBagConstraints.weightx = 1.0;
373         add(jPanel2, gridBagConstraints);
374
375         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
376         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
377         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
378         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
379         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
380         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
381         gridBagConstraints.weightx = 1.0;
382         gridBagConstraints.weighty = 1.0;
383         add(jPanel1, gridBagConstraints);
384         jPanel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_jPanel1")); // NOI18N
385
jPanel1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_jPanel1")); // NOI18N
386

387         getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSN_PanelSourceFolders")); // NOI18N
388
getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelProjectLocationExtSrc.class, "ACSD_PanelSourceFolders")); // NOI18N
389
}// </editor-fold>//GEN-END:initComponents
390

391     private void browseProjectLocation(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseProjectLocation
392
// TODO add your handling code here:
393
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
394         FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
395         chooser.setDialogTitle(NbBundle.getMessage(PanelProjectLocationExtSrc.class,"LBL_NWP1_SelectProjectLocation"));
396         chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
397         String JavaDoc path = this.projectLocation.getText();
398         if (path.length() > 0) {
399             File JavaDoc f = new File JavaDoc (path);
400             if (f.exists()) {
401                 chooser.setSelectedFile (f);
402             }
403         }
404         if (chooser.showOpenDialog(this)== JFileChooser.APPROVE_OPTION) {
405             File JavaDoc file = chooser.getSelectedFile();
406             if (file != null) {
407                 this.projectLocation.setText (FileUtil.normalizeFile(file).getAbsolutePath());
408             }
409         }
410     }//GEN-LAST:event_browseProjectLocation
411

412     
413     
414     // Variables declaration - do not modify//GEN-BEGIN:variables
415
private javax.swing.JButton JavaDoc jButton3;
416     private javax.swing.JLabel JavaDoc jLabel4;
417     private javax.swing.JLabel JavaDoc jLabel5;
418     private javax.swing.JLabel JavaDoc jLabel6;
419     private javax.swing.JPanel JavaDoc jPanel1;
420     private javax.swing.JPanel JavaDoc jPanel2;
421     private javax.swing.JTextField JavaDoc projectLocation;
422     private javax.swing.JTextField JavaDoc projectName;
423     // End of variables declaration//GEN-END:variables
424

425
426 }
427
Popular Tags