1 19 20 package org.netbeans.modules.j2ee.earproject.ui.wizards; 21 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.text.MessageFormat ; 26 import java.util.LinkedHashSet ; 27 import java.util.NoSuchElementException ; 28 import java.util.Set ; 29 import javax.swing.JComponent ; 30 import javax.swing.event.ChangeListener ; 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 61 public class NewEarProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator { 62 63 private static final long serialVersionUID = 1L; 64 65 static final String PROP_NAME_INDEX = "nameIndex"; 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 [] createSteps() { 80 return new String [] { 81 NbBundle.getMessage(NewEarProjectWizardIterator.class, "LBL_NWP1_ProjectTitleName") 82 }; 83 } 84 85 public Set instantiate() throws IOException { 86 assert false : "This method cannot be called if the class implements WizardDescriptor.ProgressInstantiatingIterator."; 87 return null; 88 } 89 90 public Set <FileObject> instantiate(ProgressHandle handle) throws IOException { 91 handle.start(9); 92 handle.progress(NbBundle.getMessage(NewEarProjectWizardIterator.class, "LBL_NewEarProjectWizardIterator_WizardProgress_CreatingProject"), 1); 93 94 File dirF = (File ) wiz.getProperty(WizardProperties.PROJECT_DIR); 95 String name = (String ) wiz.getProperty(WizardProperties.NAME); 96 String serverInstanceID = (String ) wiz.getProperty(WizardProperties.SERVER_INSTANCE_ID); 97 String j2eeLevel = (String ) wiz.getProperty(WizardProperties.J2EE_LEVEL); 98 Boolean createWAR = (Boolean ) wiz.getProperty(WizardProperties.CREATE_WAR); 100 String warName = null; 101 if (createWAR.booleanValue()) { 102 warName = (String ) wiz.getProperty(WizardProperties.WAR_NAME); 103 } 104 Boolean createJAR = (Boolean ) wiz.getProperty(WizardProperties.CREATE_JAR); 105 String ejbJarName = null; 106 if (createJAR.booleanValue()) { 107 ejbJarName = (String ) wiz.getProperty(WizardProperties.JAR_NAME); 108 } 109 Boolean createCAR = (Boolean ) wiz.getProperty(WizardProperties.CREATE_CAR); 110 String carName = null; 111 String mainClass = null; 112 if (createCAR.booleanValue()) { 113 carName = (String ) wiz.getProperty(WizardProperties.CAR_NAME); 114 mainClass = (String ) wiz.getProperty(WizardProperties.MAIN_CLASS); 115 } 116 String platformName = (String )wiz.getProperty(WizardProperties.JAVA_PLATFORM); 117 String sourceLevel = (String )wiz.getProperty(WizardProperties.SOURCE_LEVEL); 118 FoldersListSettings.getDefault().setLastUsedServer(serverInstanceID); 120 return testableInstantiate(dirF,name,j2eeLevel, serverInstanceID, warName, 121 ejbJarName, carName, mainClass, platformName, sourceLevel, handle); 122 } 123 124 125 static Set <FileObject> testableInstantiate(File dirF, String name, String j2eeLevel, 126 String serverInstanceID, String warName, String ejbJarName, String carName, 127 String mainClass, String platformName, String sourceLevel, ProgressHandle handle) throws IOException { 128 Set <FileObject> resultSet = new LinkedHashSet <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 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 webAppDir = new File (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); 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 carDir = new File (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 ejbJarDir = new File (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); } 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 dirF) { 224 File 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 warName, 231 final String carName, final EarProjectProperties epp) { 232 String clientModuleURI = null; 233 if (warName != null) { 234 String [] webURIs = epp.getWebUris(); 236 assert webURIs.length == 1 : "Exactly one application client " + 237 "may be generated during creation. Is: " + webURIs.length; clientModuleURI = webURIs[0]; 239 } else if (carName != null) { 240 String [] appClientURIs = epp.getAppClientUris(); 242 assert appClientURIs.length == 1 : "Exactly one application client " + 243 "may be generated during creation. Is: " + appClientURIs.length; 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 String [] steps = createSteps(); 258 for (int i = 0; i < panels.length; i++) { 259 Component c = panels[i].getComponent(); 260 if (steps[i] == null) { 261 steps[i] = c.getName(); 265 } 266 if (c instanceof JComponent ) { JComponent jc = (JComponent )c; 268 jc.putClientProperty("WizardPanel_contentSelectedIndex", i); jc.putClientProperty("WizardPanel_contentData", steps); } 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 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 (); 302 } 303 index++; 304 } 305 306 public void previousPanel() { 307 if (!hasPrevious()) { 308 throw new NoSuchElementException (); 309 } 310 index--; 311 } 312 313 public WizardDescriptor.Panel current() { 314 return panels[index]; 315 } 316 317 public final void addChangeListener(ChangeListener l) {} 319 public final void removeChangeListener(ChangeListener l) {} 320 321 FileObject getIndexJSPFO(FileObject webRoot, String indexJSP) { 323 return webRoot.getFileObject(indexJSP.replace('.', '/'), "jsp"); } 326 327 } 328 | Popular Tags |