KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbfreeform > ui > NewEJBFreeformProjectWizardIterator


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.ejbfreeform.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.math.BigDecimal JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.NoSuchElementException JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.swing.JComponent JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.api.project.ProjectManager;
36 import org.netbeans.modules.ant.freeform.spi.support.NewFreeformProjectSupport;
37 import org.netbeans.modules.ant.freeform.spi.support.Util;
38 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
39 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
40 import org.netbeans.modules.java.freeform.spi.support.NewJavaFreeformProjectSupport;
41 import org.netbeans.modules.j2ee.ejbfreeform.EJBProjectGenerator;
42 import org.netbeans.modules.j2ee.ejbfreeform.EJBProjectNature;
43 import org.netbeans.spi.project.AuxiliaryConfiguration;
44 import org.netbeans.spi.project.support.ant.AntProjectHelper;
45 import org.netbeans.spi.project.ui.support.ProjectChooser;
46 import org.openide.WizardDescriptor;
47 import org.openide.filesystems.FileObject;
48 import org.openide.filesystems.FileUtil;
49 import org.openide.util.NbBundle;
50
51
52 /**
53  * @author David Konecny, Radko Najman
54  */

55 public class NewEJBFreeformProjectWizardIterator implements WizardDescriptor.InstantiatingIterator {
56
57     // EJB sources
58
public static final String JavaDoc PROP_EJB_EJBMODULES = "ejbModules"; // <List> NOI18N
59
public static final String JavaDoc PROP_EJB_SOURCE_FOLDERS = "ejbSourceFolders"; // <List> NOI18N
60
public static final String JavaDoc PROP_EJB_RESOURCE_FOLDERS = "ejbResourceFolders"; // <List> NOI18N
61
public static final String JavaDoc SERVER_ID = "serverID"; //NOI18N
62
public static final String JavaDoc J2EE_LEVEL = "j2eeLevel"; //NOI18N
63
public static final String JavaDoc J2EE_SERVER_TYPE = "j2eeServerType"; //NOI18N
64

65     private static final long serialVersionUID = 1L;
66     
67     private transient int index;
68     private transient WizardDescriptor.Panel[] panels;
69     private transient WizardDescriptor wiz;
70     
71     public NewEJBFreeformProjectWizardIterator() {
72     }
73     
74     private WizardDescriptor.Panel[] createPanels () {
75         List JavaDoc l = new ArrayList JavaDoc();
76         List JavaDoc extraTargets = new ArrayList JavaDoc();
77         extraTargets.add(EJBProjectNature.getExtraTarget());
78         l.add(NewFreeformProjectSupport.createBasicProjectInfoWizardPanel());
79         l.add(NewFreeformProjectSupport.createTargetMappingWizardPanel(extraTargets));
80         l.add(new EJBLocationsWizardPanel());
81         l.addAll(Arrays.asList(NewJavaFreeformProjectSupport.createJavaPanels()));
82         return (WizardDescriptor.Panel[])l.toArray(new WizardDescriptor.Panel[l.size()]);
83     }
84     
85     public Set JavaDoc/*<FileObject>*/ instantiate () throws IOException JavaDoc {
86         final WizardDescriptor wiz = this.wiz;
87         final IOException JavaDoc[] ioe = new IOException JavaDoc[1];
88         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
89             public void run() {
90                 try {
91                     AntProjectHelper helper = NewFreeformProjectSupport.instantiateBasicProjectInfoWizardPanel(wiz);
92                     NewFreeformProjectSupport.instantiateTargetMappingWizardPanel(helper, wiz);
93                     NewJavaFreeformProjectSupport.instantiateJavaPanels(helper, wiz);
94
95                     List JavaDoc ejbSources = (List JavaDoc)wiz.getProperty(PROP_EJB_SOURCE_FOLDERS);
96                     AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(helper);
97                     EJBProjectGenerator.putEJBSourceFolder(helper, ejbSources);
98                     
99                     List JavaDoc resources = (List JavaDoc) wiz.getProperty(PROP_EJB_RESOURCE_FOLDERS);
100                     EJBProjectGenerator.putResourceFolder(helper, resources);
101         
102                     String JavaDoc j2eeLevel = (String JavaDoc) wiz.getProperty(J2EE_LEVEL);
103                     EJBProjectGenerator.putJ2EELevel(helper, j2eeLevel);
104                     
105                     String JavaDoc serverID = (String JavaDoc) wiz.getProperty(SERVER_ID);
106                     EJBProjectGenerator.putServerID(helper, serverID);
107                     
108                     List JavaDoc ejbModules = (List JavaDoc) wiz.getProperty(PROP_EJB_EJBMODULES);
109                     if (ejbModules != null) {
110                         EJBProjectGenerator.putEJBModules (helper, aux, ejbModules);
111                     }
112                     
113                     Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory());
114                     
115                     org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(p)[0];
116                     if (ejbModule != null) {
117                         FileObject ejbJarFile = ejbModule.getDeploymentDescriptor();
118                         EjbJar dd = DDProvider.getDefault().getMergedDDRoot(ejbModule.getMetadataUnit());
119                         // #82897: putEJBNodeView() is called here because Enterprise Beans
120
// node is supposed to be shown only for non EJB 3.0 projects
121
if (!new BigDecimal JavaDoc(EjbJar.VERSION_3_0).equals(dd.getVersion())) {
122                             EJBProjectGenerator.putEJBNodeView(helper, ejbSources);
123                         }
124                         boolean write = false;
125                         // set the DD display name if there is none
126
String JavaDoc dispName = dd.getDefaultDisplayName();
127                         if ((null == dispName || dispName.trim().length() == 0) && (ejbJarFile != null)) {
128                             dd.setDisplayName(helper.getProjectDirectory().getName());
129                             write = true;
130                         }
131                         // update the DD to 2.1 if it is 2.0 and the user chose J2EE 1.4
132
if (j2eeLevel.equals("1.4") && !new BigDecimal JavaDoc(EjbJar.VERSION_2_1).equals(dd.getVersion())) { // NOI18N
133
dd.setVersion(new BigDecimal JavaDoc(EjbJar.VERSION_2_1));
134                             write = true;
135                         }
136                         // probably update the DD to 3.0 if it's 1.5 and no DD specified - XXX after #82231 is fixed!
137
// if (j2eeLevel.equals("1.5") && ejbJarFile != null) { // NOI18N
138
// dd.setVersion(new BigDecimal(EjbJar.VERSION_3_0));
139
// write = true;
140
// }
141
// ejbJarFile can be null for Java EE 5
142
if (write && ejbJarFile != null) {
143                             dd.write(ejbJarFile);
144                         }
145                     }
146                     
147                     ProjectManager.getDefault().saveProject(p);
148                 } catch (IOException JavaDoc e) {
149                     ioe[0] = e;
150                     return;
151                 }
152             }});
153         if (ioe[0] != null) {
154             throw ioe[0];
155         }
156         File JavaDoc nbProjectFolder = (File JavaDoc)wiz.getProperty(NewFreeformProjectSupport.PROP_PROJECT_FOLDER);
157         Set JavaDoc resultSet = new HashSet JavaDoc();
158         resultSet.add(FileUtil.toFileObject(nbProjectFolder));
159         File JavaDoc f = nbProjectFolder.getParentFile();
160         if (f != null) {
161             ProjectChooser.setProjectsFolder(f);
162         }
163         return resultSet;
164     }
165     
166         
167     public void initialize(WizardDescriptor wiz) {
168         this.wiz = wiz;
169         index = 0;
170         panels = createPanels();
171         
172         List JavaDoc l = new ArrayList JavaDoc();
173         for (int i = 0; i < panels.length; i++) {
174             Component JavaDoc c = panels[i].getComponent();
175             assert c instanceof JComponent JavaDoc;
176             JComponent JavaDoc jc = (JComponent JavaDoc)c;
177             l.add(jc.getName());
178         }
179         String JavaDoc[] steps = (String JavaDoc[])l.toArray(new String JavaDoc[l.size()]);
180         for (int i = 0; i < panels.length; i++) {
181             Component JavaDoc c = panels[i].getComponent();
182             assert c instanceof JComponent JavaDoc;
183             JComponent JavaDoc jc = (JComponent JavaDoc)c;
184             // Step #.
185
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
186
// Step name (actually the whole list for reference).
187
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
188
// set title
189
jc.putClientProperty ("NewProjectWizard_Title", NbBundle.getMessage (NewEJBFreeformProjectWizardIterator.class, "TXT_NewEJBFreeformProjectWizardIterator_NewProjectWizardTitle")); // NOI18N
190
}
191     }
192     
193     public void uninitialize(WizardDescriptor wiz) {
194         NewFreeformProjectSupport.uninitializeBasicProjectInfoWizardPanel(wiz);
195         NewFreeformProjectSupport.uninitializeTargetMappingWizardPanel(wiz);
196         NewJavaFreeformProjectSupport.uninitializeJavaPanels(wiz);
197         wiz.putProperty(PROP_EJB_SOURCE_FOLDERS, null);
198         wiz.putProperty(PROP_EJB_RESOURCE_FOLDERS, null);
199         wiz.putProperty(PROP_EJB_EJBMODULES, null);
200         wiz.putProperty(SERVER_ID, null);
201         wiz.putProperty(J2EE_LEVEL, null);
202         wiz.putProperty(J2EE_SERVER_TYPE, null);
203         this.wiz = null;
204         panels = null;
205     }
206     
207     public String JavaDoc name() {
208         return NbBundle.getMessage(NewEJBFreeformProjectWizardIterator.class, "TXT_NewEJBFreeformProjectWizardIterator_TitleFormat",
209             new Object JavaDoc[] {new Integer JavaDoc (index + 1), new Integer JavaDoc (panels.length)} );
210     }
211     
212     public boolean hasNext() {
213         if (!NewJavaFreeformProjectSupport.enableNextButton(current())) {
214             return false;
215         }
216         return index < panels.length - 1;
217     }
218     public boolean hasPrevious() {
219         return index > 0;
220     }
221     public void nextPanel() {
222         if (!hasNext()) throw new NoSuchElementException JavaDoc();
223         index++;
224     }
225     public void previousPanel() {
226         if (!hasPrevious()) throw new NoSuchElementException JavaDoc();
227         index--;
228     }
229     public WizardDescriptor.Panel current () {
230         return panels[index];
231     }
232     
233     // If nothing unusual changes in the middle of the wizard, simply:
234
public final void addChangeListener(ChangeListener JavaDoc l) {}
235     public final void removeChangeListener(ChangeListener JavaDoc l) {}
236
237     
238 }
239
Popular Tags