KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Dialog JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.event.ChangeEvent JavaDoc;
35 import javax.swing.event.ChangeListener JavaDoc;
36 import org.netbeans.api.progress.ProgressHandle;
37
38 import org.openide.DialogDescriptor;
39 import org.openide.DialogDisplayer;
40 import org.openide.WizardDescriptor;
41 import org.openide.WizardValidationException;
42 import org.openide.filesystems.FileObject;
43 import org.openide.filesystems.FileUtil;
44 import org.openide.util.HelpCtx;
45 import org.openide.util.NbBundle;
46
47 import org.netbeans.api.project.Project;
48 import org.netbeans.api.project.ProjectManager;
49 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
50
51 import org.netbeans.modules.j2ee.api.ejbjar.Ear;
52 import org.netbeans.modules.web.api.webmodule.WebModule;
53 import org.netbeans.modules.web.project.ProjectWebModule;
54 import org.netbeans.modules.web.project.api.WebProjectUtilities;
55 import org.netbeans.modules.web.project.WebProject;
56 import org.netbeans.modules.web.project.api.WebProjectCreateData;
57 import org.netbeans.modules.web.project.ui.FoldersListSettings;
58
59 /**
60  * Wizard to create a new Web project for an existing web module.
61  * @author Pavel Buzek, Radko Najman
62  */

