KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > ui > wizards > NewAppClientProjectWizardIterator


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.clientproject.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.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.api.ejbjar.Ear;
35 import org.netbeans.modules.j2ee.clientproject.AppClientProject;
36 import org.netbeans.modules.j2ee.clientproject.api.AppClientProjectGenerator;
37 import org.netbeans.modules.j2ee.clientproject.ui.FoldersListSettings;
38 import org.netbeans.spi.project.support.ant.AntProjectHelper;
39 import org.netbeans.spi.project.ui.support.ProjectChooser;
40 import org.openide.ErrorManager;
41 import org.openide.WizardDescriptor;
42 import org.openide.filesystems.FileObject;
43 import org.openide.filesystems.FileUtil;
44 import org.openide.util.NbBundle;
45
46 /**
47  * Wizard to create a new Application Client project.
48  */

49 public class NewAppClientProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator {
50     
51     static final String JavaDoc PROP_NAME_INDEX = "nameIndex"; //NOI18N
52

53     private static final long serialVersionUID = 1L;
54     
55     public static NewAppClientProjectWizardIterator library() {
56         return new NewAppClientProjectWizardIterator();
57     }
58     
59     private WizardDescriptor.Panel[] createPanels() {
60         return new WizardDescriptor.Panel[] {
61             new PanelConfigureProject()
62         };
63     }
64     
65     private String JavaDoc[] createSteps() {
66         return new String JavaDoc[] {
67             NbBundle.getMessage(NewAppClientProjectWizardIterator.class,"LAB_ConfigureProject"),
68         };
69     }
70     
71     
72     public Set JavaDoc<FileObject> instantiate() throws IOException JavaDoc {
73         assert false : "This method cannot be called if the class implements WizardDescriptor.ProgressInstantiatingIterator.";
74         return null;
75     }
76         
77     public Set JavaDoc<FileObject> instantiate(ProgressHandle handle) throws IOException JavaDoc {
78         handle.start(3);
79         handle.progress(NbBundle.getMessage(NewAppClientProjectWizardIterator.class, "LBL_NewAppClientProjectWizardIterator_WizardProgress_CreatingProject"), 1);
80         
81         Set JavaDoc<FileObject> resultSet = new HashSet JavaDoc<FileObject>();
82         File JavaDoc dirF = (File JavaDoc)wiz.getProperty(WizardProperties.PROJECT_DIR);
83         if (dirF != null) {
84             dirF = FileUtil.normalizeFile(dirF);
85         }
86         String JavaDoc name = (String JavaDoc)wiz.getProperty(WizardProperties.NAME);
87         String JavaDoc mainClass = (String JavaDoc)wiz.getProperty(WizardProperties.MAIN_CLASS);
88         
89         String JavaDoc serverInstanceID = (String JavaDoc) wiz.getProperty(WizardProperties.SERVER_INSTANCE_ID);
90         String JavaDoc j2eeLevel = (String JavaDoc) wiz.getProperty(WizardProperties.J2EE_LEVEL);
91         
92         AntProjectHelper h = AppClientProjectGenerator.createProject(dirF, name, mainClass, j2eeLevel, serverInstanceID);
93         handle.progress(2);
94         
95         if (mainClass != null && mainClass.length() > 0) {
96             try {
97                 //String sourceRoot = "src"; //(String)j2seProperties.get (J2SEProjectProperties.SRC_DIR);
98
FileObject sourcesRoot = h.getProjectDirectory().getFileObject("src/java"); //NOI18N
99
FileObject mainClassFo = getMainClassFO(sourcesRoot, mainClass);
100                 assert mainClassFo != null : "sourcesRoot: " + sourcesRoot + ", mainClass: " + mainClass; //NOI18N
101
// Returning FileObject of main class, will be called its preferred action
102
resultSet.add(mainClassFo);
103             } catch (Exception JavaDoc x) {
104                 ErrorManager.getDefault().notify(x);
105             }
106         }
107         FileObject dir = FileUtil.toFileObject(dirF);
108         
109         Project earProject = (Project) wiz.getProperty(WizardProperties.EAR_APPLICATION);
110         AppClientProject createdAppClientProject = (AppClientProject) ProjectManager.getDefault().findProject(dir);
111         if (earProject != null && createdAppClientProject != null) {
112             Ear ear = Ear.getEar(earProject.getProjectDirectory());
113             if (ear != null) {
114                 ear.addCarModule(createdAppClientProject.getAPICar());
115             }
116         }
117         
118         // remember last used server
119
FoldersListSettings.getDefault().setLastUsedServer(serverInstanceID);
120         
121         // downgrade the Java platform or src level to 1.4
122
String JavaDoc platformName = (String JavaDoc)wiz.getProperty(WizardProperties.JAVA_PLATFORM);
123         String JavaDoc sourceLevel = (String JavaDoc)wiz.getProperty(WizardProperties.SOURCE_LEVEL);
124         if (platformName != null || sourceLevel != null) {
125             AppClientProjectGenerator.setPlatform(h, platformName, sourceLevel);
126         }
127         
128         // Returning FileObject of project diretory.
129
// Project will be open and set as main
130
Integer JavaDoc index = (Integer JavaDoc) wiz.getProperty(PROP_NAME_INDEX);
131         FoldersListSettings.getDefault().setNewApplicationCount(index.intValue());
132         resultSet.add(dir);
133         
134         dirF = (dirF != null) ? dirF.getParentFile() : null;
135         if (dirF != null && dirF.exists()) {
136             ProjectChooser.setProjectsFolder(dirF);
137         }
138         
139         handle.progress(NbBundle.getMessage(NewAppClientProjectWizardIterator.class, "LBL_NewAppClientProjectWizardIterator_WizardProgress_PreparingToOpen"), 3);
140         
141         return resultSet;
142     }
143     
144     
145     private transient int index;
146     private transient WizardDescriptor.Panel[] panels;
147     private transient WizardDescriptor wiz;
148     
149     public void initialize(WizardDescriptor wiz) {
150         this.wiz = wiz;
151         index = 0;
152         panels = createPanels();
153         // Make sure list of steps is accurate.
154
String JavaDoc[] steps = createSteps();
155         for (int i = 0; i < panels.length; i++) {
156             Component JavaDoc c = panels[i].getComponent();
157             if (steps[i] == null) {
158                 // Default step name to component name of panel.
159
// Mainly useful for getting the name of the target
160
// chooser to appear in the list of steps.
161
steps[i] = c.getName();
162             }
163             if (c instanceof JComponent JavaDoc) { // assume Swing components
164
JComponent JavaDoc jc = (JComponent JavaDoc)c;
165                 // Step #.
166
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
167
// Step name (actually the whole list for reference).
168
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
169
}
170         }
171         //set the default values of the sourceRoot and the testRoot properties
172
this.wiz.putProperty(WizardProperties.SOURCE_ROOT, new File JavaDoc[0]);
173         this.wiz.putProperty(WizardProperties.TEST_ROOT, new File JavaDoc[0]);
174     }
175     
176     public void uninitialize(WizardDescriptor wiz) {
177         if (this.wiz != null) { // #74316
178
this.wiz.putProperty(WizardProperties.PROJECT_DIR, null);
179             this.wiz.putProperty(WizardProperties.NAME, null);
180             this.wiz.putProperty(WizardProperties.MAIN_CLASS, null);
181             this.wiz = null;
182         }
183         panels = null;
184     }
185     
186     public String JavaDoc name() {
187         return MessageFormat.format(NbBundle.getMessage(NewAppClientProjectWizardIterator.class,"LAB_IteratorName"),
188                 new Object JavaDoc[] {new Integer JavaDoc(index + 1), new Integer JavaDoc(panels.length) });
189     }
190     
191     public boolean hasNext() {
192         return index < panels.length - 1;
193     }
194     public boolean hasPrevious() {
195         return index > 0;
196     }
197     public void nextPanel() {
198         if (!hasNext()) throw new NoSuchElementException JavaDoc();
199         index++;
200     }
201     public void previousPanel() {
202         if (!hasPrevious()) throw new NoSuchElementException JavaDoc();
203         index--;
204     }
205     public WizardDescriptor.Panel current() {
206         return panels[index];
207     }
208     
209     // If nothing unusual changes in the middle of the wizard, simply:
210
public final void addChangeListener(ChangeListener JavaDoc l) {}
211     public final void removeChangeListener(ChangeListener JavaDoc l) {}
212     
213     // helper methods, finds mainclass's FileObject
214
private FileObject getMainClassFO(FileObject sourcesRoot, String JavaDoc mainClass) {
215         // replace '.' with '/'
216
mainClass = mainClass.replace('.', '/');
217         
218         // ignore unvalid mainClass ???
219

220         return sourcesRoot.getFileObject(mainClass+ ".java"); // NOI18N
221
}
222     
223     static String JavaDoc getPackageName(String JavaDoc displayName) {
224         StringBuffer JavaDoc builder = new StringBuffer JavaDoc();
225         boolean firstLetter = true;
226         for (int i=0; i< displayName.length(); i++) {
227             char c = displayName.charAt(i);
228             if ((!firstLetter && Character.isJavaIdentifierPart(c)) || (firstLetter && Character.isJavaIdentifierStart(c))) {
229                 firstLetter = false;
230                 if (Character.isUpperCase(c)) {
231                     c = Character.toLowerCase(c);
232                 }
233                 builder.append(c);
234             }
235         }
236         return builder.length() == 0 ? NbBundle.getMessage(NewAppClientProjectWizardIterator.class,"TXT_DefaultPackageName") : builder.toString();
237     }
238     
239 }
240
Popular Tags