1 19 20 package org.netbeans.modules.java.j2seproject.ui.wizards; 21 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.io.PrintWriter ; 27 import java.util.HashSet ; 28 import java.util.NoSuchElementException ; 29 import java.util.Set ; 30 import javax.swing.JComponent ; 31 import javax.swing.event.ChangeListener ; 32 import org.netbeans.api.progress.ProgressHandle; 33 import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator; 34 import org.netbeans.modules.java.j2seproject.ui.FoldersListSettings; 35 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties; 36 import org.netbeans.spi.project.support.ant.AntProjectHelper; 37 import org.netbeans.spi.project.support.ant.EditableProperties; 38 import org.netbeans.spi.project.ui.support.ProjectChooser; 39 import org.openide.ErrorManager; 40 import org.openide.WizardDescriptor; 41 import org.openide.filesystems.FileLock; 42 import org.openide.filesystems.FileObject; 43 import org.openide.filesystems.FileUtil; 44 import org.openide.util.NbBundle; 45 46 49 public class NewJ2SEProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator { 50 51 enum WizardType {APP, LIB, EXT} 52 53 static final String PROP_NAME_INDEX = "nameIndex"; 55 private static final String MANIFEST_FILE = "manifest.mf"; 57 private static final long serialVersionUID = 1L; 58 59 private WizardType type; 60 61 62 public NewJ2SEProjectWizardIterator() { 63 this(WizardType.APP); 64 } 65 66 public NewJ2SEProjectWizardIterator(WizardType type) { 67 this.type = type; 68 } 69 70 public static NewJ2SEProjectWizardIterator library() { 71 return new NewJ2SEProjectWizardIterator(WizardType.LIB); 72 } 73 74 public static NewJ2SEProjectWizardIterator existing() { 75 return new NewJ2SEProjectWizardIterator(WizardType.EXT); 76 } 77 78 private WizardDescriptor.Panel[] createPanels() { 79 switch (type) { 80 case EXT: 81 return new WizardDescriptor.Panel[] { 82 new PanelConfigureProject(type), 83 new PanelSourceFolders.Panel(), 84 new PanelIncludesExcludes(), 85 }; 86 default: 87 return new WizardDescriptor.Panel[] { 88 new PanelConfigureProject(type) 89 }; 90 } 91 } 92 93 private String [] createSteps() { 94 switch (type) { 95 case EXT: 96 return new String [] { 97 NbBundle.getMessage(NewJ2SEProjectWizardIterator.class,"LAB_ConfigureProject"), 98 NbBundle.getMessage(NewJ2SEProjectWizardIterator.class,"LAB_ConfigureSourceRoots"), 99 NbBundle.getMessage(NewJ2SEProjectWizardIterator.class,"LAB_PanelIncludesExcludes"), 100 }; 101 default: 102 return new String [] { 103 NbBundle.getMessage(NewJ2SEProjectWizardIterator.class,"LAB_ConfigureProject"), 104 }; 105 } 106 } 107 108 109 public Set <?> instantiate() throws IOException { 110 assert false : "Cannot call this method if implements WizardDescriptor.ProgressInstantiatingIterator."; 111 return null; 112 } 113 114 public Set <FileObject> instantiate (ProgressHandle handle) throws IOException { 115 handle.start (4); 116 Set <FileObject> resultSet = new HashSet <FileObject>(); 118 File dirF = (File )wiz.getProperty("projdir"); if (dirF != null) { 120 dirF = FileUtil.normalizeFile(dirF); 121 } 122 String name = (String )wiz.getProperty("name"); String mainClass = (String )wiz.getProperty("mainClass"); handle.progress (NbBundle.getMessage (NewJ2SEProjectWizardIterator.class, "LBL_NewJ2SEProjectWizardIterator_WizardProgress_CreatingProject"), 1); 125 switch (type) { 126 case EXT: 127 File [] sourceFolders = (File [])wiz.getProperty("sourceRoot"); File [] testFolders = (File [])wiz.getProperty("testRoot"); AntProjectHelper h = J2SEProjectGenerator.createProject(dirF, name, sourceFolders, testFolders, MANIFEST_FILE ); 130 EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 131 String includes = (String ) wiz.getProperty(J2SEProjectProperties.INCLUDES); 132 if (includes == null) { 133 includes = "**"; } 135 ep.setProperty(J2SEProjectProperties.INCLUDES, includes); 136 String excludes = (String ) wiz.getProperty(J2SEProjectProperties.EXCLUDES); 137 if (excludes == null) { 138 excludes = ""; } 140 ep.setProperty(J2SEProjectProperties.EXCLUDES, excludes); 141 h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 142 handle.progress (2); 143 for (File f : sourceFolders) { 144 FileObject srcFo = FileUtil.toFileObject(f); 145 if (srcFo != null) { 146 resultSet.add (srcFo); 147 } 148 } 149 break; 150 default: 151 h = J2SEProjectGenerator.createProject(dirF, name, mainClass, type == WizardType.APP ? MANIFEST_FILE : null); 152 handle.progress (2); 153 if (mainClass != null && mainClass.length () > 0) { 154 try { 155 FileObject sourcesRoot = h.getProjectDirectory ().getFileObject ("src"); FileObject mainClassFo = getMainClassFO (sourcesRoot, mainClass); 158 assert mainClassFo != null : "sourcesRoot: " + sourcesRoot + ", mainClass: " + mainClass; resultSet.add (mainClassFo); 161 } catch (Exception x) { 162 ErrorManager.getDefault().notify(x); 163 } 164 } 165 } 170 FileObject dir = FileUtil.toFileObject(dirF); 171 switch (type) { 172 case APP: 173 case EXT: 174 createManifest(dir); 175 } 176 handle.progress (3); 177 178 int index = (Integer ) wiz.getProperty(PROP_NAME_INDEX); 181 switch (type) { 182 case APP: 183 FoldersListSettings.getDefault().setNewApplicationCount(index); 184 break; 185 case LIB: 186 FoldersListSettings.getDefault().setNewLibraryCount(index); 187 break; 188 case EXT: 189 FoldersListSettings.getDefault().setNewProjectCount(index); 190 break; 191 } 192 resultSet.add (dir); 193 handle.progress (NbBundle.getMessage (NewJ2SEProjectWizardIterator.class, "LBL_NewJ2SEProjectWizardIterator_WizardProgress_PreparingToOpen"), 4); 194 dirF = (dirF != null) ? dirF.getParentFile() : null; 195 if (dirF != null && dirF.exists()) { 196 ProjectChooser.setProjectsFolder (dirF); 197 } 198 199 return resultSet; 200 } 201 202 203 private transient int index; 204 private transient WizardDescriptor.Panel[] panels; 205 private transient WizardDescriptor wiz; 206 207 public void initialize(WizardDescriptor wiz) { 208 this.wiz = wiz; 209 index = 0; 210 panels = createPanels(); 211 String [] steps = createSteps(); 213 for (int i = 0; i < panels.length; i++) { 214 Component c = panels[i].getComponent(); 215 if (steps[i] == null) { 216 steps[i] = c.getName(); 220 } 221 if (c instanceof JComponent ) { JComponent jc = (JComponent )c; 223 jc.putClientProperty("WizardPanel_contentSelectedIndex", i); jc.putClientProperty("WizardPanel_contentData", steps); } 228 } 229 this.wiz.putProperty("sourceRoot", new File [0]); this.wiz.putProperty("testRoot", new File [0]); } 233 234 public void uninitialize(WizardDescriptor wiz) { 235 if (this.wiz != null) { 236 this.wiz.putProperty("projdir",null); this.wiz.putProperty("name",null); this.wiz.putProperty("mainClass",null); switch (type) { 240 case EXT: 241 this.wiz.putProperty("sourceRoot",null); this.wiz.putProperty("testRoot",null); } 244 this.wiz = null; 245 panels = null; 246 } 247 } 248 249 public String name() { 250 return NbBundle.getMessage(NewJ2SEProjectWizardIterator.class, "LAB_IteratorName", index + 1, panels.length); 251 } 252 253 public boolean hasNext() { 254 return index < panels.length - 1; 255 } 256 public boolean hasPrevious() { 257 return index > 0; 258 } 259 public void nextPanel() { 260 if (!hasNext()) throw new NoSuchElementException (); 261 index++; 262 } 263 public void previousPanel() { 264 if (!hasPrevious()) throw new NoSuchElementException (); 265 index--; 266 } 267 public WizardDescriptor.Panel current () { 268 return panels[index]; 269 } 270 271 public final void addChangeListener(ChangeListener l) {} 273 public final void removeChangeListener(ChangeListener l) {} 274 275 private FileObject getMainClassFO (FileObject sourcesRoot, String mainClass) { 277 mainClass = mainClass.replace ('.', '/'); 280 282 return sourcesRoot.getFileObject (mainClass+ ".java"); } 284 285 static String getPackageName (String displayName) { 286 StringBuffer builder = new StringBuffer (); 287 boolean firstLetter = true; 288 for (int i=0; i< displayName.length(); i++) { 289 char c = displayName.charAt(i); 290 if ((!firstLetter && Character.isJavaIdentifierPart (c)) || (firstLetter && Character.isJavaIdentifierStart(c))) { 291 firstLetter = false; 292 if (Character.isUpperCase(c)) { 293 c = Character.toLowerCase(c); 294 } 295 builder.append(c); 296 } 297 } 298 return builder.length() == 0 ? NbBundle.getMessage(NewJ2SEProjectWizardIterator.class,"TXT_DefaultPackageName") : builder.toString(); 299 } 300 301 306 private static void createManifest(final FileObject dir) throws IOException { 307 FileObject manifest = dir.createData(MANIFEST_FILE); 308 FileLock lock = manifest.lock(); 309 try { 310 OutputStream os = manifest.getOutputStream(lock); 311 try { 312 PrintWriter pw = new PrintWriter (os); 313 pw.println("Manifest-Version: 1.0"); pw.println("X-COMMENT: Main-Class will be added automatically by build"); pw.println(); pw.flush(); 317 } finally { 318 os.close(); 319 } 320 } finally { 321 lock.releaseLock(); 322 } 323 } 324 325 } 326 | Popular Tags |