63 public class ImportWebProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator {
64     
65     private static final long serialVersionUID = 1L;
66     private String JavaDoc buildfileName = GeneratedFilesHelper.BUILD_XML_PATH;
67
68     private String JavaDoc moduleLoc;
69     
70     /** Create a new wizard iterator. */
71     public ImportWebProjectWizardIterator () {}
72     
73     private WizardDescriptor.Panel[] createPanels() {
74         return new WizardDescriptor.Panel[] {
75             new ImportWebProjectWizardIterator.ThePanel(),
76             new PanelSourceFolders.Panel()
77         };
78     }
79     
80     private String JavaDoc[] createSteps() {
81         return new String JavaDoc[] {
82             NbBundle.getMessage(ImportWebProjectWizardIterator.class, "LBL_IW_Step1"), //NOI18N
83
NbBundle.getMessage(ImportWebProjectWizardIterator.class, "LBL_IW_Step2") //NOI18N
84
};
85     }
86     
87     public Set JavaDoc instantiate() throws IOException JavaDoc {
88         assert false : "This method cannot be called if the class implements WizardDescriptor.ProgressInstantiatingIterator.";
89         return null;
90     }
91         
92     public Set JavaDoc<FileObject> instantiate(ProgressHandle handle) throws IOException JavaDoc {
93         handle.start(3);
94         handle.progress(NbBundle.getMessage(ImportWebProjectWizardIterator.class, "LBL_NewWebProjectWizardIterator_WizardProgress_CreatingProject"), 1);
95         
96         Set JavaDoc resultSet = new HashSet JavaDoc();
97         
98         File JavaDoc dirF = (File JavaDoc) wiz.getProperty(WizardProperties.PROJECT_DIR);
99         if (dirF != null) {
100             dirF = FileUtil.normalizeFile(dirF);
101         }
102         
103         File JavaDoc dirSrcF = (File JavaDoc) wiz.getProperty (WizardProperties.SOURCE_ROOT);
104         
105         String JavaDoc name = (String JavaDoc) wiz.getProperty(WizardProperties.NAME);
106         String JavaDoc contextPath = (String JavaDoc) wiz.getProperty(WizardProperties.CONTEXT_PATH);
107         String JavaDoc docBaseName = (String JavaDoc) wiz.getProperty(WizardProperties.DOC_BASE);
108         File JavaDoc[] sourceFolders = (File JavaDoc[]) wiz.getProperty(WizardProperties.JAVA_ROOT);
109         File JavaDoc[] testFolders = (File JavaDoc[]) wiz.getProperty(WizardProperties.TEST_ROOT);
110         String JavaDoc libName = (String JavaDoc) wiz.getProperty(WizardProperties.LIB_FOLDER);
111         String JavaDoc serverInstanceID = (String JavaDoc) wiz.getProperty(WizardProperties.SERVER_INSTANCE_ID);
112         String JavaDoc j2eeLevel = (String JavaDoc) wiz.getProperty(WizardProperties.J2EE_LEVEL);
113
114         FileObject wmFO = FileUtil.toFileObject (dirSrcF);
115         assert wmFO != null : "No such dir on disk: " + dirSrcF;
116         assert wmFO.isFolder() : "Not really a dir: " + dirSrcF;
117         
118         FileObject docBase;
119         FileObject libFolder;
120         if (docBaseName == null || docBaseName.equals("")) //NOI18N
121
docBase = FileSearchUtility.guessDocBase(wmFO);
122         else {
123             File JavaDoc f = new File JavaDoc(docBaseName);
124             docBase = FileUtil.toFileObject(f);
125         }
126 // if (javaRootName == null || javaRootName.equals("")) //NOI18N
127
// javaRoot = guessJavaRoot(wmFO);
128
// else {
129
// File f = new File(javaRootName);
130
// javaRoot = FileUtil.toFileObject(f);
131
// }
132
if (libName == null || libName.equals("")) //NOI18N
133
libFolder = FileSearchUtility.guessLibrariesFolder(wmFO);
134         else {
135             File JavaDoc f = new File JavaDoc(libName);
136             libFolder = FileUtil.toFileObject(f);
137         }
138
139         if(j2eeLevel == null) {
140             j2eeLevel = WebModule.J2EE_14_LEVEL;
141         }
142
143         String JavaDoc buildfile = getBuildfile();
144         
145         WebProjectCreateData createData = new WebProjectCreateData();
146         createData.setProjectDir(dirF);
147         createData.setName(name);
148         createData.setWebModuleFO(wmFO);
149         createData.setSourceFolders(sourceFolders);
150         createData.setTestFolders(testFolders);
151         createData.setDocBase(docBase);
152         createData.setLibFolder(libFolder);
153         createData.setJavaEEVersion(j2eeLevel);
154         createData.setServerInstanceID(serverInstanceID);
155         createData.setBuildfile(buildfile);
156         createData.setJavaPlatformName((String JavaDoc) wiz.getProperty(WizardProperties.JAVA_PLATFORM));
157         createData.setSourceLevel((String JavaDoc) wiz.getProperty(WizardProperties.SOURCE_LEVEL));
158         WebProjectUtilities.importProject(createData);
159         handle.progress(2);
160         
161         FileObject dir = FileUtil.toFileObject(dirF);
162         
163         resultSet.add(dir);
164         
165         Project earProject = (Project) wiz.getProperty(WizardProperties.EAR_APPLICATION);
166         WebProject createdWebProject = (WebProject) ProjectManager.getDefault().findProject(dir);
167         if (earProject != null && createdWebProject != null) {
168             Ear ear = Ear.getEar(earProject.getProjectDirectory());
169             if (ear != null) {
170                 ear.addWebModule(createdWebProject.getAPIWebModule());
171             }
172         }
173
174         Project p = ProjectManager.getDefault().findProject(dir);
175         ProjectWebModule wm = (ProjectWebModule) p.getLookup ().lookup (ProjectWebModule.class);
176         if (wm != null) //should not be null
177
wm.setContextPath(contextPath);
178
179         Integer JavaDoc index = (Integer JavaDoc) wiz.getProperty(NewWebProjectWizardIterator.PROP_NAME_INDEX);
180         if (index != null) {
181             FoldersListSettings.getDefault().setNewProjectCount(index.intValue());
182         }
183         wiz.putProperty(WizardProperties.NAME, null); // reset project name
184

185         //remember last used server
186
FoldersListSettings.getDefault().setLastUsedServer(serverInstanceID);
187
188         handle.progress(NbBundle.getMessage(ImportWebProjectWizardIterator.class, "LBL_NewWebProjectWizardIterator_WizardProgress_PreparingToOpen"), 3);
189         
190         return resultSet;
191     }
192     
193     private String JavaDoc getBuildfile() {
194         return buildfileName;
195     }
196     
197     private void setBuildfile(String JavaDoc name) {
198         buildfileName = name;
199     }
200
201     private transient int index;
202     private transient WizardDescriptor.Panel[] panels;
203     private transient WizardDescriptor wiz;
204
205     public void initialize(WizardDescriptor wiz) {
206         this.wiz = wiz;
207         index = 0;
208         panels = createPanels();
209         // Make sure list of steps is accurate.
210
String JavaDoc[] steps = createSteps();
211         for (int i = 0; i < panels.length; i++) {
212             Component JavaDoc c = panels[i].getComponent();
213             if (steps[i] == null) {
214                 // Default step name to component name of panel.
215
// Mainly useful for getting the name of the target
216
// chooser to appear in the list of steps.
217
steps[i] = c.getName();
218             }
219             if (c instanceof JComponent JavaDoc) { // assume Swing components
220
JComponent JavaDoc jc = (JComponent JavaDoc)c;
221                 jc.putClientProperty("NewProjectWizard_Title", NbBundle.getMessage(ImportWebProjectWizardIterator.class, "TXT_WebExtSources")); // NOI18N
222
// Step #.
223
jc.putClientProperty("WizardPanel_contentSelectedIndex", Integer.valueOf(i)); // NOI18N
224
// Step name (actually the whole list for reference).
225
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
226
}
227         }
228     }
229     public void uninitialize(WizardDescriptor wiz) {
230         this.wiz = null;
231         panels = null;
232     }
233     
234     public String JavaDoc name() {
235         return MessageFormat.format(NbBundle.getMessage(ImportWebProjectWizardIterator.class, "LBL_WizardStepsCount"), Integer.toString(index + 1), Integer.toString(panels.length)); //NOI18N
236
}
237     
238     public boolean hasNext() {
239         return index < panels.length - 1;
240     }
241     public boolean hasPrevious() {
242         return index > 0;
243     }
244     public void nextPanel() {
245         if (!hasNext()) throw new NoSuchElementException JavaDoc();
246         index++;
247     }
248     public void previousPanel() {
249         if (!hasPrevious()) throw new NoSuchElementException JavaDoc();
250         index--;
251     }
252     public WizardDescriptor.Panel current() {
253         return panels[index];
254     }
255     
256     // If nothing unusual changes in the middle of the wizard, simply:
257
public final void addChangeListener(ChangeListener JavaDoc l) {}
258     public final void removeChangeListener(ChangeListener JavaDoc l) {}
259
260     public final class ThePanel implements WizardDescriptor.ValidatingPanel {
261
262         private ImportLocationVisual panel;
263         private WizardDescriptor wizardDescriptor;
264         
265         private ThePanel () {
266         }
267         
268         public java.awt.Component JavaDoc getComponent () {
269             if (panel == null) {
270                 panel = new ImportLocationVisual (this);
271             }
272             return panel;
273         }
274         
275         public HelpCtx getHelp() {
276             return new HelpCtx(ThePanel.class);
277         }
278
279         public boolean isValid () {
280             getComponent();
281             return panel.valid(wizardDescriptor);
282         }
283         
284         private final Set JavaDoc/*<ChangeListener>*/ listeners = new HashSet JavaDoc(1);
285         public void addChangeListener(ChangeListener JavaDoc l) {
286             synchronized (listeners) {
287                 listeners.add(l);
288             }
289         }
290         public void removeChangeListener(ChangeListener JavaDoc l) {
291             synchronized (listeners) {
292                 listeners.remove(l);
293             }
294         }
295         protected void fireChangeEvent() {
296             Iterator JavaDoc it;
297             synchronized (listeners) {
298                 it = new HashSet JavaDoc(listeners).iterator();
299             }
300             ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
301             while (it.hasNext()) {
302                 ((ChangeListener JavaDoc)it.next()).stateChanged(ev);
303             }
304         }
305         public void readSettings (Object JavaDoc settings) {
306             wizardDescriptor = (WizardDescriptor) settings;
307             panel.read(wizardDescriptor);
308
309             // XXX hack, TemplateWizard in final setTemplateImpl() forces new wizard's title
310
// this name is used in NewProjectWizard to modify the title
311
Object JavaDoc substitute = ((JComponent JavaDoc) panel).getClientProperty("NewProjectWizard_Title"); //NOI18N
312
if (substitute != null)
313                 wizardDescriptor.putProperty("NewProjectWizard_Title", substitute); //NOI18N
314
}
315         
316         public void storeSettings (Object JavaDoc settings) {
317             WizardDescriptor d = (WizardDescriptor) settings;
318             panel.store(d);
319             ((WizardDescriptor) d).putProperty ("NewProjectWizard_Title", null); //NOI18N
320

321             moduleLoc = panel.moduleLocationTextField.getText().trim();
322         }
323
324         public void validate() throws WizardValidationException {
325             File JavaDoc dirF = new File JavaDoc(panel.projectLocationTextField.getText());
326             if (!new File JavaDoc(dirF, GeneratedFilesHelper.BUILD_XML_PATH).exists()) {
327                 setBuildfile(GeneratedFilesHelper.BUILD_XML_PATH);
328             } else {
329                 File JavaDoc buildFile = new File JavaDoc(dirF, getBuildfile());
330                 if (buildFile.exists()) {
331                     JButton JavaDoc ok = createButton(
332                             "LBL_IW_Buildfile_OK", "ACS_IW_BuildFileDialog_OKButton_LabelMnemonic", //NOI18N
333
"LBL_IW_BuildFileDialog_OK_LabelMnemonic"); //NOI18N
334
JButton JavaDoc cancel = createButton(
335                             "LBL_IW_Buildfile_Cancel", "ACS_IW_BuildFileDialog_CancelButton_LabelMnemonic", //NOI18N
336
"LBL_IW_BuildFileDialog_Cancel_LabelMnemonic"); //NOI18N
337
final ImportBuildfile ibf = new ImportBuildfile(buildFile, ok);
338                     DialogDescriptor descriptor = new DialogDescriptor(ibf,
339                             NbBundle.getMessage(ImportWebProjectWizardIterator.class, "LBL_IW_BuildfileTitle"), //NOI18N
340
true, new Object JavaDoc[]{ok, cancel}, DialogDescriptor.OK_OPTION, DialogDescriptor.DEFAULT_ALIGN,
341                             null, null);
342                     Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(descriptor);
343                     dialog.setVisible(true);
344                     if (descriptor.getValue() != ok) {
345                         throw new WizardValidationException(panel.projectLocationTextField, "", "");
346                     }
347                     setBuildfile(ibf.getBuildName());
348                 }
349             }
350         }
351
352         private JButton JavaDoc createButton(String JavaDoc labelId, String JavaDoc labelMnemonicId, String JavaDoc mnemonicId) {
353             JButton JavaDoc button = new JButton JavaDoc(NbBundle.getMessage(ImportWebProjectWizardIterator.class, labelId));
354             button.getAccessibleContext().setAccessibleDescription(
355                     NbBundle.getMessage(ImportWebProjectWizardIterator.class, labelMnemonicId));
356             button.setMnemonic(NbBundle.getMessage(ImportWebProjectWizardIterator.class, mnemonicId).charAt(0));
357             return button;
358         }
359
360     }
361     
362 }
363
Popular Tags