KickJava   Java API By Example, From Geeks To Geeks.

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

43 /**
44  * Sets up name and location for new Java project from existing sources.
45  * @author Tomas Zezula et al.
46  */

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

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

219     private void initComponents() {//GEN-BEGIN:initComponents
220
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
221
222         jLabel3 = new javax.swing.JLabel JavaDoc();
223         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"),
224             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"));
225         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"),
226             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"));
227
228         setLayout(new java.awt.GridBagLayout JavaDoc());
229
230         getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PanelSourceFolders.class, "ACSN_PanelSourceFolders"));
231         getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PanelSourceFolders.class, "ACSD_PanelSourceFolders"));
232         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(PanelSourceFolders.class, "LBL_SourceDirectoriesLabel"));
233         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
234         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
235         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
236         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
237         add(jLabel3, gridBagConstraints);
238         jLabel3.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(PanelSourceFolders.class).getString("ACSN_jLabel3"));
239         jLabel3.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(PanelSourceFolders.class).getString("ACSD_jLabel3"));
240
241         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
242         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
243         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
244         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
245         gridBagConstraints.weightx = 1.0;
246         gridBagConstraints.weighty = 0.45;
247         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
248         add(sourcePanel, gridBagConstraints);
249
250         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
251         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
252         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
253         gridBagConstraints.weightx = 1.0;
254         gridBagConstraints.weighty = 0.45;
255         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
256         add(testsPanel, gridBagConstraints);
257
258     }//GEN-END:initComponents
259

260     
261     
262     // Variables declaration - do not modify//GEN-BEGIN:variables
263
private javax.swing.JLabel JavaDoc jLabel3;
264     private javax.swing.JPanel JavaDoc sourcePanel;
265     private javax.swing.JPanel JavaDoc testsPanel;
266     // End of variables declaration//GEN-END:variables
267

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