KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > ui > wizards > PanelSourceFolders


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.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.event.ChangeEvent JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
34 import org.netbeans.spi.project.ui.templates.support.Templates;
35 import org.openide.DialogDisplayer;
36 import org.openide.ErrorManager;
37 import org.openide.NotifyDescriptor;
38 import org.openide.WizardDescriptor;
39 import org.openide.WizardValidationException;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.util.HelpCtx;
43 import org.openide.util.NbBundle;
44
45 //XXX There should be a way how to add nonexistent test dir
46

47 /**
48  * Sets up name and location for new Java project from existing sources.
49  * @author Tomas Zezula et al.
50  */

51 public class PanelSourceFolders extends SettingsPanel implements PropertyChangeListener JavaDoc {
52
53     private Panel firer;
54     private WizardDescriptor wizardDescriptor;
55
56     // key to action value that influence folder list current directory
57
public static final String JavaDoc INITIAL_SOURCE_ROOT = "EXISTING_SOURCES_CURRENT_DIRECTORY"; // NOI18N
58

59     /** Creates new form PanelSourceFolders */
60     public PanelSourceFolders (Panel panel) {
61         this.firer = panel;
62         initComponents();
63         this.setName(NbBundle.getMessage(PanelConfigureProjectVisual.class,"LAB_ConfigureSourceRoots"));
64         this.putClientProperty ("NewProjectWizard_Title", NbBundle.getMessage(PanelSourceFolders.class,"TXT_JavaExtSourcesProjectLocation")); // NOI18N
65
this.getAccessibleContext().setAccessibleName(NbBundle.getMessage(PanelSourceFolders.class,"AN_PanelSourceFolders"));
66         this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PanelSourceFolders.class,"AD_PanelSourceFolders"));
67         this.sourcePanel.addPropertyChangeListener (this);
68         this.testsPanel.addPropertyChangeListener(this);
69         ((FolderList)this.sourcePanel).setRelatedFolderList((FolderList)this.testsPanel);
70         ((FolderList)this.testsPanel).setRelatedFolderList((FolderList)this.sourcePanel);
71     }
72
73     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
74         if (FolderList.PROP_FILES.equals(evt.getPropertyName())) {
75             this.dataChanged();
76         }
77         else if (FolderList.PROP_LAST_USED_DIR.equals (evt.getPropertyName())) {
78             if (evt.getSource() == this.sourcePanel) {
79                 ((FolderList)this.testsPanel).setLastUsedDir
80                         ((File JavaDoc)evt.getNewValue());
81             }
82             else if (evt.getSource() == this.testsPanel) {
83                 ((FolderList)this.sourcePanel).setLastUsedDir
84                         ((File JavaDoc)evt.getNewValue());
85             }
86         }
87     }
88
89     private void dataChanged () {
90         this.firer.fireChangeEvent();
91     }
92
93
94     void read (WizardDescriptor settings) {
95         this.wizardDescriptor = settings;
96         File JavaDoc projectLocation = (File JavaDoc) settings.getProperty ("projdir"); //NOI18N
97
((FolderList)this.sourcePanel).setProjectFolder(projectLocation);
98         ((FolderList)this.testsPanel).setProjectFolder(projectLocation);
99         File JavaDoc[] srcRoot = (File JavaDoc[]) settings.getProperty ("sourceRoot"); //NOI18N
100
assert srcRoot != null : "sourceRoot property must be initialized!" ; //NOI18N
101
((FolderList)this.sourcePanel).setFiles(srcRoot);
102         File JavaDoc[] testRoot = (File JavaDoc[]) settings.getProperty ("testRoot"); //NOI18N
103
assert testRoot != null : "testRoot property must be initialized!"; //NOI18N
104
((FolderList)this.testsPanel).setFiles (testRoot);
105
106         // #58489 honor existing source folder
107
File JavaDoc currentDirectory = null;
108         FileObject folder = Templates.getExistingSourcesFolder(wizardDescriptor);
109         if (folder != null) {
110             currentDirectory = FileUtil.toFile(folder);
111         }
112         if (currentDirectory != null && currentDirectory.isDirectory()) {
113             ((FolderList)sourcePanel).setLastUsedDir(currentDirectory);
114             ((FolderList)testsPanel).setLastUsedDir(currentDirectory);
115         }
116     }
117
118     void store (WizardDescriptor settings) {
119         File JavaDoc[] sourceRoots = ((FolderList)this.sourcePanel).getFiles();
120         File JavaDoc[] testRoots = ((FolderList)this.testsPanel).getFiles();
121         settings.putProperty ("sourceRoot",sourceRoots); //NOI18N
122
settings.putProperty("testRoot",testRoots); //NOI18N
123
}
124     
125     boolean valid (WizardDescriptor settings) {
126         File JavaDoc projectLocation = (File JavaDoc) settings.getProperty ("projdir"); //NOI18N
127
File JavaDoc[] sourceRoots = ((FolderList)this.sourcePanel).getFiles();
128         File JavaDoc[] testRoots = ((FolderList)this.testsPanel).getFiles();
129         String JavaDoc result = checkValidity (projectLocation, sourceRoots, testRoots);
130         if (result == null) {
131             wizardDescriptor.putProperty( "WizardPanel_errorMessage",""); //NOI18N
132
return true;
133         }
134         else {
135             wizardDescriptor.putProperty( "WizardPanel_errorMessage",result); //NOI18N
136
return false;
137         }
138     }
139
140     static String JavaDoc checkValidity (final File JavaDoc projectLocation, final File JavaDoc[] sources, final File JavaDoc[] tests ) {
141         String JavaDoc ploc = projectLocation.getAbsolutePath ();
142         for (int i=0; i<sources.length;i++) {
143             if (!sources[i].isDirectory() || !sources[i].canRead()) {
144                 return MessageFormat.format(NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalSources"),
145                         new Object JavaDoc[] {sources[i].getAbsolutePath()});
146             }
147             String JavaDoc sloc = sources[i].getAbsolutePath ();
148             if (ploc.equals (sloc) || ploc.startsWith (sloc + File.separatorChar)) {
149                 return NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalProjectFolder");
150             }
151         }
152         for (int i=0; i<tests.length; i++) {
153             if (!tests[i].isDirectory() || !tests[i].canRead()) {
154                 return MessageFormat.format(NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalTests"),
155                         new Object JavaDoc[] {sources[i].getAbsolutePath()});
156             }
157             String JavaDoc tloc = tests[i].getAbsolutePath();
158             if (ploc.equals(tloc) || ploc.startsWith(tloc + File.separatorChar)) {
159                 return NbBundle.getMessage(PanelSourceFolders.class,"MSG_IllegalProjectFolder");
160             }
161         }
162         return null;
163     }
164     
165     void validate (WizardDescriptor d) throws WizardValidationException {
166         // sources root
167
searchClassFiles (((FolderList)this.sourcePanel).getFiles());
168         // test root, not asked in issue 48198
169
//searchClassFiles (FileUtil.toFileObject (FileUtil.normalizeFile(new File (tests.getText ()))));
170
}
171     
172     private static void findClassFiles(File JavaDoc folder, List JavaDoc<File JavaDoc> files) {
173         File JavaDoc[] kids = folder.listFiles();
174         if (kids == null) {
175             return;
176         }
177         for (File JavaDoc kid : kids) {
178             if (kid.isFile() && kid.getName().endsWith(".class")) {
179                 files.add(kid);
180             } else if (kid.isDirectory()) {
181                 findClassFiles(kid, files);
182             }
183         }
184     }
185     
186     private void searchClassFiles (File JavaDoc[] folders) throws WizardValidationException {
187         List JavaDoc<File JavaDoc> classFiles = new ArrayList JavaDoc<File JavaDoc>();
188         for (File JavaDoc folder : folders) {
189             findClassFiles(folder, classFiles);
190         }
191         if (!classFiles.isEmpty()) {
192             JButton JavaDoc DELETE_OPTION = new JButton JavaDoc (NbBundle.getMessage (PanelSourceFolders.class, "TXT_DeleteOption")); // NOI18N
193
JButton JavaDoc KEEP_OPTION = new JButton JavaDoc (NbBundle.getMessage (PanelSourceFolders.class, "TXT_KeepOption")); // NOI18N
194
JButton JavaDoc CANCEL_OPTION = new JButton JavaDoc (NbBundle.getMessage (PanelSourceFolders.class, "TXT_CancelOption")); // NOI18N
195
KEEP_OPTION.setMnemonic(NbBundle.getMessage (PanelSourceFolders.class, "MNE_KeepOption").charAt(0));
196             DELETE_OPTION.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (PanelSourceFolders.class, "AD_DeleteOption"));
197             KEEP_OPTION.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (PanelSourceFolders.class, "AD_KeepOption"));
198             CANCEL_OPTION.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage (PanelSourceFolders.class, "AD_CancelOption"));
199             NotifyDescriptor desc = new NotifyDescriptor (
200                     NbBundle.getMessage (PanelSourceFolders.class, "MSG_FoundClassFiles"), // NOI18N
201
NbBundle.getMessage (PanelSourceFolders.class, "MSG_FoundClassFiles_Title"), // NOI18N
202
NotifyDescriptor.YES_NO_CANCEL_OPTION,
203                     NotifyDescriptor.QUESTION_MESSAGE,
204                     new Object JavaDoc[] {DELETE_OPTION, KEEP_OPTION, CANCEL_OPTION},
205                     DELETE_OPTION
206                     );
207             Object JavaDoc result = DialogDisplayer.getDefault().notify(desc);
208             if (DELETE_OPTION.equals (result)) {
209                 for (File JavaDoc f : classFiles) {
210                     f.delete(); // ignore if fails
211
}
212             } else if (!KEEP_OPTION.equals (result)) {
213                 // cancel, back to wizard
214
throw new WizardValidationException (this.sourcePanel, "", ""); // NOI18N
215
}
216         }
217     }
218     
219     /** This method is called from within the constructor to
220      * initialize the form.
221      * WARNING: Do NOT modify this code. The content of this method is
222      * always regenerated by the Form Editor.
223      */

