KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > ui > wizards > NewEarProjectWizardIterator


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.j2ee.earproject.ui.wizards;
21
22 import java.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.LinkedHashSet JavaDoc;
27 import java.util.NoSuchElementException JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import org.netbeans.api.progress.ProgressHandle;
32 import org.netbeans.api.project.Project;
33 import org.netbeans.api.project.ProjectManager;
34 import org.netbeans.modules.j2ee.clientproject.api.AppClientProjectGenerator;
35 import org.netbeans.modules.j2ee.dd.api.application.Application;
36 import org.netbeans.modules.j2ee.dd.api.application.DDProvider;
37 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
38 import org.netbeans.modules.j2ee.earproject.EarProject;
39 import org.netbeans.modules.j2ee.earproject.EarProjectGenerator;
40 import org.netbeans.modules.j2ee.earproject.EarProjectType;
41 import org.netbeans.modules.j2ee.earproject.ui.FoldersListSettings;
42 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties;
43 import org.netbeans.modules.j2ee.ejbjarproject.api.EjbJarProjectGenerator;
44 import org.netbeans.modules.web.project.api.WebProjectCreateData;
45 import org.netbeans.modules.web.project.api.WebProjectUtilities;
46 import org.netbeans.spi.project.AuxiliaryConfiguration;
47 import org.netbeans.spi.project.support.ant.AntProjectHelper;
48 import org.netbeans.spi.project.support.ant.ReferenceHelper;
49 import org.netbeans.spi.project.ui.support.ProjectChooser;
50 import org.openide.ErrorManager;
51 import org.openide.WizardDescriptor;
52 import org.openide.filesystems.FileObject;
53 import org.openide.filesystems.FileUtil;
54 import org.openide.util.HelpCtx;
55 import org.openide.util.NbBundle;
56
57 /**
58  * Wizard to create a new Enterprise Application project.
59  * @author Jesse Glick
60  */

