1 19 20 package org.netbeans.modules.ruby.railsprojects.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.modules.ruby.rubyproject.api.RubyInstallation; 34 import org.netbeans.api.progress.ProgressHandle; 35 import org.netbeans.modules.ruby.railsprojects.RailsProjectGenerator; 36 import org.netbeans.modules.ruby.railsprojects.ui.FoldersListSettings; 37 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper; 38 import org.netbeans.spi.project.ui.support.ProjectChooser; 39 import org.openide.ErrorManager; 40 import org.openide.WizardDescriptor; 41 import org.openide.cookies.EditorCookie; 42 import org.openide.filesystems.FileLock; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileUtil; 45 import org.openide.loaders.DataObject; 46 import org.openide.loaders.DataObjectNotFoundException; 47 import org.openide.modules.InstalledFileLocator; 48 import org.openide.util.NbBundle; 49 50 55 public class NewRailsProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator { 56 57 static final int TYPE_APP = 0; 58 static final int TYPE_EXT = 2; 60 61 static final String PROP_NAME_INDEX = "nameIndex"; 63 private static final long serialVersionUID = 1L; 64 65 private int type; 66 67 public NewRailsProjectWizardIterator() { 68 this(TYPE_APP); 69 } 70 71 public NewRailsProjectWizardIterator(int type) { 72 this.type = type; 73 } 74 75 public static NewRailsProjectWizardIterator existing () { 76 return new NewRailsProjectWizardIterator(TYPE_EXT); 77 } 78 79 private WizardDescriptor.Panel[] createPanels () { 80 return new WizardDescriptor.Panel[] { 81 new PanelConfigureProject( this.type ), 82 new RailsInstallationPanel.Panel() 83 }; 84 } 85 86 private String [] createSteps() { 87 return new String [] { 88 NbBundle.getMessage(NewRailsProjectWizardIterator.class,"LAB_ConfigureProject"), 89 NbBundle.getMessage(NewRailsProjectWizardIterator.class,"LAB_InstallRails"), 90 }; 91 } 92 93 94 public Set instantiate () throws IOException { 95 assert false : "Cannot call this method if implements WizardDescriptor.ProgressInstantiatingIterator."; 96 return null; 97 } 98 99 public Set instantiate (ProgressHandle handle) throws IOException { 100 handle.start (4); 101 Set resultSet = new HashSet (); 103 File dirF = (File )wiz.getProperty("projdir"); if (dirF != null) { 105 dirF = FileUtil.normalizeFile(dirF); 106 } 107 String name = (String )wiz.getProperty("name"); handle.progress (NbBundle.getMessage (NewRailsProjectWizardIterator.class, "LBL_NewRailsProjectWizardIterator_WizardProgress_CreatingProject"), 1); 110 111 RakeProjectHelper h = null; 112 113 h = RailsProjectGenerator.createProject(dirF, name, type == TYPE_APP); 114 handle.progress (2); 115 116 FileObject dir = FileUtil.toFileObject(dirF); 129 handle.progress (3); 133 134 resultSet.add (dir); 139 handle.progress (NbBundle.getMessage (NewRailsProjectWizardIterator.class, "LBL_NewRailsProjectWizardIterator_WizardProgress_PreparingToOpen"), 4); 140 dirF = (dirF != null) ? dirF.getParentFile() : null; 141 if (dirF != null && dirF.exists()) { 142 ProjectChooser.setProjectsFolder (dirF); 143 } 144 145 if (type == TYPE_APP) { 148 String jrubyHome = RubyInstallation.getInstance().getJRubyHome(); 149 File railsFile = new File (jrubyHome + File.separator + "docs" + File.separator + "README.rails"); 150 FileObject fo = FileUtil.toFileObject(railsFile); 151 if (fo != null) { 152 try { 154 DataObject dobj = DataObject.find(fo); 155 EditorCookie cookie = dobj.getCookie(EditorCookie.class); 156 if (cookie != null) { 157 cookie.open(); 158 } 159 } catch (DataObjectNotFoundException ex) { 160 ErrorManager.getDefault().notify(ex); 161 } 162 } 163 } 164 165 return resultSet; 166 } 167 168 169 private transient int index; 170 private transient WizardDescriptor.Panel[] panels; 171 private transient WizardDescriptor wiz; 172 173 public void initialize(WizardDescriptor wiz) { 174 this.wiz = wiz; 175 index = 0; 176 panels = createPanels(); 177 String [] steps = createSteps(); 179 for (int i = 0; i < panels.length; i++) { 180 Component c = panels[i].getComponent(); 181 if (steps[i] == null) { 182 steps[i] = c.getName(); 186 } 187 if (c instanceof JComponent ) { JComponent jc = (JComponent )c; 189 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 194 } 195 this.wiz.putProperty("sourceRoot", new File [0]); this.wiz.putProperty("testRoot", new File [0]); } 199 200 public void uninitialize(WizardDescriptor wiz) { 201 if (this.wiz != null) { 202 this.wiz.putProperty("projdir",null); this.wiz.putProperty("name",null); this.wiz.putProperty("mainClass",null); this.wiz = null; 206 panels = null; 207 } 208 } 209 210 public String name() { 211 return MessageFormat.format (NbBundle.getMessage(NewRailsProjectWizardIterator.class,"LAB_IteratorName"), 212 new Object [] {new Integer (index + 1), new Integer (panels.length) }); 213 } 214 215 public boolean hasNext() { 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 (); 223 index++; 224 } 225 public void previousPanel() { 226 if (!hasPrevious()) throw new NoSuchElementException (); 227 index--; 228 } 229 public WizardDescriptor.Panel current () { 230 return panels[index]; 231 } 232 233 public final void addChangeListener(ChangeListener l) {} 235 public final void removeChangeListener(ChangeListener l) {} 236 237 private FileObject getMainClassFO (FileObject sourcesRoot, String mainClass) { 239 return sourcesRoot.getFileObject(mainClass); 246 } 247 248 static String getPackageName (String displayName) { 249 StringBuffer builder = new StringBuffer (); 250 boolean firstLetter = true; 251 for (int i=0; i< displayName.length(); i++) { 252 char c = displayName.charAt(i); 253 if ((!firstLetter && Character.isJavaIdentifierPart (c)) || (firstLetter && Character.isJavaIdentifierStart(c))) { 254 firstLetter = false; 255 if (Character.isUpperCase(c)) { 256 c = Character.toLowerCase(c); 257 } 258 builder.append(c); 259 } 260 } 261 return builder.length() == 0 ? NbBundle.getMessage(NewRailsProjectWizardIterator.class,"TXT_DefaultPackageName") : builder.toString(); 262 } 263 264 288 } 289 | Popular Tags |