1 19 20 package org.netbeans.modules.ruby.rubyproject.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.text.MessageFormat ; 28 import java.util.HashSet ; 29 import java.util.NoSuchElementException ; 30 import java.util.Set ; 31 import javax.swing.JComponent ; 32 import javax.swing.event.ChangeListener ; 33 import org.netbeans.api.progress.ProgressHandle; 34 import org.netbeans.modules.ruby.rubyproject.RubyProjectGenerator; 35 import org.netbeans.modules.ruby.rubyproject.ui.FoldersListSettings; 36 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper; 37 import org.netbeans.spi.project.ui.support.ProjectChooser; 38 import org.openide.ErrorManager; 39 import org.openide.WizardDescriptor; 40 import org.openide.filesystems.FileLock; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.util.NbBundle; 44 45 48 public class NewRubyProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator { 49 50 static final int TYPE_APP = 0; 51 static final int TYPE_EXT = 2; 53 54 static final String PROP_NAME_INDEX = "nameIndex"; 56 58 private static final long serialVersionUID = 1L; 59 60 private int type; 61 62 63 public NewRubyProjectWizardIterator() { 64 this(TYPE_APP); 65 } 66 67 public NewRubyProjectWizardIterator(int type) { 68 this.type = type; 69 } 70 71 75 public static NewRubyProjectWizardIterator existing () { 76 return new NewRubyProjectWizardIterator( TYPE_EXT ); 77 } 78 79 private WizardDescriptor.Panel[] createPanels () { 80 return this.type == TYPE_EXT ? 81 new WizardDescriptor.Panel[] { 82 new PanelConfigureProject( this.type ), 83 new PanelSourceFolders.Panel() 84 } 85 :new WizardDescriptor.Panel[] { 86 new PanelConfigureProject( this.type ) 87 }; 88 } 89 90 private String [] createSteps() { 91 return this.type == TYPE_EXT ? 92 new String [] { 93 NbBundle.getMessage(NewRubyProjectWizardIterator.class,"LAB_ConfigureProject"), 94 NbBundle.getMessage(NewRubyProjectWizardIterator.class,"LAB_ConfigureSourceRoots"), 95 } 96 :new String [] { 97 NbBundle.getMessage(NewRubyProjectWizardIterator.class,"LAB_ConfigureProject"), 98 }; 99 } 100 101 102 public Set instantiate () throws IOException { 103 assert false : "Cannot call this method if implements WizardDescriptor.ProgressInstantiatingIterator."; 104 return null; 105 } 106 107 public Set instantiate (ProgressHandle handle) throws IOException { 108 handle.start (4); 109 Set resultSet = new HashSet (); 111 File dirF = (File )wiz.getProperty("projdir"); if (dirF != null) { 113 dirF = FileUtil.normalizeFile(dirF); 114 } 115 String name = (String )wiz.getProperty("name"); String mainClass = (String )wiz.getProperty("mainClass"); handle.progress (NbBundle.getMessage (NewRubyProjectWizardIterator.class, "LBL_NewRubyProjectWizardIterator_WizardProgress_CreatingProject"), 1); 118 if (this.type == TYPE_EXT) { 119 File [] sourceFolders = (File [])wiz.getProperty("sourceRoot"); File [] testFolders = (File [])wiz.getProperty("testRoot"); RubyProjectGenerator.createProject(dirF, name, sourceFolders, testFolders, null ); 122 handle.progress (2); 123 for (int i=0; i<sourceFolders.length; i++) { 124 FileObject srcFo = FileUtil.toFileObject(sourceFolders[i]); 125 if (srcFo != null) { 126 resultSet.add (srcFo); 127 } 128 } 129 } 130 else { 131 RakeProjectHelper h = RubyProjectGenerator.createProject(dirF, name, mainClass, null); 132 handle.progress (2); 133 138 if (mainClass != null && mainClass.length () > 0) { 139 try { 140 FileObject sourcesRoot = h.getProjectDirectory ().getFileObject ("lib"); FileObject mainClassFo = getMainClassFO (sourcesRoot, mainClass); 143 assert mainClassFo != null : "sourcesRoot: " + sourcesRoot + ", mainClass: " + mainClass; resultSet.add (mainClassFo); 146 } catch (Exception x) { 147 ErrorManager.getDefault().notify(x); 148 } 149 } 150 } 155 FileObject dir = FileUtil.toFileObject(dirF); 156 handle.progress (3); 160 161 Integer index = (Integer ) wiz.getProperty(PROP_NAME_INDEX); 164 switch (this.type) { 165 case TYPE_APP: 166 FoldersListSettings.getDefault().setNewApplicationCount(index.intValue()); 167 break; 168 case TYPE_EXT: 172 FoldersListSettings.getDefault().setNewProjectCount(index.intValue()); 173 break; 174 } 175 resultSet.add (dir); 176 handle.progress (NbBundle.getMessage (NewRubyProjectWizardIterator.class, "LBL_NewRubyProjectWizardIterator_WizardProgress_PreparingToOpen"), 4); 177 dirF = (dirF != null) ? dirF.getParentFile() : null; 178 if (dirF != null && dirF.exists()) { 179 ProjectChooser.setProjectsFolder (dirF); 180 } 181 182 return resultSet; 183 } 184 185 186 private transient int index; 187 private transient WizardDescriptor.Panel[] panels; 188 private transient WizardDescriptor wiz; 189 190 public void initialize(WizardDescriptor wiz) { 191 this.wiz = wiz; 192 index = 0; 193 panels = createPanels(); 194 String [] steps = createSteps(); 196 for (int i = 0; i < panels.length; i++) { 197 Component c = panels[i].getComponent(); 198 if (steps[i] == null) { 199 steps[i] = c.getName(); 203 } 204 if (c instanceof JComponent ) { JComponent jc = (JComponent )c; 206 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 211 } 212 this.wiz.putProperty("sourceRoot", new File [0]); this.wiz.putProperty("testRoot", new File [0]); } 216 217 public void uninitialize(WizardDescriptor wiz) { 218 if (this.wiz != null) { 219 this.wiz.putProperty("projdir",null); this.wiz.putProperty("name",null); this.wiz.putProperty("mainClass",null); if (this.type == TYPE_EXT) { 223 this.wiz.putProperty("sourceRoot",null); this.wiz.putProperty("testRoot",null); } 226 this.wiz = null; 227 panels = null; 228 } 229 } 230 231 public String name() { 232 return MessageFormat.format (NbBundle.getMessage(NewRubyProjectWizardIterator.class,"LAB_IteratorName"), 233 new Object [] {new Integer (index + 1), new Integer (panels.length) }); 234 } 235 236 public boolean hasNext() { 237 return index < panels.length - 1; 238 } 239 public boolean hasPrevious() { 240 return index > 0; 241 } 242 public void nextPanel() { 243 if (!hasNext()) throw new NoSuchElementException (); 244 index++; 245 } 246 public void previousPanel() { 247 if (!hasPrevious()) throw new NoSuchElementException (); 248 index--; 249 } 250 public WizardDescriptor.Panel current () { 251 return panels[index]; 252 } 253 254 public final void addChangeListener(ChangeListener l) {} 256 public final void removeChangeListener(ChangeListener l) {} 257 258 private FileObject getMainClassFO (FileObject sourcesRoot, String mainClass) { 260 return sourcesRoot.getFileObject(mainClass); 267 } 268 269 static String getPackageName (String displayName) { 270 StringBuffer builder = new StringBuffer (); 271 boolean firstLetter = true; 272 for (int i=0; i< displayName.length(); i++) { 273 char c = displayName.charAt(i); 274 if ((!firstLetter && Character.isJavaIdentifierPart (c)) || (firstLetter && Character.isJavaIdentifierStart(c))) { 275 firstLetter = false; 276 if (Character.isUpperCase(c)) { 277 c = Character.toLowerCase(c); 278 } 279 builder.append(c); 280 } 281 } 282 return builder.length() == 0 ? NbBundle.getMessage(NewRubyProjectWizardIterator.class,"TXT_DefaultPackageName") : builder.toString(); 283 } 284 285 309 } 310 | Popular Tags |