KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > wizard > NewNbModuleWizardIterator


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.apisupport.project.ui.wizard;
21
22 import java.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.NoSuchElementException JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.ui.OpenProjects;
32 import org.netbeans.modules.apisupport.project.NbModuleProjectGenerator;
33 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
34 import org.netbeans.modules.apisupport.project.suite.SuiteProjectGenerator;
35 import org.netbeans.modules.apisupport.project.ui.ModuleUISettings;
36 import org.netbeans.modules.apisupport.project.ui.UIUtil;
37 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteUtils;
38 import org.netbeans.spi.project.ui.support.ProjectChooser;
39 import org.openide.WizardDescriptor;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.util.NbBundle;
43
44 /**
45  * Wizard to create a new NetBeans Module project.
46  *
47  * @author Martin Krauskopf
48  */

49 public class NewNbModuleWizardIterator implements WizardDescriptor.AsynchronousInstantiatingIterator {
50     
51     /** Either standalone module, suite component or NB CVS module. */
52     static final int TYPE_MODULE = 1;
53     
54     /** Suite wizard. */
55     static final int TYPE_SUITE = 2;
56     
57     /** Library wrapper module wizard. */
58     static final int TYPE_LIBRARY_MODULE = 3;
59     
60     /** Pure suite component wizard. */
61     static final int TYPE_SUITE_COMPONENT = 4;
62     
63     /**
64      * Property under which a suite to be selected in the suite combo can be
65      * stored.
66      */

67     static final String JavaDoc PREFERRED_SUITE_DIR = "preferredSuiteDir"; // NOI18N
68

69     /** Tells whether the wizard should be run in a suite dedicate mode. */
70     static final String JavaDoc ONE_SUITE_DEDICATED_MODE = "oneSuiteDedicatedMode"; // NOI18N
71

72     private final NewModuleProjectData data;
73     private int position;
74     private WizardDescriptor.Panel[] panels;
75     private FileObject createdProjectFolder;
76     
77     /** See {@link #PREFERRED_SUITE_DIR}. */
78     private String JavaDoc preferredSuiteDir;
79     
80     /** See {@link #ONE_SUITE_DEDICATED_MODE}. */
81     private Boolean JavaDoc suiteDedicated = Boolean.FALSE; // default
82

83     /** Create a new wizard iterator. */
84     private NewNbModuleWizardIterator(int type) {
85         data = new NewModuleProjectData(type);
86     }
87     
88     /**
89      * Returns wizard for creating NetBeans module in general - i.e. either
90      * standalone module, suite component or NB CVS module.
91      */

92     public static NewNbModuleWizardIterator createModuleIterator() {
93         return new NewNbModuleWizardIterator(TYPE_MODULE);
94     }
95     
96     /**
97      * Returns wizard for creating suite component <strong>only</strong>.
98      */

99     public static NewNbModuleWizardIterator createSuiteComponentIterator(final SuiteProject suite) {
100         NewNbModuleWizardIterator iterator = new NewNbModuleWizardIterator(TYPE_SUITE_COMPONENT);
101         iterator.preferredSuiteDir = suite.getProjectDirectoryFile().getAbsolutePath();
102         iterator.suiteDedicated = Boolean.TRUE;
103         return iterator;
104     }
105     
106     public static NewNbModuleWizardIterator createSuiteIterator() {
107         return new NewNbModuleWizardIterator(TYPE_SUITE);
108     }
109     
110     /**
111      * Returns wizard for creating library wrapper module
112      * <strong>only</strong>. Given project <strong>must</strong> have an
113      * instance of {@link SuiteProvider} in its lookup.
114      */

115     public static NewNbModuleWizardIterator createLibraryModuleIterator(final Project project) {
116         NewNbModuleWizardIterator iterator = new NewNbModuleWizardIterator(TYPE_LIBRARY_MODULE);
117         iterator.preferredSuiteDir = SuiteUtils.getSuiteDirectoryPath(project);
118         assert iterator.preferredSuiteDir != null : project + " does not have a SuiteProvider in its lookup?"; // NOI18N
119
iterator.suiteDedicated = Boolean.TRUE;
120         return iterator;
121     }
122     
123     public static NewNbModuleWizardIterator createLibraryModuleIterator() {
124         return new NewNbModuleWizardIterator(TYPE_LIBRARY_MODULE);
125     }
126     
127     public FileObject getCreateProjectFolder() {
128         return createdProjectFolder;
129     }
130     
131     public Set JavaDoc instantiate() throws IOException JavaDoc {
132         final File JavaDoc projectFolder = new File JavaDoc(data.getProjectFolder());
133         ProjectChooser.setProjectsFolder(new File JavaDoc(data.getProjectLocation()));
134         ModuleUISettings.getDefault().setLastUsedPlatformID(data.getPlatformID());
135         WizardDescriptor settings = data.getSettings();
136         switch (data.getWizardType()) {
137             case NewNbModuleWizardIterator.TYPE_SUITE:
138                 ModuleUISettings.getDefault().setNewSuiteCounter(data.getSuiteCounter());
139                 SuiteProjectGenerator.createSuiteProject(projectFolder, data.getPlatformID());
140                 break;
141             case NewNbModuleWizardIterator.TYPE_MODULE:
142             case NewNbModuleWizardIterator.TYPE_SUITE_COMPONENT:
143                 ModuleUISettings.getDefault().setNewModuleCounter(data.getModuleCounter());
144                 if (data.isNetBeansOrg()) {
145                     // create module within the netbeans.org CVS tree
146
NbModuleProjectGenerator.createNetBeansOrgModule(projectFolder,
147                             data.getCodeNameBase(), data.getProjectDisplayName(),
148                             data.getBundle(), data.getLayer());
149                 } else if (data.isStandalone()) {
150                     // create standalone module
151
NbModuleProjectGenerator.createStandAloneModule(projectFolder,
152                             data.getCodeNameBase(), data.getProjectDisplayName(),
153                             data.getBundle(), data.getLayer(), data.getPlatformID());
154                 } else {
155                     // create suite-component module
156
NbModuleProjectGenerator.createSuiteComponentModule(projectFolder,
157                             data.getCodeNameBase(), data.getProjectDisplayName(),
158                             data.getBundle(), data.getLayer(), new File JavaDoc(data.getSuiteRoot()));
159                 }
160                 break;
161             case NewNbModuleWizardIterator.TYPE_LIBRARY_MODULE:
162                 // create suite-component module
163
File JavaDoc[] jars = LibraryStartVisualPanel.convertStringToFiles((String JavaDoc) settings.getProperty(LibraryStartVisualPanel.PROP_LIBRARY_PATH));
164                 
165                 File JavaDoc license = null;
166                 String JavaDoc licPath = (String JavaDoc) settings.getProperty(LibraryStartVisualPanel.PROP_LICENSE_PATH);
167                 if (licPath != null && licPath.length() > 0) {
168                     license = new File JavaDoc(licPath);
169                 }
170                 NbModuleProjectGenerator.createSuiteLibraryModule(projectFolder,
171                         data.getCodeNameBase(), data.getProjectDisplayName(),
172                         data.getBundle(), new File JavaDoc(data.getSuiteRoot()),
173                         license, jars);
174                 break;
175             default:
176                 throw new IllegalStateException JavaDoc("Uknown wizard type: " + data.getWizardType()); // NOI18N
177
}
178         
179         this.createdProjectFolder = FileUtil.toFileObject(FileUtil.normalizeFile(projectFolder));
180         
181         Set JavaDoc<FileObject> resultSet = new HashSet JavaDoc();
182         resultSet.add(createdProjectFolder);
183         
184         UIUtil.setProjectChooserDirParent(projectFolder);
185         
186         // XXX this constant should be defined somewhere!
187
settings.putProperty("setAsMain", Boolean.valueOf(data.isMainProject())); // NOI18N
188

189         return resultSet;
190     }
191     
192     public void initialize(WizardDescriptor wiz) {
193         data.setSettings(wiz);
194         if (preferredSuiteDir == null) {
195             Project mainPrj = OpenProjects.getDefault().getMainProject();
196             if (mainPrj != null) {
197                 preferredSuiteDir = SuiteUtils.getSuiteDirectoryPath(mainPrj);
198             }
199         }
200         if (preferredSuiteDir != null) {
201             wiz.putProperty(PREFERRED_SUITE_DIR, preferredSuiteDir);
202             wiz.putProperty(ONE_SUITE_DEDICATED_MODE, suiteDedicated);
203         }
204         
205         position = 0;
206         String JavaDoc[] steps = null;
207         switch (data.getWizardType()) {
208             case TYPE_MODULE:
209                 steps = initModuleWizard();
210                 break;
211             case TYPE_SUITE_COMPONENT:
212                 steps = initModuleWizard();
213                 break;
214             case TYPE_SUITE:
215                 steps = initSuiteModuleWizard();
216                 break;
217             case TYPE_LIBRARY_MODULE:
218                 steps = initLibraryModuleWizard();
219                 break;
220             default:
221                 assert false : "Should never get here. type: " + data.getWizardType();
222         }
223         for (int i = 0; i < panels.length; i++) {
224             Component JavaDoc c = panels[i].getComponent();
225             if (c instanceof JComponent JavaDoc) { // assume Swing components
226
JComponent JavaDoc jc = (JComponent JavaDoc) c;
227                 // step number
228
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
229
// names of currently used steps
230
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
231

232                 // Following is actually needed only by direct usage of this wizard.
233
// Turn on subtitle creation on each step
234
jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); // NOI18N
235
// Show steps on the left side with the image on the background
236
jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE); // NOI18N
237
// Turn on numbering of all steps
238
jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE); // NOI18N
239
}
240         }
241     }
242     
243     public void uninitialize(WizardDescriptor wiz) {
244         panels = null;
245     }
246     
247     private String JavaDoc[] initModuleWizard() {
248         panels = new WizardDescriptor.Panel[] {
249             new BasicInfoWizardPanel(data),
250             new BasicConfWizardPanel(data)
251         };
252         String JavaDoc[] steps = new String JavaDoc[] {
253             getMessage("LBL_BasicInfoPanel_Title"), // NOI18N
254
getMessage("LBL_BasicConfigPanel_Title") // NOI18N
255
};
256         return steps;
257     }
258     
259     private String JavaDoc[] initSuiteModuleWizard() {
260         panels = new WizardDescriptor.Panel[] {
261             new BasicInfoWizardPanel(data),
262         };
263         String JavaDoc[] steps = new String JavaDoc[] {
264             getMessage("LBL_BasicInfoPanel_Title"), // NOI18N
265
};
266         return steps;
267     }
268     
269     private String JavaDoc[] initLibraryModuleWizard() {
270         panels = new WizardDescriptor.Panel[] {
271             new LibraryStartWizardPanel(data),
272             new BasicInfoWizardPanel(data),
273             new LibraryConfWizardPanel(data)
274         };
275         String JavaDoc[] steps = new String JavaDoc[] {
276             getMessage("LBL_LibraryStartPanel_Title"), //NOi18N
277
getMessage("LBL_BasicInfoPanel_Title"), // NOI18N
278
getMessage("LBL_PlatformSelectionPanel_Title") // NOI18N
279
};
280         return steps;
281     }
282     
283     public String JavaDoc name() {
284         // TemplateWizard internally does not use the value returned by this
285
// method so we may return whatever (e.g. null) in the meantime. But it
286
// would be resolved as "null" string by MessageFormat. So probably the
287
// safest is to return empty string.
288
return "";
289     }
290     
291     public boolean hasNext() {
292         return position < (panels.length - 1);
293     }
294     
295     public boolean hasPrevious() {
296         return position > 0;
297     }
298     
299     public void nextPanel() {
300         if (!hasNext()) {
301             throw new NoSuchElementException JavaDoc();
302         }
303         position++;
304     }
305     
306     public void previousPanel() {
307         if (!hasPrevious()) {
308             throw new NoSuchElementException JavaDoc();
309         }
310         position--;
311     }
312     
313     public WizardDescriptor.Panel current() {
314         return panels[position];
315     }
316     
317     /**
318      * Convenience method for accessing Bundle resources from this package.
319      */

320     static String JavaDoc getMessage(String JavaDoc key) {
321         return NbBundle.getMessage(NewNbModuleWizardIterator.class, key);
322     }
323     
324     // If nothing unusual changes in the middle of the wizard, simply:
325
public final void addChangeListener(ChangeListener JavaDoc l) {}
326     public final void removeChangeListener(ChangeListener JavaDoc l) {}
327     
328 }
329
Popular Tags