61 public class NewEarProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator {
62     
63     private static final long serialVersionUID = 1L;
64     
65     static final String JavaDoc PROP_NAME_INDEX = "nameIndex"; //NOI18N
66

67     private transient int index;
68     private transient WizardDescriptor.Panel[] panels;
69     transient WizardDescriptor wiz;
70     
71     private WizardDescriptor.Panel[] createPanels() {
72         return new WizardDescriptor.Panel[] {
73             new PanelConfigureProject(PROP_NAME_INDEX,
74                     NbBundle.getBundle(NewEarProjectWizardIterator.class),
75                     new HelpCtx(this.getClass())),
76         };
77     }
78     
79     private String JavaDoc[] createSteps() {
80         return new String JavaDoc[] {
81             NbBundle.getMessage(NewEarProjectWizardIterator.class, "LBL_NWP1_ProjectTitleName")
82         };
83     }
84     
85     public Set JavaDoc instantiate() throws IOException JavaDoc {
86         assert false : "This method cannot be called if the class implements WizardDescriptor.ProgressInstantiatingIterator.";
87         return null;
88     }
89         
90     public Set JavaDoc<FileObject> instantiate(ProgressHandle handle) throws IOException JavaDoc {
91         handle.start(9);
92         handle.progress(NbBundle.getMessage(NewEarProjectWizardIterator.class, "LBL_NewEarProjectWizardIterator_WizardProgress_CreatingProject"), 1);
93         
94         File JavaDoc dirF = (File JavaDoc) wiz.getProperty(WizardProperties.PROJECT_DIR);
95         String JavaDoc name = (String JavaDoc) wiz.getProperty(WizardProperties.NAME);
96         String JavaDoc serverInstanceID = (String JavaDoc) wiz.getProperty(WizardProperties.SERVER_INSTANCE_ID);
97         String JavaDoc j2eeLevel = (String JavaDoc) wiz.getProperty(WizardProperties.J2EE_LEVEL);
98         // Integer index = (Integer) wiz.getProperty(PROP_NAME_INDEX);
99
Boolean JavaDoc createWAR = (Boolean JavaDoc) wiz.getProperty(WizardProperties.CREATE_WAR);
100         String JavaDoc warName = null;
101         if (createWAR.booleanValue()) {
102             warName = (String JavaDoc) wiz.getProperty(WizardProperties.WAR_NAME);
103         }
104         Boolean JavaDoc createJAR = (Boolean JavaDoc) wiz.getProperty(WizardProperties.CREATE_JAR);
105         String JavaDoc ejbJarName = null;
106         if (createJAR.booleanValue()) {
107             ejbJarName = (String JavaDoc) wiz.getProperty(WizardProperties.JAR_NAME);
108         }
109         Boolean JavaDoc createCAR = (Boolean JavaDoc) wiz.getProperty(WizardProperties.CREATE_CAR);
110         String JavaDoc carName = null;
111         String JavaDoc mainClass = null;
112         if (createCAR.booleanValue()) {
113             carName = (String JavaDoc) wiz.getProperty(WizardProperties.CAR_NAME);
114             mainClass = (String JavaDoc) wiz.getProperty(WizardProperties.MAIN_CLASS);
115         }
116         String JavaDoc platformName = (String JavaDoc)wiz.getProperty(WizardProperties.JAVA_PLATFORM);
117         String JavaDoc sourceLevel = (String JavaDoc)wiz.getProperty(WizardProperties.SOURCE_LEVEL);
118         // remember last used server
119
FoldersListSettings.getDefault().setLastUsedServer(serverInstanceID);
120         return testableInstantiate(dirF,name,j2eeLevel, serverInstanceID, warName,
121                 ejbJarName, carName, mainClass, platformName, sourceLevel, handle);
122     }
123     
124     /** <strong>Package private for unit test only</strong>. */
125     static Set JavaDoc<FileObject> testableInstantiate(File JavaDoc dirF, String JavaDoc name, String JavaDoc j2eeLevel,
126             String JavaDoc serverInstanceID, String JavaDoc warName, String JavaDoc ejbJarName, String JavaDoc carName,
127             String JavaDoc mainClass, String JavaDoc platformName, String JavaDoc sourceLevel, ProgressHandle handle) throws IOException JavaDoc {
128         Set JavaDoc<FileObject> resultSet = new LinkedHashSet JavaDoc<FileObject>();
129         AntProjectHelper h = EarProjectGenerator.createProject(dirF, name, j2eeLevel, serverInstanceID, sourceLevel);
130         if (handle != null)
131             handle.progress(2);
132         FileObject dir = FileUtil.toFileObject(FileUtil.normalizeFile(dirF));
133         Project p = ProjectManager.getDefault().findProject(dir);
134         EarProject earProject = (EarProject) p.getLookup().lookup(EarProject.class);
135         if (null != earProject) {
136             Application app = null;
137             try {
138                 app = DDProvider.getDefault().getDDRoot(earProject.getAppModule().getDeploymentDescriptor());
139                 app.setDisplayName(name);
140                 app.write(earProject.getAppModule().getDeploymentDescriptor());
141             } catch (IOException JavaDoc ioe) {
142                 ErrorManager.getDefault().log(ioe.getLocalizedMessage());
143             }
144         }
145         
146         resultSet.add(dir);
147         
148         AuxiliaryConfiguration aux = h.createAuxiliaryConfiguration();
149         ReferenceHelper refHelper = new ReferenceHelper(h, aux, h.getStandardPropertyEvaluator());
150         EarProjectProperties epp = new EarProjectProperties((EarProject) p, refHelper, new EarProjectType());
151         Project webProject = null;
152         if (null != warName) {
153             File JavaDoc webAppDir = new File JavaDoc(dirF, warName);
154             
155             WebProjectCreateData createData = new WebProjectCreateData();
156             createData.setProjectDir(FileUtil.normalizeFile(webAppDir));
157             createData.setName(warName);
158             createData.setServerInstanceID(serverInstanceID);
159             createData.setSourceStructure(WebProjectUtilities.SRC_STRUCT_BLUEPRINTS);
160             createData.setJavaEEVersion(EarProjectGenerator.checkJ2eeVersion(j2eeLevel, serverInstanceID, J2eeModule.WAR));
161             createData.setContextPath('/' + warName); //NOI18N
162
createData.setJavaPlatformName(platformName);
163             createData.setSourceLevel(sourceLevel);
164             if (handle != null)
165                 handle.progress(NbBundle.getMessage(NewEarProjectWizardIterator.class, "LBL_NewEarProjectWizardIterator_WizardProgress_WAR"), 3);
166             AntProjectHelper webHelper = WebProjectUtilities.createProject(createData);
167             if (handle != null)
168                 handle.progress(4);
169
170             FileObject webAppDirFO = FileUtil.toFileObject(FileUtil.normalizeFile(webAppDir));
171             webProject = ProjectManager.getDefault().findProject(webAppDirFO);
172             epp.addJ2eeSubprojects(new Project[] { webProject });
173             resultSet.add(webAppDirFO);
174         }
175         Project appClient = null;
176         if (null != carName) {
177             File JavaDoc carDir = new File JavaDoc(dirF,carName);
178             if (handle != null)
179                 handle.progress(NbBundle.getMessage(NewEarProjectWizardIterator.class, "LBL_NewEarProjectWizardIterator_WizardProgress_AppClient"), 5);
180             AntProjectHelper clientHelper = AppClientProjectGenerator.createProject(
181                     FileUtil.normalizeFile(carDir), carName, mainClass,
182                     EarProjectGenerator.checkJ2eeVersion(j2eeLevel, serverInstanceID,
183                     J2eeModule.CLIENT), serverInstanceID);
184             if (handle != null)
185                 handle.progress(6);
186
187             if (platformName != null || sourceLevel != null) {
188                 AppClientProjectGenerator.setPlatform(clientHelper, platformName, sourceLevel);
189             }
190             FileObject carDirFO = FileUtil.toFileObject(FileUtil.normalizeFile(carDir));
191             appClient = ProjectManager.getDefault().findProject(carDirFO);
192             
193             epp.addJ2eeSubprojects(new Project[] { appClient });
194             resultSet.add(carDirFO);
195         }
196         if (null != ejbJarName) {
197             File JavaDoc ejbJarDir = new File JavaDoc(dirF,ejbJarName);
198             if (handle != null)
199                 handle.progress(NbBundle.getMessage(NewEarProjectWizardIterator.class, "LBL_NewEarProjectWizardIterator_WizardProgress_EJB"), 7);
200             AntProjectHelper ejbHelper = EjbJarProjectGenerator.createProject(FileUtil.normalizeFile(ejbJarDir),ejbJarName,
201                     EarProjectGenerator.checkJ2eeVersion(j2eeLevel, serverInstanceID, J2eeModule.EJB), serverInstanceID);
202             if (handle != null)
203                 handle.progress(8);
204
205             if (platformName != null || sourceLevel != null) {
206                 EjbJarProjectGenerator.setPlatform(ejbHelper, platformName, sourceLevel);
207             }
208             FileObject ejbJarDirFO = FileUtil.toFileObject(FileUtil.normalizeFile(ejbJarDir));
209             Project ejbJarProject = ProjectManager.getDefault().findProject(ejbJarDirFO);
210             epp.addJ2eeSubprojects(new Project[] { ejbJarProject });
211             resultSet.add(ejbJarDirFO);
212             EarProjectGenerator.addEJBToClassPaths(ejbJarProject, appClient, webProject); // #74123
213
}
214         updateModuleURI(warName, carName, epp);
215         NewEarProjectWizardIterator.setProjectChooserFolder(dirF);
216         
217         if (handle != null)
218             handle.progress(NbBundle.getMessage(NewEarProjectWizardIterator.class, "LBL_NewEarProjectWizardIterator_WizardProgress_PreparingToOpen"), 9);
219         
220         return resultSet;
221     }
222     
223     static void setProjectChooserFolder(final File JavaDoc dirF) {
224         File JavaDoc parentF = (dirF != null) ? dirF.getParentFile() : null;
225         if (parentF != null && parentF.exists()) {
226             ProjectChooser.setProjectsFolder(parentF);
227         }
228     }
229     
230     private static void updateModuleURI(final String JavaDoc warName,
231             final String JavaDoc carName, final EarProjectProperties epp) {
232         String JavaDoc clientModuleURI = null;
233         if (warName != null) {
234             // genereate application client related properties
235
String JavaDoc[] webURIs = epp.getWebUris();
236             assert webURIs.length == 1 : "Exactly one application client " +
237                     "may be generated during creation. Is: " + webURIs.length; // NOI18N
238
clientModuleURI = webURIs[0];
239         } else if (carName != null) {
240             // genereate application client related properties
241
String JavaDoc[] appClientURIs = epp.getAppClientUris();
242             assert appClientURIs.length == 1 : "Exactly one application client " +
243                     "may be generated during creation. Is: " + appClientURIs.length; // NOI18N
244
clientModuleURI = appClientURIs[0];
245         }
246         if (clientModuleURI != null) {
247             epp.put(EarProjectProperties.CLIENT_MODULE_URI, clientModuleURI);
248             epp.store();
249         }
250     }
251     
252     public void initialize(WizardDescriptor wiz) {
253         this.wiz = wiz;
254         index = 0;
255         panels = createPanels();
256         // Make sure list of steps is accurate.
257
String JavaDoc[] steps = createSteps();
258         for (int i = 0; i < panels.length; i++) {
259             Component JavaDoc c = panels[i].getComponent();
260             if (steps[i] == null) {
261                 // Default step name to component name of panel.
262
// Mainly useful for getting the name of the target
263
// chooser to appear in the list of steps.
264
steps[i] = c.getName();
265             }
266             if (c instanceof JComponent JavaDoc) { // assume Swing components
267
JComponent JavaDoc jc = (JComponent JavaDoc)c;
268                 // Step #.
269
jc.putClientProperty("WizardPanel_contentSelectedIndex", i); // NOI18N
270
// Step name (actually the whole list for reference).
271
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
272
}
273         }
274     }
275     
276     public void uninitialize(WizardDescriptor wiz) {
277         if (this.wiz != null) {
278             this.wiz.putProperty(WizardProperties.PROJECT_DIR,null);
279             this.wiz.putProperty(WizardProperties.NAME,null);
280         }
281         this.wiz = null;
282         panels = null;
283     }
284     
285     public String JavaDoc name() {
286         return MessageFormat.format(
287                 NbBundle.getMessage(NewEarProjectWizardIterator.class, "LBL_WizardStepsCount"),
288                 index + 1, panels.length);
289     }
290     
291     public boolean hasNext() {
292         return index < panels.length - 1;
293     }
294     
295     public boolean hasPrevious() {
296         return index > 0;
297     }
298     
299     public void nextPanel() {
300         if (!hasNext()) {
301             throw new NoSuchElementException JavaDoc();
302         }
303         index++;
304     }
305     
306     public void previousPanel() {
307         if (!hasPrevious()) {
308             throw new NoSuchElementException JavaDoc();
309         }
310         index--;
311     }
312     
313     public WizardDescriptor.Panel current() {
314         return panels[index];
315     }
316     
317     // If nothing unusual changes in the middle of the wizard, simply:
318
public final void addChangeListener(ChangeListener JavaDoc l) {}
319     public final void removeChangeListener(ChangeListener JavaDoc l) {}
320     
321     // helper methods, finds indexJSP's FileObject
322
FileObject getIndexJSPFO(FileObject webRoot, String JavaDoc indexJSP) {
323         // XXX: ignore unvalid mainClass?
324
return webRoot.getFileObject(indexJSP.replace('.', '/'), "jsp"); // NOI18N
325
}
326     
327 }
328
Popular Tags