KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > ui > wizards > NewJ2SEProjectWizardIterator


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.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import org.netbeans.api.progress.ProgressHandle;
33 import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator;
34 import org.netbeans.modules.java.j2seproject.ui.FoldersListSettings;
35 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
36 import org.netbeans.spi.project.support.ant.AntProjectHelper;
37 import org.netbeans.spi.project.support.ant.EditableProperties;
38 import org.netbeans.spi.project.ui.support.ProjectChooser;
39 import org.openide.ErrorManager;
40 import org.openide.WizardDescriptor;
41 import org.openide.filesystems.FileLock;
42 import org.openide.filesystems.FileObject;
43 import org.openide.filesystems.FileUtil;
44 import org.openide.util.NbBundle;
45
46 /**
47  * Wizard to create a new J2SE project.
48  */

49 public class NewJ2SEProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator {
50
51     enum WizardType {APP, LIB, EXT}
52     
53     static final String JavaDoc PROP_NAME_INDEX = "nameIndex"; //NOI18N
54

55     private static final String JavaDoc MANIFEST_FILE = "manifest.mf"; // NOI18N
56

57     private static final long serialVersionUID = 1L;
58     
59     private WizardType type;
60     
61     /** Create a new wizard iterator. */
62     public NewJ2SEProjectWizardIterator() {
63         this(WizardType.APP);
64     }
65     
66     public NewJ2SEProjectWizardIterator(WizardType type) {
67         this.type = type;
68     }
69         
70     public static NewJ2SEProjectWizardIterator library() {
71         return new NewJ2SEProjectWizardIterator(WizardType.LIB);
72     }
73     
74     public static NewJ2SEProjectWizardIterator existing() {
75         return new NewJ2SEProjectWizardIterator(WizardType.EXT);
76     }
77
78     private WizardDescriptor.Panel[] createPanels() {
79         switch (type) {
80             case EXT:
81                 return new WizardDescriptor.Panel[] {
82                     new PanelConfigureProject(type),
83                     new PanelSourceFolders.Panel(),
84                     new PanelIncludesExcludes(),
85                 };
86             default:
87                 return new WizardDescriptor.Panel[] {
88                     new PanelConfigureProject(type)
89                 };
90         }
91     }
92     
93     private String JavaDoc[] createSteps() {
94         switch (type) {
95             case EXT:
96                 return new String JavaDoc[] {
97                     NbBundle.getMessage(NewJ2SEProjectWizardIterator.class,"LAB_ConfigureProject"),
98                     NbBundle.getMessage(NewJ2SEProjectWizardIterator.class,"LAB_ConfigureSourceRoots"),
99                     NbBundle.getMessage(NewJ2SEProjectWizardIterator.class,"LAB_PanelIncludesExcludes"),
100                 };
101             default:
102                 return new String JavaDoc[] {
103                     NbBundle.getMessage(NewJ2SEProjectWizardIterator.class,"LAB_ConfigureProject"),
104                 };
105         }
106     }
107     
108     
109     public Set JavaDoc<?> instantiate() throws IOException JavaDoc {
110         assert false : "Cannot call this method if implements WizardDescriptor.ProgressInstantiatingIterator.";
111         return null;
112     }
113         
114     public Set JavaDoc<FileObject> instantiate (ProgressHandle handle) throws IOException JavaDoc {
115         handle.start (4);
116         //handle.progress (NbBundle.getMessage (NewJ2SEProjectWizardIterator.class, "LBL_NewJ2SEProjectWizardIterator_WizardProgress_ReadingProperties"));
117
Set JavaDoc<FileObject> resultSet = new HashSet JavaDoc<FileObject>();
118         File JavaDoc dirF = (File JavaDoc)wiz.getProperty("projdir"); //NOI18N
119
if (dirF != null) {
120             dirF = FileUtil.normalizeFile(dirF);
121         }
122         String JavaDoc name = (String JavaDoc)wiz.getProperty("name"); //NOI18N
123
String JavaDoc mainClass = (String JavaDoc)wiz.getProperty("mainClass"); //NOI18N
124
handle.progress (NbBundle.getMessage (NewJ2SEProjectWizardIterator.class, "LBL_NewJ2SEProjectWizardIterator_WizardProgress_CreatingProject"), 1);
125         switch (type) {
126         case EXT:
127             File JavaDoc[] sourceFolders = (File JavaDoc[])wiz.getProperty("sourceRoot"); //NOI18N
128
File JavaDoc[] testFolders = (File JavaDoc[])wiz.getProperty("testRoot"); //NOI18N
129
AntProjectHelper h = J2SEProjectGenerator.createProject(dirF, name, sourceFolders, testFolders, MANIFEST_FILE );
130             EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
131             String JavaDoc includes = (String JavaDoc) wiz.getProperty(J2SEProjectProperties.INCLUDES);
132             if (includes == null) {
133                 includes = "**"; // NOI18N
134
}
135             ep.setProperty(J2SEProjectProperties.INCLUDES, includes);
136             String JavaDoc excludes = (String JavaDoc) wiz.getProperty(J2SEProjectProperties.EXCLUDES);
137             if (excludes == null) {
138                 excludes = ""; // NOI18N
139
}
140             ep.setProperty(J2SEProjectProperties.EXCLUDES, excludes);
141             h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
142             handle.progress (2);
143             for (File JavaDoc f : sourceFolders) {
144                 FileObject srcFo = FileUtil.toFileObject(f);
145                 if (srcFo != null) {
146                     resultSet.add (srcFo);
147                 }
148             }
149             break;
150         default:
151             h = J2SEProjectGenerator.createProject(dirF, name, mainClass, type == WizardType.APP ? MANIFEST_FILE : null);
152             handle.progress (2);
153             if (mainClass != null && mainClass.length () > 0) {
154                 try {
155                     //String sourceRoot = "src"; //(String)j2seProperties.get (J2SEProjectProperties.SRC_DIR);
156
FileObject sourcesRoot = h.getProjectDirectory ().getFileObject ("src"); //NOI18N
157
FileObject mainClassFo = getMainClassFO (sourcesRoot, mainClass);
158                     assert mainClassFo != null : "sourcesRoot: " + sourcesRoot + ", mainClass: " + mainClass; //NOI18N
159
// Returning FileObject of main class, will be called its preferred action
160
resultSet.add (mainClassFo);
161                 } catch (Exception JavaDoc x) {
162                     ErrorManager.getDefault().notify(x);
163                 }
164             }
165             // if ( type == TYPE_LIB ) {
166
// resultSet.add( h.getProjectDirectory ().getFileObject ("src") ); //NOI18N
167
// resultSet.add( h.getProjectDirectory() ); // Only expand the project directory
168
// }
169
}
170         FileObject dir = FileUtil.toFileObject(dirF);
171         switch (type) {
172             case APP:
173             case EXT:
174                 createManifest(dir);
175         }
176         handle.progress (3);
177
178         // Returning FileObject of project diretory.
179
// Project will be open and set as main
180
int index = (Integer JavaDoc) wiz.getProperty(PROP_NAME_INDEX);
181         switch (type) {
182             case APP:
183                 FoldersListSettings.getDefault().setNewApplicationCount(index);
184                 break;
185             case LIB:
186                 FoldersListSettings.getDefault().setNewLibraryCount(index);
187                 break;
188             case EXT:
189                 FoldersListSettings.getDefault().setNewProjectCount(index);
190                 break;
191         }
192         resultSet.add (dir);
193         handle.progress (NbBundle.getMessage (NewJ2SEProjectWizardIterator.class, "LBL_NewJ2SEProjectWizardIterator_WizardProgress_PreparingToOpen"), 4);
194         dirF = (dirF != null) ? dirF.getParentFile() : null;
195         if (dirF != null && dirF.exists()) {
196             ProjectChooser.setProjectsFolder (dirF);
197         }
198                         
199         return resultSet;
200     }
201     
202         
203     private transient int index;
204     private transient WizardDescriptor.Panel[] panels;
205     private transient WizardDescriptor wiz;
206     
207     public void initialize(WizardDescriptor wiz) {
208         this.wiz = wiz;
209         index = 0;
210         panels = createPanels();
211         // Make sure list of steps is accurate.
212
String JavaDoc[] steps = createSteps();
213         for (int i = 0; i < panels.length; i++) {
214             Component JavaDoc c = panels[i].getComponent();
215             if (steps[i] == null) {
216                 // Default step name to component name of panel.
217
// Mainly useful for getting the name of the target
218
// chooser to appear in the list of steps.
219
steps[i] = c.getName();
220             }
221             if (c instanceof JComponent JavaDoc) { // assume Swing components
222
JComponent JavaDoc jc = (JComponent JavaDoc)c;
223                 // Step #.
224
jc.putClientProperty("WizardPanel_contentSelectedIndex", i); // NOI18N
225
// Step name (actually the whole list for reference).
226
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
227
}
228         }
229         //set the default values of the sourceRoot and the testRoot properties
230
this.wiz.putProperty("sourceRoot", new File JavaDoc[0]); //NOI18N
231
this.wiz.putProperty("testRoot", new File JavaDoc[0]); //NOI18N
232
}
233
234     public void uninitialize(WizardDescriptor wiz) {
235         if (this.wiz != null) {
236             this.wiz.putProperty("projdir",null); //NOI18N
237
this.wiz.putProperty("name",null); //NOI18N
238
this.wiz.putProperty("mainClass",null); //NOI18N
239
switch (type) {
240             case EXT:
241                 this.wiz.putProperty("sourceRoot",null); //NOI18N
242
this.wiz.putProperty("testRoot",null); //NOI18N
243
}
244             this.wiz = null;
245             panels = null;
246         }
247     }
248     
249     public String JavaDoc name() {
250         return NbBundle.getMessage(NewJ2SEProjectWizardIterator.class, "LAB_IteratorName", index + 1, panels.length);
251     }
252     
253     public boolean hasNext() {
254         return index < panels.length - 1;
255     }
256     public boolean hasPrevious() {
257         return index > 0;
258     }
259     public void nextPanel() {
260         if (!hasNext()) throw new NoSuchElementException JavaDoc();
261         index++;
262     }
263     public void previousPanel() {
264         if (!hasPrevious()) throw new NoSuchElementException JavaDoc();
265         index--;
266     }
267     public WizardDescriptor.Panel current () {
268         return panels[index];
269     }
270     
271     // If nothing unusual changes in the middle of the wizard, simply:
272
public final void addChangeListener(ChangeListener JavaDoc l) {}
273     public final void removeChangeListener(ChangeListener JavaDoc l) {}
274     
275     // helper methods, finds mainclass's FileObject
276
private FileObject getMainClassFO (FileObject sourcesRoot, String JavaDoc mainClass) {
277         // replace '.' with '/'
278
mainClass = mainClass.replace ('.', '/'); // NOI18N
279

280         // ignore unvalid mainClass ???
281

282         return sourcesRoot.getFileObject (mainClass+ ".java"); // NOI18N
283
}
284
285     static String JavaDoc getPackageName (String JavaDoc displayName) {
286         StringBuffer JavaDoc builder = new StringBuffer JavaDoc ();
287         boolean firstLetter = true;
288         for (int i=0; i< displayName.length(); i++) {
289             char c = displayName.charAt(i);
290             if ((!firstLetter && Character.isJavaIdentifierPart (c)) || (firstLetter && Character.isJavaIdentifierStart(c))) {
291                 firstLetter = false;
292                 if (Character.isUpperCase(c)) {
293                     c = Character.toLowerCase(c);
294                 }
295                 builder.append(c);
296             }
297         }
298         return builder.length() == 0 ? NbBundle.getMessage(NewJ2SEProjectWizardIterator.class,"TXT_DefaultPackageName") : builder.toString();
299     }
300     
301     /**
302      * Create a new application manifest file with minimal initial contents.
303      * @param dir the directory to create it in
304      * @throws IOException in case of problems
305      */

306     private static void createManifest(final FileObject dir) throws IOException JavaDoc {
307         FileObject manifest = dir.createData(MANIFEST_FILE);
308         FileLock lock = manifest.lock();
309         try {
310             OutputStream JavaDoc os = manifest.getOutputStream(lock);
311             try {
312                 PrintWriter JavaDoc pw = new PrintWriter JavaDoc(os);
313                 pw.println("Manifest-Version: 1.0"); // NOI18N
314
pw.println("X-COMMENT: Main-Class will be added automatically by build"); // NOI18N
315
pw.println(); // safest to end in \n\n due to JRE parsing bug
316
pw.flush();
317             } finally {
318                 os.close();
319             }
320         } finally {
321             lock.releaseLock();
322         }
323     }
324
325 }
326
Popular Tags