1 19 20 package org.netbeans.modules.project.ui; 21 22 import java.awt.Component ; 23 import java.io.IOException ; 24 import java.util.Collections ; 25 import java.util.Set ; 26 import javax.swing.JComponent ; 27 import javax.swing.event.ChangeListener ; 28 import org.netbeans.api.project.Project; 29 import org.netbeans.api.project.ProjectUtils; 30 import org.netbeans.api.project.Sources; 31 import org.netbeans.spi.project.ui.templates.support.Templates; 32 import org.openide.WizardDescriptor; 33 import org.openide.filesystems.FileObject; 34 import org.openide.loaders.DataFolder; 35 import org.openide.loaders.DataObject; 36 37 38 43 public class NewFileIterator implements WizardDescriptor.InstantiatingIterator { 44 45 private static final long serialVersionUID = 1L; 46 47 private transient boolean isLibrary; 48 private transient WizardDescriptor.Iterator simpleIterator; 49 private transient WizardDescriptor.Panel[] panels; 50 private transient WizardDescriptor wiz; 51 private transient Project currentProject; 52 53 private transient boolean isFolder; 54 55 56 57 private NewFileIterator (boolean isFolderIterator) { 58 isFolder = isFolderIterator; 59 } 60 61 public static NewFileIterator genericFileIterator () { 62 return new NewFileIterator (false); 63 } 64 65 public static NewFileIterator folderIterator () { 66 return new NewFileIterator (true); 67 } 68 69 public static NewFileIterator emptyFileIterator () { 70 return new NewFileIterator (false); 71 } 72 73 private WizardDescriptor.Iterator getSimpleIterator () { 74 if (simpleIterator == null) { 75 assert panels != null && panels.length > 0; 76 simpleIterator = new WizardDescriptor.ArrayIterator (panels); 77 } 78 return simpleIterator; 79 } 80 81 private WizardDescriptor.Panel[] getPanels (WizardDescriptor wizardDescriptor) { 82 Project project = Templates.getProject( wizardDescriptor ); 83 assert project != null : wizardDescriptor; 84 if (!project.equals (currentProject) || panels == null) { 85 currentProject = project; 86 Sources sources = ProjectUtils.getSources(project); 87 if (isFolder) { 88 panels = new WizardDescriptor.Panel[] { 89 new SimpleTargetChooserPanel (project, sources.getSourceGroups (Sources.TYPE_GENERIC), null, true) 90 }; 91 } else { 92 panels = new WizardDescriptor.Panel[] { 93 Templates.createSimpleTargetChooser (project, sources.getSourceGroups (Sources.TYPE_GENERIC)) 94 }; 95 } 96 } 97 return panels; 98 } 99 100 private String [] createSteps (String [] before) { 101 assert panels != null; 102 103 if (before == null) { 104 before = new String [0]; 105 } 106 107 String [] res = new String [ (before.length - 1) + panels.length]; 108 for (int i = 0; i < res.length; i++) { 109 if (i < (before.length - 1)) { 110 res[i] = before[i]; 111 } else { 112 res[i] = panels[i - before.length + 1].getComponent ().getName (); 113 } 114 } 115 return res; 116 } 117 118 public Set instantiate () throws IOException { 119 FileObject dir = Templates.getTargetFolder( wiz ); 120 121 DataFolder df = DataFolder.findFolder( dir ); 122 FileObject template = Templates.getTemplate( wiz ); 123 124 DataObject dTemplate = DataObject.find( template ); 125 DataObject dobj = dTemplate.createFromTemplate( df, Templates.getTargetName( wiz ) ); 126 127 return Collections.singleton (dobj.getPrimaryFile ()); 128 } 129 130 public void initialize(WizardDescriptor wiz) { 131 panels = getPanels (wiz); 132 this.wiz = wiz; 133 134 String [] beforeSteps = null; 136 Object prop = wiz.getProperty ("WizardPanel_contentData"); if (prop != null && prop instanceof String []) { 138 beforeSteps = (String [])prop; 139 } 140 String [] steps = createSteps (beforeSteps); 141 for (int i = 0; i < panels.length; i++) { 142 Component c = panels[i].getComponent(); 143 if (steps[i] == null) { 144 steps[i] = c.getName(); 148 } 149 if (c instanceof JComponent ) { JComponent jc = (JComponent )c; 151 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 156 } 157 } 158 159 public void uninitialize (WizardDescriptor wiz) { 160 this.simpleIterator = null; 161 this.wiz = null; 162 panels = null; 163 } 164 165 public String name() { 166 return getSimpleIterator ().name (); 167 } 168 169 public boolean hasNext() { 170 return getSimpleIterator ().hasNext (); 171 } 172 public boolean hasPrevious() { 173 return getSimpleIterator ().hasPrevious (); 174 } 175 public void nextPanel() { 176 getSimpleIterator ().nextPanel (); 177 } 178 public void previousPanel() { 179 getSimpleIterator ().previousPanel (); 180 } 181 public WizardDescriptor.Panel current() { 182 return getSimpleIterator ().current (); 183 } 184 public final void addChangeListener(ChangeListener l) { 185 getSimpleIterator ().addChangeListener (l); 186 } 187 public final void removeChangeListener(ChangeListener l) { 188 getSimpleIterator ().removeChangeListener (l); 189 } 190 } 191 | Popular Tags |