KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.modules.ruby.rubyproject.api.RubyInstallation;
34 import org.netbeans.api.progress.ProgressHandle;
35 import org.netbeans.modules.ruby.railsprojects.RailsProjectGenerator;
36 import org.netbeans.modules.ruby.railsprojects.ui.FoldersListSettings;
37 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper;
38 import org.netbeans.spi.project.ui.support.ProjectChooser;
39 import org.openide.ErrorManager;
40 import org.openide.WizardDescriptor;
41 import org.openide.cookies.EditorCookie;
42 import org.openide.filesystems.FileLock;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileUtil;
45 import org.openide.loaders.DataObject;
46 import org.openide.loaders.DataObjectNotFoundException;
47 import org.openide.modules.InstalledFileLocator;
48 import org.openide.util.NbBundle;
49
50 /**
51  * Wizard to create a new Ruby project.
52  *
53  * TODO: Disable when no valid Rails install
54  */

55 public class NewRailsProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator {
56
57     static final int TYPE_APP = 0;
58     //static final int TYPE_LIB = 1;
59
static final int TYPE_EXT = 2;
60
61     static final String JavaDoc PROP_NAME_INDEX = "nameIndex"; //NOI18N
62

63     private static final long serialVersionUID = 1L;
64     
65     private int type;
66
67     public NewRailsProjectWizardIterator() {
68         this(TYPE_APP);
69     }
70     
71     public NewRailsProjectWizardIterator(int type) {
72         this.type = type;
73     }
74
75     public static NewRailsProjectWizardIterator existing () {
76         return new NewRailsProjectWizardIterator(TYPE_EXT);
77     }
78     
79     private WizardDescriptor.Panel[] createPanels () {
80         return new WizardDescriptor.Panel[] {
81                 new PanelConfigureProject( this.type ),
82                 new RailsInstallationPanel.Panel()
83             };
84     }
85     
86     private String JavaDoc[] createSteps() {
87         return new String JavaDoc[] {
88                 NbBundle.getMessage(NewRailsProjectWizardIterator.class,"LAB_ConfigureProject"),
89                 NbBundle.getMessage(NewRailsProjectWizardIterator.class,"LAB_InstallRails"),
90         };
91     }
92     
93     
94     public Set JavaDoc/*<FileObject>*/ instantiate () throws IOException JavaDoc {
95         assert false : "Cannot call this method if implements WizardDescriptor.ProgressInstantiatingIterator.";
96         return null;
97     }
98         
99     public Set JavaDoc/*<FileObject>*/ instantiate (ProgressHandle handle) throws IOException JavaDoc {
100         handle.start (4);
101         //handle.progress (NbBundle.getMessage (NewRailsProjectWizardIterator.class, "LBL_NewRailsProjectWizardIterator_WizardProgress_ReadingProperties"));
102
Set JavaDoc resultSet = new HashSet JavaDoc ();
103         File JavaDoc dirF = (File JavaDoc)wiz.getProperty("projdir"); //NOI18N
104
if (dirF != null) {
105             dirF = FileUtil.normalizeFile(dirF);
106         }
107         String JavaDoc name = (String JavaDoc)wiz.getProperty("name"); //NOI18N
108
//String mainClass = (String)wiz.getProperty("mainClass"); //NOI18N
109
handle.progress (NbBundle.getMessage (NewRailsProjectWizardIterator.class, "LBL_NewRailsProjectWizardIterator_WizardProgress_CreatingProject"), 1);
110
111         RakeProjectHelper h = null;
112         
113         h = RailsProjectGenerator.createProject(dirF, name, type == TYPE_APP);
114         handle.progress (2);
115
116 // if (mainClass != null && mainClass.length () > 0) {
117
// try {
118
// //String sourceRoot = "src"; //(String)j2seProperties.get (RailsProjectProperties.SRC_DIR);
119
// FileObject sourcesRoot = h.getProjectDirectory ().getFileObject ("src"); //NOI18N
120
// FileObject mainClassFo = getMainClassFO (sourcesRoot, mainClass);
121
// assert mainClassFo != null : "sourcesRoot: " + sourcesRoot + ", mainClass: " + mainClass; //NOI18N
122
// // Returning FileObject of main class, will be called its preferred action
123
// resultSet.add (mainClassFo);
124
// } catch (Exception x) {
125
// ErrorManager.getDefault().notify(x);
126
// }
127
// }
128
FileObject dir = FileUtil.toFileObject(dirF);
129 // if (type == TYPE_APP || type == TYPE_EXT) {
130
// createManifest(dir);
131
// }
132
handle.progress (3);
133
134         // Returning FileObject of project diretory.
135
// Project will be open and set as main
136
// Integer index = (Integer) wiz.getProperty(PROP_NAME_INDEX);
137
// FoldersListSettings.getDefault().setNewApplicationCount(index.intValue());
138
resultSet.add (dir);
139         handle.progress (NbBundle.getMessage (NewRailsProjectWizardIterator.class, "LBL_NewRailsProjectWizardIterator_WizardProgress_PreparingToOpen"), 4);
140         dirF = (dirF != null) ? dirF.getParentFile() : null;
141         if (dirF != null && dirF.exists()) {
142             ProjectChooser.setProjectsFolder (dirF);
143         }
144         
145         // Open README.rails in the JRuby distribution
146
// (unless we're creating a rails app from existing sources - those probably don't need configuration steps)
147
if (type == TYPE_APP) {
148             String JavaDoc jrubyHome = RubyInstallation.getInstance().getJRubyHome();
149             File JavaDoc railsFile = new File JavaDoc(jrubyHome + File.separator + "docs" + File.separator + "README.rails");
150             FileObject fo = FileUtil.toFileObject(railsFile);
151             if (fo != null) {
152                 // Open
153
try {
154                     DataObject dobj = DataObject.find(fo);
155                     EditorCookie cookie = dobj.getCookie(EditorCookie.class);
156                     if (cookie != null) {
157                         cookie.open();
158                     }
159                 } catch (DataObjectNotFoundException ex) {
160                     ErrorManager.getDefault().notify(ex);
161                 }
162             }
163         }
164                         
165         return resultSet;
166     }
167     
168         
169     private transient int index;
170     private transient WizardDescriptor.Panel[] panels;
171     private transient WizardDescriptor wiz;
172     
173     public void initialize(WizardDescriptor wiz) {
174         this.wiz = wiz;
175         index = 0;
176         panels = createPanels();
177         // Make sure list of steps is accurate.
178
String JavaDoc[] steps = createSteps();
179         for (int i = 0; i < panels.length; i++) {
180             Component JavaDoc c = panels[i].getComponent();
181             if (steps[i] == null) {
182                 // Default step name to component name of panel.
183
// Mainly useful for getting the name of the target
184
// chooser to appear in the list of steps.
185
steps[i] = c.getName();
186             }
187             if (c instanceof JComponent JavaDoc) { // assume Swing components
188
JComponent JavaDoc jc = (JComponent JavaDoc)c;
189                 // Step #.
190
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
191
// Step name (actually the whole list for reference).
192
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
193
}
194         }
195         //set the default values of the sourceRoot and the testRoot properties
196
this.wiz.putProperty("sourceRoot", new File JavaDoc[0]); //NOI18N
197
this.wiz.putProperty("testRoot", new File JavaDoc[0]); //NOI18N
198
}
199
200     public void uninitialize(WizardDescriptor wiz) {
201         if (this.wiz != null) {
202             this.wiz.putProperty("projdir",null); //NOI18N
203
this.wiz.putProperty("name",null); //NOI18N
204
this.wiz.putProperty("mainClass",null); //NOI18N
205
this.wiz = null;
206             panels = null;
207         }
208     }
209     
210     public String JavaDoc name() {
211         return MessageFormat.format (NbBundle.getMessage(NewRailsProjectWizardIterator.class,"LAB_IteratorName"),
212             new Object JavaDoc[] {new Integer JavaDoc (index + 1), new Integer JavaDoc (panels.length) });
213     }
214     
215     public boolean hasNext() {
216         return index < panels.length - 1;
217     }
218     public boolean hasPrevious() {
219         return index > 0;
220     }
221     public void nextPanel() {
222         if (!hasNext()) throw new NoSuchElementException JavaDoc();
223         index++;
224     }
225     public void previousPanel() {
226         if (!hasPrevious()) throw new NoSuchElementException JavaDoc();
227         index--;
228     }
229     public WizardDescriptor.Panel current () {
230         return panels[index];
231     }
232     
233     // If nothing unusual changes in the middle of the wizard, simply:
234
public final void addChangeListener(ChangeListener JavaDoc l) {}
235     public final void removeChangeListener(ChangeListener JavaDoc l) {}
236     
237     // helper methods, finds mainclass's FileObject
238
private FileObject getMainClassFO (FileObject sourcesRoot, String JavaDoc mainClass) {
239         // replace '.' with '/'
240
// mainClass = mainClass.replace ('.', '/'); // NOI18N
241
//
242
// // ignore unvalid mainClass ???
243
//
244
// return sourcesRoot.getFileObject (mainClass+ ".java"); // NOI18N
245
return sourcesRoot.getFileObject(mainClass);
246     }
247
248     static String JavaDoc getPackageName (String JavaDoc displayName) {
249         StringBuffer JavaDoc builder = new StringBuffer JavaDoc ();
250         boolean firstLetter = true;
251         for (int i=0; i< displayName.length(); i++) {
252             char c = displayName.charAt(i);
253             if ((!firstLetter && Character.isJavaIdentifierPart (c)) || (firstLetter && Character.isJavaIdentifierStart(c))) {
254                 firstLetter = false;
255                 if (Character.isUpperCase(c)) {
256                     c = Character.toLowerCase(c);
257                 }
258                 builder.append(c);
259             }
260         }
261         return builder.length() == 0 ? NbBundle.getMessage(NewRailsProjectWizardIterator.class,"TXT_DefaultPackageName") : builder.toString();
262     }
263     
264 // /**
265
// * Create a new application manifest file with minimal initial contents.
266
// * @param dir the directory to create it in
267
// * @throws IOException in case of problems
268
// */
269
// private static void createManifest(final FileObject dir) throws IOException {
270
// FileObject manifest = dir.createData(MANIFEST_FILE);
271
// FileLock lock = manifest.lock();
272
// try {
273
// OutputStream os = manifest.getOutputStream(lock);
274
// try {
275
// PrintWriter pw = new PrintWriter(os);
276
// pw.println("Manifest-Version: 1.0"); // NOI18N
277
// pw.println("X-COMMENT: Main-Class will be added automatically by build"); // NOI18N
278
// pw.println(); // safest to end in \n\n due to JRE parsing bug
279
// pw.flush();
280
// } finally {
281
// os.close();
282
// }
283
// } finally {
284
// lock.releaseLock();
285
// }
286
// }
287

288 }
289
Popular Tags