224     private void initComponents() {//GEN-BEGIN:initComponents
225
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
226
227         jLabel3 = new javax.swing.JLabel JavaDoc();
228         sourcePanel = new FolderList (NbBundle.getMessage(PanelSourceFolders.class,"CTL_SourceRoots"), NbBundle.getMessage(PanelSourceFolders.class,"MNE_SourceRoots").charAt(0),NbBundle.getMessage(PanelSourceFolders.class,"AD_SourceRoots"), NbBundle.getMessage(PanelSourceFolders.class,"CTL_AddSourceRoot"),
229             NbBundle.getMessage(PanelSourceFolders.class,"MNE_AddSourceFolder").charAt(0), NbBundle.getMessage(PanelSourceFolders.class,"AD_AddSourceFolder"),NbBundle.getMessage(PanelSourceFolders.class,"MNE_RemoveSourceFolder").charAt(0), NbBundle.getMessage(PanelSourceFolders.class,"AD_RemoveSourceFolder"));
230         testsPanel = new FolderList (NbBundle.getMessage(PanelSourceFolders.class,"CTL_TestRoots"), NbBundle.getMessage(PanelSourceFolders.class,"MNE_TestRoots").charAt(0),NbBundle.getMessage(PanelSourceFolders.class,"AD_TestRoots"), NbBundle.getMessage(PanelSourceFolders.class,"CTL_AddTestRoot"),
231             NbBundle.getMessage(PanelSourceFolders.class,"MNE_AddTestFolder").charAt(0), NbBundle.getMessage(PanelSourceFolders.class,"AD_AddTestFolder"),NbBundle.getMessage(PanelSourceFolders.class,"MNE_RemoveTestFolder").charAt(0), NbBundle.getMessage(PanelSourceFolders.class,"AD_RemoveTestFolder"));
232
233         setLayout(new java.awt.GridBagLayout JavaDoc());
234
235         getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelSourceFolders.class, "ACSN_PanelSourceFolders"));
236         getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelSourceFolders.class, "ACSD_PanelSourceFolders"));
237         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(PanelSourceFolders.class, "LBL_SourceDirectoriesLabel"));
238         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
239         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
240         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
241         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
242         add(jLabel3, gridBagConstraints);
243         jLabel3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelSourceFolders.class).getString("ACSN_jLabel3"));
244         jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelSourceFolders.class).getString("ACSD_jLabel3"));
245
246         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
247         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
248         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
249         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
250         gridBagConstraints.weightx = 1.0;
251         gridBagConstraints.weighty = 0.45;
252         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
253         add(sourcePanel, gridBagConstraints);
254
255         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
256         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
257         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
258         gridBagConstraints.weightx = 1.0;
259         gridBagConstraints.weighty = 0.45;
260         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
261         add(testsPanel, gridBagConstraints);
262
263     }//GEN-END:initComponents
264

