KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.text.MessageFormat JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30 import java.util.Set JavaDoc;
31 import javax.swing.JComponent JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33 import org.netbeans.api.progress.ProgressHandle;
34 import org.netbeans.modules.ruby.rubyproject.RubyProjectGenerator;
35 import org.netbeans.modules.ruby.rubyproject.ui.FoldersListSettings;
36 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper;
37 import org.netbeans.spi.project.ui.support.ProjectChooser;
38 import org.openide.ErrorManager;
39 import org.openide.WizardDescriptor;
40 import org.openide.filesystems.FileLock;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.util.NbBundle;
44
45 /**
46  * Wizard to create a new Ruby project.
47  */

48 public class NewRubyProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator {
49
50     static final int TYPE_APP = 0;
51     //static final int TYPE_LIB = 1;
52
static final int TYPE_EXT = 2;
53     
54     static final String JavaDoc PROP_NAME_INDEX = "nameIndex"; //NOI18N
55

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

58     private static final long serialVersionUID = 1L;
59     
60     private int type;
61     
62     /** Create a new wizard iterator. */
63     public NewRubyProjectWizardIterator() {
64         this(TYPE_APP);
65     }
66     
67     public NewRubyProjectWizardIterator(int type) {
68         this.type = type;
69     }
70         
71 // public static NewRubyProjectWizardIterator library() {
72
// return new NewRubyProjectWizardIterator( TYPE_LIB );
73
// }
74

75     public static NewRubyProjectWizardIterator existing () {
76         return new NewRubyProjectWizardIterator( TYPE_EXT );
77     }
78
79     private WizardDescriptor.Panel[] createPanels () {
80         return this.type == TYPE_EXT ?
81             new WizardDescriptor.Panel[] {
82                 new PanelConfigureProject( this.type ),
83                 new PanelSourceFolders.Panel()
84             }
85             :new WizardDescriptor.Panel[] {
86                 new PanelConfigureProject( this.type )
87             };
88     }
89     
90     private String JavaDoc[] createSteps() {
91         return this.type == TYPE_EXT ?
92             new String JavaDoc[] {
93                 NbBundle.getMessage(NewRubyProjectWizardIterator.class,"LAB_ConfigureProject"),
94                 NbBundle.getMessage(NewRubyProjectWizardIterator.class,"LAB_ConfigureSourceRoots"),
95             }
96             :new String JavaDoc[] {
97                 NbBundle.getMessage(NewRubyProjectWizardIterator.class,"LAB_ConfigureProject"),
98             };
99     }
100     
101     
102     public Set JavaDoc/*<FileObject>*/ instantiate () throws IOException JavaDoc {
103         assert false : "Cannot call this method if implements WizardDescriptor.ProgressInstantiatingIterator.";
104         return null;
105     }
106         
107     public Set JavaDoc/*<FileObject>*/ instantiate (ProgressHandle handle) throws IOException JavaDoc {
108         handle.start (4);
109         //handle.progress (NbBundle.getMessage (NewRubyProjectWizardIterator.class, "LBL_NewRubyProjectWizardIterator_WizardProgress_ReadingProperties"));
110
Set JavaDoc resultSet = new HashSet JavaDoc ();
111         File JavaDoc dirF = (File JavaDoc)wiz.getProperty("projdir"); //NOI18N
112
if (dirF != null) {
113             dirF = FileUtil.normalizeFile(dirF);
114         }
115         String JavaDoc name = (String JavaDoc)wiz.getProperty("name"); //NOI18N
116
String JavaDoc mainClass = (String JavaDoc)wiz.getProperty("mainClass"); //NOI18N
117
handle.progress (NbBundle.getMessage (NewRubyProjectWizardIterator.class, "LBL_NewRubyProjectWizardIterator_WizardProgress_CreatingProject"), 1);
118         if (this.type == TYPE_EXT) {
119             File JavaDoc[] sourceFolders = (File JavaDoc[])wiz.getProperty("sourceRoot"); //NOI18N
120
File JavaDoc[] testFolders = (File JavaDoc[])wiz.getProperty("testRoot"); //NOI18N
121
RubyProjectGenerator.createProject(dirF, name, sourceFolders, testFolders, null );
122             handle.progress (2);
123             for (int i=0; i<sourceFolders.length; i++) {
124                 FileObject srcFo = FileUtil.toFileObject(sourceFolders[i]);
125                 if (srcFo != null) {
126                     resultSet.add (srcFo);
127                 }
128             }
129         }
130         else {
131             RakeProjectHelper h = RubyProjectGenerator.createProject(dirF, name, mainClass, null);
132             handle.progress (2);
133             // BEGIN SEMPLICE MODIFICATIONS
134
// Update since format is customized
135
//mainClass = (String)wiz.getProperty("mainClass");
136
// END SEMPLICE MODIFICATIONS
137

138             if (mainClass != null && mainClass.length () > 0) {
139                 try {
140                     //String sourceRoot = "lib"; //(String)j2seProperties.get (RubyProjectProperties.SRC_DIR);
141
FileObject sourcesRoot = h.getProjectDirectory ().getFileObject ("lib"); //NOI18N
142
FileObject mainClassFo = getMainClassFO (sourcesRoot, mainClass);
143                     assert mainClassFo != null : "sourcesRoot: " + sourcesRoot + ", mainClass: " + mainClass; //NOI18N
144
// Returning FileObject of main class, will be called its preferred action
145
resultSet.add (mainClassFo);
146                 } catch (Exception JavaDoc x) {
147                     ErrorManager.getDefault().notify(x);
148                 }
149             }
150             // if ( type == TYPE_LIB ) {
151
// resultSet.add( h.getProjectDirectory ().getFileObject ("lib") ); //NOI18N
152
// resultSet.add( h.getProjectDirectory() ); // Only expand the project directory
153
// }
154
}
155         FileObject dir = FileUtil.toFileObject(dirF);
156 // if (type == TYPE_APP || type == TYPE_EXT) {
157
// createManifest(dir);
158
// }
159
handle.progress (3);
160
161         // Returning FileObject of project diretory.
162
// Project will be open and set as main
163
Integer JavaDoc index = (Integer JavaDoc) wiz.getProperty(PROP_NAME_INDEX);
164         switch (this.type) {
165             case TYPE_APP:
166                 FoldersListSettings.getDefault().setNewApplicationCount(index.intValue());
167                 break;
168 // case TYPE_LIB:
169
// FoldersListSettings.getDefault().setNewLibraryCount(index.intValue());
170
// break;
171
case TYPE_EXT:
172                 FoldersListSettings.getDefault().setNewProjectCount(index.intValue());
173                 break;
174         }
175         resultSet.add (dir);
176         handle.progress (NbBundle.getMessage (NewRubyProjectWizardIterator.class, "LBL_NewRubyProjectWizardIterator_WizardProgress_PreparingToOpen"), 4);
177         dirF = (dirF != null) ? dirF.getParentFile() : null;
178         if (dirF != null && dirF.exists()) {
179             ProjectChooser.setProjectsFolder (dirF);
180         }
181                         
182         return resultSet;
183     }
184     
185         
186     private transient int index;
187     private transient WizardDescriptor.Panel[] panels;
188     private transient WizardDescriptor wiz;
189     
190     public void initialize(WizardDescriptor wiz) {
191         this.wiz = wiz;
192         index = 0;
193         panels = createPanels();
194         // Make sure list of steps is accurate.
195
String JavaDoc[] steps = createSteps();
196         for (int i = 0; i < panels.length; i++) {
197             Component JavaDoc c = panels[i].getComponent();
198             if (steps[i] == null) {
199                 // Default step name to component name of panel.
200
// Mainly useful for getting the name of the target
201
// chooser to appear in the list of steps.
202
steps[i] = c.getName();
203             }
204             if (c instanceof JComponent JavaDoc) { // assume Swing components
205
JComponent JavaDoc jc = (JComponent JavaDoc)c;
206                 // Step #.
207
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
208
// Step name (actually the whole list for reference).
209
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
210
}
211         }
212         //set the default values of the sourceRoot and the testRoot properties
213
this.wiz.putProperty("sourceRoot", new File JavaDoc[0]); //NOI18N
214
this.wiz.putProperty("testRoot", new File JavaDoc[0]); //NOI18N
215
}
216
217     public void uninitialize(WizardDescriptor wiz) {
218         if (this.wiz != null) {
219             this.wiz.putProperty("projdir",null); //NOI18N
220
this.wiz.putProperty("name",null); //NOI18N
221
this.wiz.putProperty("mainClass",null); //NOI18N
222
if (this.type == TYPE_EXT) {
223                 this.wiz.putProperty("sourceRoot",null); //NOI18N
224
this.wiz.putProperty("testRoot",null); //NOI18N
225
}
226             this.wiz = null;
227             panels = null;
228         }
229     }
230     
231     public String JavaDoc name() {
232         return MessageFormat.format (NbBundle.getMessage(NewRubyProjectWizardIterator.class,"LAB_IteratorName"),
233             new Object JavaDoc[] {new Integer JavaDoc (index + 1), new Integer JavaDoc (panels.length) });
234     }
235     
236     public boolean hasNext() {
237         return index < panels.length - 1;
238     }
239     public boolean hasPrevious() {
240         return index > 0;
241     }
242     public void nextPanel() {
243         if (!hasNext()) throw new NoSuchElementException JavaDoc();
244         index++;
245     }
246     public void previousPanel() {
247         if (!hasPrevious()) throw new NoSuchElementException JavaDoc();
248         index--;
249     }
250     public WizardDescriptor.Panel current () {
251         return panels[index];
252     }
253     
254     // If nothing unusual changes in the middle of the wizard, simply:
255
public final void addChangeListener(ChangeListener JavaDoc l) {}
256     public final void removeChangeListener(ChangeListener JavaDoc l) {}
257     
258     // helper methods, finds mainclass's FileObject
259
private FileObject getMainClassFO (FileObject sourcesRoot, String JavaDoc mainClass) {
260         // replace '.' with '/'
261
// mainClass = mainClass.replace ('.', '/'); // NOI18N
262
//
263
// // ignore unvalid mainClass ???
264
//
265
// return sourcesRoot.getFileObject (mainClass+ ".java"); // NOI18N
266
return sourcesRoot.getFileObject(mainClass);
267     }
268
269     static String JavaDoc getPackageName (String JavaDoc displayName) {
270         StringBuffer JavaDoc builder = new StringBuffer JavaDoc ();
271         boolean firstLetter = true;
272         for (int i=0; i< displayName.length(); i++) {
273             char c = displayName.charAt(i);
274             if ((!firstLetter && Character.isJavaIdentifierPart (c)) || (firstLetter && Character.isJavaIdentifierStart(c))) {
275                 firstLetter = false;
276                 if (Character.isUpperCase(c)) {
277                     c = Character.toLowerCase(c);
278                 }
279                 builder.append(c);
280             }
281         }
282         return builder.length() == 0 ? NbBundle.getMessage(NewRubyProjectWizardIterator.class,"TXT_DefaultPackageName") : builder.toString();
283     }
284     
285 // /**
286
// * Create a new application manifest file with minimal initial contents.
287
// * @param dir the directory to create it in
288
// * @throws IOException in case of problems
289
// */
290
// private static void createManifest(final FileObject dir) throws IOException {
291
// FileObject manifest = dir.createData(MANIFEST_FILE);
292
// FileLock lock = manifest.lock();
293
// try {
294
// OutputStream os = manifest.getOutputStream(lock);
295
// try {
296
// PrintWriter pw = new PrintWriter(os);
297
// pw.println("Manifest-Version: 1.0"); // NOI18N
298
// pw.println("X-COMMENT: Main-Class will be added automatically by build"); // NOI18N
299
// pw.println(); // safest to end in \n\n due to JRE parsing bug
300
// pw.flush();
301
// } finally {
302
// os.close();
303
// }
304
// } finally {
305
// lock.releaseLock();
306
// }
307
// }
308

309 }
310
Popular Tags