1 19 20 package org.netbeans.modules.java.freeform.ui; 21 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.HashSet ; 27 import java.util.List ; 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.java.project.JavaProjectConstants; 33 import org.netbeans.api.progress.ProgressHandle; 34 import org.netbeans.api.project.Project; 35 import org.netbeans.api.project.ProjectManager; 36 import org.netbeans.api.project.ProjectUtils; 37 import org.netbeans.api.project.SourceGroup; 38 import org.netbeans.api.project.Sources; 39 import org.netbeans.modules.ant.freeform.spi.TargetDescriptor; 40 import org.netbeans.modules.ant.freeform.spi.support.NewFreeformProjectSupport; 41 import org.netbeans.modules.java.freeform.spi.support.NewJavaFreeformProjectSupport; 42 import org.netbeans.spi.project.support.ant.AntProjectHelper; 43 import org.netbeans.spi.project.ui.support.ProjectChooser; 44 import org.openide.WizardDescriptor; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.util.NbBundle; 48 49 52 public class NewJ2SEFreeformProjectWizardIterator implements WizardDescriptor.ProgressInstantiatingIterator { 53 54 public static final String PROP_PROJECT_MODEL = "projectModel"; 56 private static final long serialVersionUID = 1L; 57 58 private transient int index; 59 private transient WizardDescriptor.Panel[] panels; 60 private transient WizardDescriptor wiz; 61 62 public NewJ2SEFreeformProjectWizardIterator() { 63 } 64 65 private WizardDescriptor.Panel[] createPanels () { 66 return new WizardDescriptor.Panel[] { 67 NewFreeformProjectSupport.createBasicProjectInfoWizardPanel(), 68 NewFreeformProjectSupport.createTargetMappingWizardPanel(new ArrayList <TargetDescriptor>()), new SourceFoldersWizardPanel(), 70 new ClasspathWizardPanel(), 71 }; 72 } 73 74 public Set <?> instantiate() throws IOException { 75 throw new AssertionError (); 76 } 77 78 public Set <?> instantiate(final ProgressHandle handle) throws IOException { 79 handle.start(6); 80 final WizardDescriptor wiz = this.wiz; 81 final IOException [] ioe = new IOException [1]; 82 ProjectManager.mutex().writeAccess(new Runnable () { 83 public void run() { 84 try { 85 AntProjectHelper helper = NewFreeformProjectSupport.instantiateBasicProjectInfoWizardPanel(wiz); 86 handle.progress(1); 87 NewFreeformProjectSupport.instantiateTargetMappingWizardPanel(helper, wiz); 88 handle.progress(2); 89 ProjectModel pm = (ProjectModel)wiz.getProperty(PROP_PROJECT_MODEL); 90 ProjectModel.instantiateJavaProject(helper, pm); 91 handle.progress(3); 92 Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory()); 93 handle.progress(4); 94 ProjectManager.getDefault().saveProject(p); 95 handle.progress(5); 96 } catch (IOException e) { 97 ioe[0] = e; 98 return; 99 } 100 }}); 101 if (ioe[0] != null) { 102 throw ioe[0]; 103 } 104 ProjectModel pm = (ProjectModel)wiz.getProperty(PROP_PROJECT_MODEL); 105 File nbProjectFolder = pm.getNBProjectFolder(); 106 Set <FileObject> resultSet = new HashSet <FileObject>(); 107 resultSet.add(FileUtil.toFileObject(nbProjectFolder)); 108 Project p = ProjectManager.getDefault().findProject(FileUtil.toFileObject(nbProjectFolder)); 109 if (p != null) { 110 Sources srcs = ProjectUtils.getSources(p); 111 if (srcs != null) { 112 SourceGroup[] grps = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 113 if (grps != null && grps.length > 0) { 114 resultSet.add(grps[0].getRootFolder()); 115 } 116 } 117 } 118 119 File f = nbProjectFolder.getParentFile(); 120 if (f != null) { 121 ProjectChooser.setProjectsFolder(f); 122 } 123 handle.finish(); 124 return resultSet; 125 } 126 127 128 public void initialize(WizardDescriptor wiz) { 129 this.wiz = wiz; 130 index = 0; 131 panels = createPanels(); 132 133 List <String > l = new ArrayList <String >(); 134 for (int i = 0; i < panels.length; i++) { 135 Component c = panels[i].getComponent(); 136 assert c instanceof JComponent ; 137 JComponent jc = (JComponent )c; 138 l.add(jc.getName()); 139 } 140 String [] steps = l.toArray(new String [l.size()]); 141 142 for (int i = 0; i < panels.length; i++) { 143 Component c = panels[i].getComponent(); 144 assert c instanceof JComponent ; 145 JComponent jc = (JComponent )c; 146 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); jc.putClientProperty ("NewProjectWizard_Title", NbBundle.getMessage (NewJ2SEFreeformProjectWizardIterator.class, "TXT_NewJ2SEFreeformProjectWizardIterator_NewProjectWizardTitle")); } 153 } 154 155 public void uninitialize(WizardDescriptor wiz) { 156 NewFreeformProjectSupport.uninitializeBasicProjectInfoWizardPanel(wiz); 157 NewFreeformProjectSupport.uninitializeTargetMappingWizardPanel(wiz); 158 NewJavaFreeformProjectSupport.uninitializeJavaPanels(wiz); 159 this.wiz = null; 160 panels = null; 161 } 162 163 public String name() { 164 return NbBundle.getMessage(NewJ2SEFreeformProjectWizardIterator.class, "TXT_NewJ2SEFreeformProjectWizardIterator_TitleFormat", index + 1, panels.length); 165 } 166 167 public boolean hasNext() { 168 if (current() instanceof SourceFoldersWizardPanel) { 169 assert current().getComponent() instanceof SourceFoldersPanel; 170 SourceFoldersPanel sfp = (SourceFoldersPanel)current().getComponent(); 171 if (!sfp.hasSomeSourceFolder()) { 172 return false; 173 } 174 } 175 return index < panels.length - 1; 176 } 177 public boolean hasPrevious() { 178 return index > 0; 179 } 180 public void nextPanel() { 181 if (!hasNext()) throw new NoSuchElementException (); 182 index++; 183 } 184 public void previousPanel() { 185 if (!hasPrevious()) throw new NoSuchElementException (); 186 index--; 187 } 188 public WizardDescriptor.Panel current () { 189 return panels[index]; 190 } 191 192 public final void addChangeListener(ChangeListener l) {} 194 public final void removeChangeListener(ChangeListener l) {} 195 196 } 197 | Popular Tags |