1 19 20 package org.netbeans.modules.java.project; 21 22 import java.awt.Component ; 23 import java.io.IOException ; 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.NoSuchElementException ; 28 import java.util.Set ; 29 import javax.swing.JComponent ; 30 import javax.swing.event.ChangeEvent ; 31 import javax.swing.event.ChangeListener ; 32 import org.netbeans.api.java.project.JavaProjectConstants; 33 import org.netbeans.api.project.Project; 34 import org.netbeans.api.project.ProjectUtils; 35 import org.netbeans.api.project.SourceGroup; 36 import org.netbeans.api.project.Sources; 37 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates; 38 import org.netbeans.spi.project.ui.templates.support.Templates; 39 import org.openide.WizardDescriptor; 40 import org.openide.ErrorManager; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.loaders.DataFolder; 44 import org.openide.loaders.DataObject; 45 46 49 public class NewJavaFileWizardIterator implements WizardDescriptor.InstantiatingIterator { 50 51 private static final long serialVersionUID = 1L; 52 53 public static final int TYPE_FILE = 0; 54 public static final int TYPE_PACKAGE = 1; 55 public static final int TYPE_PKG_INFO = 2; 56 57 private int type = TYPE_FILE; 58 59 60 public NewJavaFileWizardIterator() {} 61 62 63 private NewJavaFileWizardIterator( int type ) { 64 this.type = type; 65 } 66 67 public static NewJavaFileWizardIterator packageWizard() { 68 return new NewJavaFileWizardIterator( TYPE_PACKAGE ); 69 } 70 71 public static NewJavaFileWizardIterator packageInfoWizard () { 72 return new NewJavaFileWizardIterator( TYPE_PKG_INFO ); 73 } 74 75 private WizardDescriptor.Panel[] createPanels (WizardDescriptor wizardDescriptor) { 76 77 Project project = Templates.getProject( wizardDescriptor ); 79 Sources sources = ProjectUtils.getSources(project); 80 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 81 assert groups != null : "Cannot return null from Sources.getSourceGroups: " + sources; 82 if (groups.length == 0) { 83 groups = sources.getSourceGroups( Sources.TYPE_GENERIC ); 84 return new WizardDescriptor.Panel[] { 85 Templates.createSimpleTargetChooser( project, groups ), 86 }; 87 } 88 else { 89 90 if ( this.type == TYPE_FILE ) { 91 return new WizardDescriptor.Panel[] { 92 JavaTemplates.createPackageChooser( project, groups ), 93 }; 94 } 95 else { 96 return new WizardDescriptor.Panel[] { 97 new JavaTargetChooserPanel( project, groups, null, this.type, this.type == TYPE_PKG_INFO), 98 }; 99 } 100 } 101 102 } 103 104 private String [] createSteps(String [] before, WizardDescriptor.Panel[] panels) { 105 assert panels != null; 106 int diff = 0; 108 if (before == null) { 109 before = new String [0]; 110 } else if (before.length > 0) { 111 diff = ("...".equals (before[before.length - 1])) ? 1 : 0; } 113 String [] res = new String [ (before.length - diff) + panels.length]; 114 for (int i = 0; i < res.length; i++) { 115 if (i < (before.length - diff)) { 116 res[i] = before[i]; 117 } else { 118 res[i] = panels[i - before.length + diff].getComponent ().getName (); 119 } 120 } 121 return res; 122 } 123 124 public Set instantiate () throws IOException { 125 FileObject dir = Templates.getTargetFolder( wiz ); 126 String targetName = Templates.getTargetName( wiz ); 127 128 DataFolder df = DataFolder.findFolder( dir ); 129 FileObject template = Templates.getTemplate( wiz ); 130 131 FileObject createdFile = null; 132 if ( this.type == TYPE_PACKAGE ) { 133 targetName = targetName.replace( '.', '/' ); createdFile = FileUtil.createFolder( dir, targetName ); 135 } 136 else { 137 DataObject dTemplate = DataObject.find( template ); 138 DataObject dobj = dTemplate.createFromTemplate( df, targetName ); 139 createdFile = dobj.getPrimaryFile(); 140 } 141 142 return Collections.singleton( createdFile ); 143 } 144 145 146 private transient int index; 147 private transient WizardDescriptor.Panel[] panels; 148 private transient WizardDescriptor wiz; 149 150 public void initialize(WizardDescriptor wiz) { 151 this.wiz = wiz; 152 index = 0; 153 panels = createPanels( wiz ); 154 String [] beforeSteps = null; 156 Object prop = wiz.getProperty ("WizardPanel_contentData"); if (prop != null && prop instanceof String []) { 158 beforeSteps = (String [])prop; 159 } 160 String [] steps = createSteps (beforeSteps, panels); 161 for (int i = 0; i < panels.length; i++) { 162 Component c = panels[i].getComponent(); 163 if (steps[i] == null) { 164 steps[i] = c.getName(); 168 } 169 if (c instanceof JComponent ) { JComponent jc = (JComponent )c; 171 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 176 } 177 } 178 public void uninitialize (WizardDescriptor wiz) { 179 this.wiz = null; 180 panels = null; 181 } 182 183 public String name() { 184 return ""; } 187 188 public boolean hasNext() { 189 return index < panels.length - 1; 190 } 191 public boolean hasPrevious() { 192 return index > 0; 193 } 194 public void nextPanel() { 195 if (!hasNext()) throw new NoSuchElementException (); 196 index++; 197 } 198 public void previousPanel() { 199 if (!hasPrevious()) throw new NoSuchElementException (); 200 index--; 201 } 202 public WizardDescriptor.Panel current() { 203 return panels[index]; 204 } 205 206 private transient Set <ChangeListener > listeners = new HashSet <ChangeListener >(1); 207 208 public final void addChangeListener(ChangeListener l) { 209 synchronized(listeners) { 210 listeners.add(l); 211 } 212 } 213 public final void removeChangeListener(ChangeListener l) { 214 synchronized(listeners) { 215 listeners.remove(l); 216 } 217 } 218 protected final void fireChangeEvent() { 219 ChangeListener [] ls; 220 synchronized (listeners) { 221 ls = listeners.toArray(new ChangeListener [listeners.size()]); 222 } 223 ChangeEvent ev = new ChangeEvent (this); 224 for (ChangeListener l : ls) { 225 l.stateChanged(ev); 226 } 227 } 228 229 230 } 231 | Popular Tags |