265     
266     
267     // Variables declaration - do not modify//GEN-BEGIN:variables
268
private javax.swing.JLabel JavaDoc jLabel3;
269     private javax.swing.JPanel JavaDoc sourcePanel;
270     private javax.swing.JPanel JavaDoc testsPanel;
271     // End of variables declaration//GEN-END:variables
272

273     
274     static class Panel implements WizardDescriptor.ValidatingPanel {
275         
276         private ArrayList JavaDoc listeners;
277         private PanelSourceFolders component;
278         private WizardDescriptor settings;
279         
280         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
281             if (this.listeners == null) {
282                 return;
283             }
284             this.listeners.remove(l);
285         }
286
287         public void addChangeListener(ChangeListener JavaDoc l) {
288             if (this.listeners == null) {
289                 this.listeners = new ArrayList JavaDoc ();
290             }
291             this.listeners.add (l);
292         }
293
294         public void readSettings(Object JavaDoc settings) {
295             this.settings = (WizardDescriptor) settings;
296             this.component.read (this.settings);
297             // XXX hack, TemplateWizard in final setTemplateImpl() forces new wizard's title
298
// this name is used in NewProjectWizard to modify the title
299
Object JavaDoc substitute = component.getClientProperty ("NewProjectWizard_Title"); // NOI18N
300
if (substitute != null) {
301                 this.settings.putProperty ("NewProjectWizard_Title", substitute); // NOI18N
302
}
303         }
304
305         public void storeSettings(Object JavaDoc settings) {
306             this.component.store (this.settings);
307         }
308         
309         public void validate() throws WizardValidationException {
310             this.component.validate(this.settings);
311         }
312                 
313         public boolean isValid() {
314             return this.component.valid (this.settings);
315         }
316
317         public synchronized java.awt.Component JavaDoc getComponent() {
318             if (this.component == null) {
319                 this.component = new PanelSourceFolders (this);
320             }
321             return this.component;
322         }
323
324         public HelpCtx getHelp() {
325             return new HelpCtx (PanelSourceFolders.class);
326         }
327         
328         private void fireChangeEvent () {
329            Iterator JavaDoc it = null;
330            synchronized (this) {
331                if (this.listeners == null) {
332                    return;
333                }
334                it = ((ArrayList JavaDoc)this.listeners.clone()).iterator();
335            }
336            ChangeEvent JavaDoc event = new ChangeEvent JavaDoc (this);
337            while (it.hasNext()) {
338                ((ChangeListener JavaDoc)it.next()).stateChanged(event);
339            }
340         }
341                 
342     }
343
344 }
345
Popular Tags