KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > ui > wizards > NewWebProjectWizardIterator


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.web.project.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.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.swing.JComponent JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33
34 import org.openide.WizardDescriptor;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.util.NbBundle;
38
39 import org.netbeans.api.project.Project;
40 import org.netbeans.api.project.ProjectManager;
41 import org.netbeans.spi.project.support.ant.AntProjectHelper;
42 import org.netbeans.spi.project.ui.support.ProjectChooser;
43 import org.netbeans.api.progress.ProgressHandle;
44
45 import org.netbeans.modules.j2ee.api.ejbjar.Ear;
46 import org.netbeans.modules.web.api.webmodule.WebFrameworkSupport;
47 import org.netbeans.modules.web.spi.webmodule.WebFrameworkProvider;
48 import org.netbeans.modules.web.project.WebProject;
49 import org.netbeans.modules.web.project.api.WebProjectCreateData;
50 import org.netbeans.modules.web.project.api.WebProjectUtilities;
51 import org.netbeans.modules.web.project.ui.FoldersListSettings;
52
53 /**
54  * Wizard to create a new Web project.
55  * @author Jesse Glick, Radko Najman
56  */

57 public class NewWebProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator {
58     
59     private static final long serialVersionUID = 1L;
60     
61     static final String JavaDoc PROP_NAME_INDEX = "nameIndex"; //NOI18N
62

63     /** Create a new wizard iterator. */
64     public NewWebProjectWizardIterator() {}
65         
66     private String JavaDoc[] createSteps() {
67     String JavaDoc[] steps;
68     if (WebFrameworkSupport.getFrameworkProviders().size() > 0)
69         steps = new String JavaDoc[] {
70         NbBundle.getMessage(NewWebProjectWizardIterator.class, "LBL_NWP1_ProjectTitleName"), //NOI18N
71
NbBundle.getMessage(NewWebProjectWizardIterator.class, "LBL_NWP2_Frameworks") //NOI18N
72
};
73     else
74         steps = new String JavaDoc[] {
75         NbBundle.getMessage(NewWebProjectWizardIterator.class, "LBL_NWP1_ProjectTitleName"), //NOI18N
76
};
77     
78         return steps;
79     }
80     
81     public Set JavaDoc instantiate() throws IOException JavaDoc {
82         assert false : "This method cannot be called if the class implements WizardDescriptor.ProgressInstantiatingIterator.";
83         return null;
84     }
85         
86     public Set JavaDoc<FileObject> instantiate(ProgressHandle handle) throws IOException JavaDoc {
87         handle.start(4);
88         handle.progress(NbBundle.getMessage(NewWebProjectWizardIterator.class, "LBL_NewWebProjectWizardIterator_WizardProgress_CreatingProject"), 1);
89         
90         Set JavaDoc resultSet = new HashSet JavaDoc();
91
92         File JavaDoc dirF = (File JavaDoc) wiz.getProperty(WizardProperties.PROJECT_DIR);
93         String JavaDoc servInstID = (String JavaDoc) wiz.getProperty(WizardProperties.SERVER_INSTANCE_ID);
94         
95         WebProjectCreateData createData = new WebProjectCreateData();
96         createData.setProjectDir(dirF);
97         createData.setName((String JavaDoc) wiz.getProperty(WizardProperties.NAME));
98         createData.setServerInstanceID(servInstID);
99         createData.setSourceStructure((String JavaDoc)wiz.getProperty(WizardProperties.SOURCE_STRUCTURE));
100         createData.setJavaEEVersion((String JavaDoc) wiz.getProperty(WizardProperties.J2EE_LEVEL));
101         createData.setContextPath((String JavaDoc) wiz.getProperty(WizardProperties.CONTEXT_PATH));
102         createData.setJavaPlatformName((String JavaDoc) wiz.getProperty(WizardProperties.JAVA_PLATFORM));
103         createData.setSourceLevel((String JavaDoc) wiz.getProperty(WizardProperties.SOURCE_LEVEL));
104         AntProjectHelper h = WebProjectUtilities.createProject(createData);
105         handle.progress(2);
106         
107         FileObject webRoot = h.getProjectDirectory().getFileObject("web");//NOI18N
108
FileObject indexJSPFo = getIndexJSPFO(webRoot, "index"); //NOI18N
109
assert indexJSPFo != null : "webRoot: " + webRoot + ", defaultJSP: index";//NOI18N
110
// Returning FileObject of main class, will be called its preferred action
111
resultSet.add (indexJSPFo);
112         
113         FileObject dir = FileUtil.toFileObject(dirF);
114
115         Integer JavaDoc index = (Integer JavaDoc) wiz.getProperty(PROP_NAME_INDEX);
116         if(index != null) {
117             FoldersListSettings.getDefault().setNewProjectCount(index.intValue());
118         }
119         wiz.putProperty(WizardProperties.NAME, null); // reset project name
120

121         Project earProject = (Project) wiz.getProperty(WizardProperties.EAR_APPLICATION);
122         
123         WebProject createdWebProject = (WebProject) ProjectManager.getDefault().findProject(dir);
124         if (earProject != null && createdWebProject != null) {
125             Ear ear = Ear.getEar(earProject.getProjectDirectory());
126             if (ear != null) {
127                 ear.addWebModule(createdWebProject.getAPIWebModule());
128             }
129         }
130
131         //remember last used server
132
FoldersListSettings.getDefault().setLastUsedServer(servInstID);
133     
134         // save last project location
135
dirF = (dirF != null) ? dirF.getParentFile() : null;
136         if (dirF != null && dirF.exists()) {
137             ProjectChooser.setProjectsFolder (dirF);
138         }
139
140         resultSet.add(dir);
141
142         //add framework extensions
143
List JavaDoc selectedFrameworks = (List JavaDoc) wiz.getProperty(WizardProperties.FRAMEWORKS);
144         if (selectedFrameworks != null){
145             handle.progress(NbBundle.getMessage(NewWebProjectWizardIterator.class, "LBL_NewWebProjectWizardIterator_WizardProgress_AddingFrameworks"), 3);
146             for(int i = 0; i < selectedFrameworks.size(); i++) {
147                 Object JavaDoc o = ((WebFrameworkProvider) selectedFrameworks.get(i)).extend(createdWebProject.getAPIWebModule());
148                 if (o != null && o instanceof Set JavaDoc)
149                     resultSet.addAll((Set JavaDoc)o);
150             }
151         }
152         
153         handle.progress(NbBundle.getMessage(NewWebProjectWizardIterator.class, "LBL_NewWebProjectWizardIterator_WizardProgress_PreparingToOpen"), 4);
154
155         // Returning set of FileObject of project diretory.
156
// Project will be open and set as main
157
return resultSet;
158     }
159     
160     private transient int index;
161     private transient int panelsCount;
162     private transient WizardDescriptor.Panel[] panels;
163     private transient WizardDescriptor wiz;
164     
165     public void initialize(WizardDescriptor wiz) {
166         this.wiz = wiz;
167         index = 0;
168
169     if (WebFrameworkSupport.getFrameworkProviders().size() > 0)
170         //standard panels + configurable framework panel
171
panels = new WizardDescriptor.Panel[] {
172         new PanelConfigureProject(),
173         new PanelSupportedFrameworks()
174         };
175     else
176         //no framework available, don't show framework panel
177
panels = new WizardDescriptor.Panel[] {
178         new PanelConfigureProject(),
179         };
180         panelsCount = panels.length;
181         
182         // Make sure list of steps is accurate.
183
String JavaDoc[] steps = createSteps();
184         for (int i = 0; i < steps.length; i++) {
185             Component JavaDoc c = panels[i].getComponent();
186             if (c instanceof JComponent JavaDoc) { // assume Swing components
187
JComponent JavaDoc jc = (JComponent JavaDoc)c;
188                 // Step #.
189
jc.putClientProperty("WizardPanel_contentSelectedIndex", Integer.valueOf(i)); // NOI18N
190
// Step name (actually the whole list for reference).
191
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
192
}
193         }
194     }
195     
196     public void uninitialize(WizardDescriptor wiz) {
197         if (this.wiz != null) {
198             this.wiz.putProperty(WizardProperties.PROJECT_DIR,null);
199             this.wiz.putProperty(WizardProperties.NAME,null);
200             this.wiz = null;
201         }
202         panels = null;
203     }
204     
205     public String JavaDoc name() {
206         return MessageFormat.format(NbBundle.getMessage(NewWebProjectWizardIterator.class, "LBL_WizardStepsCount"), Integer.toString(index + 1), Integer.toString(panels.length)); //NOI18N
207
}
208     
209     public boolean hasNext() {
210         return index < panelsCount - 1;
211     }
212     
213     public boolean hasPrevious() {
214         return index > 0;
215     }
216     
217     public void nextPanel() {
218         if (!hasNext()) throw new NoSuchElementException JavaDoc();
219         index++;
220     }
221     
222     public void previousPanel() {
223         if (!hasPrevious()) throw new NoSuchElementException JavaDoc();
224         index--;
225     }
226     
227     public WizardDescriptor.Panel current() {
228         return panels[index];
229     }
230     
231     // If nothing unusual changes in the middle of the wizard, simply:
232
public final void addChangeListener(ChangeListener JavaDoc l) {}
233     public final void removeChangeListener(ChangeListener JavaDoc l) {}
234     
235     // helper methods, finds indexJSP's FileObject
236
private FileObject getIndexJSPFO(FileObject webRoot, String JavaDoc indexJSP) {
237         // replace '.' with '/'
238
indexJSP = indexJSP.replace ('.', '/'); // NOI18N
239

240         // ignore unvalid mainClass ???
241

242         return webRoot.getFileObject (indexJSP, "jsp"); // NOI18N
243
}
244 }
245
Popular Tags