1 19 20 package org.netbeans.modules.web.project.ui.wizards; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.text.MessageFormat ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.NoSuchElementException ; 30 import java.util.Set ; 31 32 import javax.swing.JButton ; 33 import javax.swing.JComponent ; 34 import javax.swing.event.ChangeEvent ; 35 import javax.swing.event.ChangeListener ; 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 63 public class ImportWebProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator { 64 65 private static final long serialVersionUID = 1L; 66 private String buildfileName = GeneratedFilesHelper.BUILD_XML_PATH; 67 68 private String moduleLoc; 69 70 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 [] createSteps() { 81 return new String [] { 82 NbBundle.getMessage(ImportWebProjectWizardIterator.class, "LBL_IW_Step1"), NbBundle.getMessage(ImportWebProjectWizardIterator.class, "LBL_IW_Step2") }; 85 } 86 87 public Set instantiate() throws IOException { 88 assert false : "This method cannot be called if the class implements WizardDescriptor.ProgressInstantiatingIterator."; 89 return null; 90 } 91 92 public Set <FileObject> instantiate(ProgressHandle handle) throws IOException { 93 handle.start(3); 94 handle.progress(NbBundle.getMessage(ImportWebProjectWizardIterator.class, "LBL_NewWebProjectWizardIterator_WizardProgress_CreatingProject"), 1); 95 96 Set resultSet = new HashSet (); 97 98 File dirF = (File ) wiz.getProperty(WizardProperties.PROJECT_DIR); 99 if (dirF != null) { 100 dirF = FileUtil.normalizeFile(dirF); 101 } 102 103 File dirSrcF = (File ) wiz.getProperty (WizardProperties.SOURCE_ROOT); 104 105 String name = (String ) wiz.getProperty(WizardProperties.NAME); 106 String contextPath = (String ) wiz.getProperty(WizardProperties.CONTEXT_PATH); 107 String docBaseName = (String ) wiz.getProperty(WizardProperties.DOC_BASE); 108 File [] sourceFolders = (File []) wiz.getProperty(WizardProperties.JAVA_ROOT); 109 File [] testFolders = (File []) wiz.getProperty(WizardProperties.TEST_ROOT); 110 String libName = (String ) wiz.getProperty(WizardProperties.LIB_FOLDER); 111 String serverInstanceID = (String ) wiz.getProperty(WizardProperties.SERVER_INSTANCE_ID); 112 String j2eeLevel = (String ) 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("")) docBase = FileSearchUtility.guessDocBase(wmFO); 122 else { 123 File f = new File (docBaseName); 124 docBase = FileUtil.toFileObject(f); 125 } 126 if (libName == null || libName.equals("")) libFolder = FileSearchUtility.guessLibrariesFolder(wmFO); 134 else { 135 File f = new File (libName); 136 libFolder = FileUtil.toFileObject(f); 137 } 138 139 if(j2eeLevel == null) { 140 j2eeLevel = WebModule.J2EE_14_LEVEL; 141 } 142 143 String 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 ) wiz.getProperty(WizardProperties.JAVA_PLATFORM)); 157 createData.setSourceLevel((String ) 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) wm.setContextPath(contextPath); 178 179 Integer index = (Integer ) wiz.getProperty(NewWebProjectWizardIterator.PROP_NAME_INDEX); 180 if (index != null) { 181 FoldersListSettings.getDefault().setNewProjectCount(index.intValue()); 182 } 183 wiz.putProperty(WizardProperties.NAME, null); 185 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 getBuildfile() { 194 return buildfileName; 195 } 196 197 private void setBuildfile(String 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 String [] steps = createSteps(); 211 for (int i = 0; i < panels.length; i++) { 212 Component c = panels[i].getComponent(); 213 if (steps[i] == null) { 214 steps[i] = c.getName(); 218 } 219 if (c instanceof JComponent ) { JComponent jc = (JComponent )c; 221 jc.putClientProperty("NewProjectWizard_Title", NbBundle.getMessage(ImportWebProjectWizardIterator.class, "TXT_WebExtSources")); jc.putClientProperty("WizardPanel_contentSelectedIndex", Integer.valueOf(i)); jc.putClientProperty("WizardPanel_contentData", steps); } 227 } 228 } 229 public void uninitialize(WizardDescriptor wiz) { 230 this.wiz = null; 231 panels = null; 232 } 233 234 public String name() { 235 return MessageFormat.format(NbBundle.getMessage(ImportWebProjectWizardIterator.class, "LBL_WizardStepsCount"), Integer.toString(index + 1), Integer.toString(panels.length)); } 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 (); 246 index++; 247 } 248 public void previousPanel() { 249 if (!hasPrevious()) throw new NoSuchElementException (); 250 index--; 251 } 252 public WizardDescriptor.Panel current() { 253 return panels[index]; 254 } 255 256 public final void addChangeListener(ChangeListener l) {} 258 public final void removeChangeListener(ChangeListener 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 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 listeners = new HashSet (1); 285 public void addChangeListener(ChangeListener l) { 286 synchronized (listeners) { 287 listeners.add(l); 288 } 289 } 290 public void removeChangeListener(ChangeListener l) { 291 synchronized (listeners) { 292 listeners.remove(l); 293 } 294 } 295 protected void fireChangeEvent() { 296 Iterator it; 297 synchronized (listeners) { 298 it = new HashSet (listeners).iterator(); 299 } 300 ChangeEvent ev = new ChangeEvent (this); 301 while (it.hasNext()) { 302 ((ChangeListener )it.next()).stateChanged(ev); 303 } 304 } 305 public void readSettings (Object settings) { 306 wizardDescriptor = (WizardDescriptor) settings; 307 panel.read(wizardDescriptor); 308 309 Object substitute = ((JComponent ) panel).getClientProperty("NewProjectWizard_Title"); if (substitute != null) 313 wizardDescriptor.putProperty("NewProjectWizard_Title", substitute); } 315 316 public void storeSettings (Object settings) { 317 WizardDescriptor d = (WizardDescriptor) settings; 318 panel.store(d); 319 ((WizardDescriptor) d).putProperty ("NewProjectWizard_Title", null); 321 moduleLoc = panel.moduleLocationTextField.getText().trim(); 322 } 323 324 public void validate() throws WizardValidationException { 325 File dirF = new File (panel.projectLocationTextField.getText()); 326 if (!new File (dirF, GeneratedFilesHelper.BUILD_XML_PATH).exists()) { 327 setBuildfile(GeneratedFilesHelper.BUILD_XML_PATH); 328 } else { 329 File buildFile = new File (dirF, getBuildfile()); 330 if (buildFile.exists()) { 331 JButton ok = createButton( 332 "LBL_IW_Buildfile_OK", "ACS_IW_BuildFileDialog_OKButton_LabelMnemonic", "LBL_IW_BuildFileDialog_OK_LabelMnemonic"); JButton cancel = createButton( 335 "LBL_IW_Buildfile_Cancel", "ACS_IW_BuildFileDialog_CancelButton_LabelMnemonic", "LBL_IW_BuildFileDialog_Cancel_LabelMnemonic"); final ImportBuildfile ibf = new ImportBuildfile(buildFile, ok); 338 DialogDescriptor descriptor = new DialogDescriptor(ibf, 339 NbBundle.getMessage(ImportWebProjectWizardIterator.class, "LBL_IW_BuildfileTitle"), true, new Object []{ok, cancel}, DialogDescriptor.OK_OPTION, DialogDescriptor.DEFAULT_ALIGN, 341 null, null); 342 Dialog 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 createButton(String labelId, String labelMnemonicId, String mnemonicId) { 353 JButton button = new JButton (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 |