1 19 package org.netbeans.modules.apisupport.paintapp; 20 21 import java.awt.Component ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.util.Enumeration ; 27 import java.util.LinkedHashSet ; 28 import java.util.NoSuchElementException ; 29 import java.util.Set ; 30 import java.util.zip.ZipEntry ; 31 import java.util.zip.ZipInputStream ; 32 import javax.swing.JComponent ; 33 import javax.swing.event.ChangeListener ; 34 import org.netbeans.api.project.ProjectManager; 35 import org.netbeans.spi.project.ui.support.ProjectChooser; 36 import org.netbeans.spi.project.ui.templates.support.Templates; 37 import org.openide.WizardDescriptor; 38 import org.openide.filesystems.FileLock; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileUtil; 41 import org.openide.util.NbBundle; 42 43 public class PaintAppWizardIterator implements WizardDescriptor.InstantiatingIterator { 44 45 private static final long serialVersionUID = 1L; 46 47 private transient int index; 48 private transient WizardDescriptor.Panel[] panels; 49 private transient WizardDescriptor wiz; 50 51 public PaintAppWizardIterator() {} 52 53 public static PaintAppWizardIterator createIterator() { 54 return new PaintAppWizardIterator(); 55 } 56 57 private WizardDescriptor.Panel[] createPanels() { 58 return new WizardDescriptor.Panel[] { 59 new PaintAppWizardPanel(), 60 }; 61 } 62 63 private String [] createSteps() { 64 return new String [] { 65 NbBundle.getMessage(PaintAppWizardIterator.class, "LBL_CreateProjectStep") 66 }; 67 } 68 69 public Set instantiate() throws IOException { 70 Set resultSet = new LinkedHashSet (); 71 File dirF = FileUtil.normalizeFile((File ) wiz.getProperty("projdir")); dirF.mkdirs(); 73 74 FileObject template = Templates.getTemplate(wiz); 75 FileObject dir = FileUtil.toFileObject(dirF); 76 unZipFile(template.getInputStream(), dir); 77 78 resultSet.add(dir); 80 Enumeration e = dir.getFolders(true); 82 while (e.hasMoreElements()) { 83 FileObject subfolder = (FileObject) e.nextElement(); 84 if (ProjectManager.getDefault().isProject(subfolder)) { 85 resultSet.add(subfolder); 86 } 87 } 88 89 File parent = dirF.getParentFile(); 90 if (parent != null && parent.exists()) { 91 ProjectChooser.setProjectsFolder(parent); 92 } 93 94 return resultSet; 95 } 96 97 public void initialize(WizardDescriptor wiz) { 98 this.wiz = wiz; 99 index = 0; 100 panels = createPanels(); 101 String [] steps = createSteps(); 103 for (int i = 0; i < panels.length; i++) { 104 Component c = panels[i].getComponent(); 105 if (steps[i] == null) { 106 steps[i] = c.getName(); 110 } 111 if (c instanceof JComponent ) { JComponent jc = (JComponent ) c; 113 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 118 } 119 } 120 121 public void uninitialize(WizardDescriptor wiz) { 122 this.wiz.putProperty("projdir",null); this.wiz.putProperty("name",null); this.wiz = null; 125 panels = null; 126 } 127 128 public String name() { 129 return NbBundle.getMessage(PaintAppWizardIterator.class, "PaintAppWizardIterator.name.format", 130 new Object [] {new Integer (index + 1), new Integer (panels.length)}); 131 } 132 133 public boolean hasNext() { 134 return index < panels.length - 1; 135 } 136 137 public boolean hasPrevious() { 138 return index > 0; 139 } 140 141 public void nextPanel() { 142 if (!hasNext()) { 143 throw new NoSuchElementException (); 144 } 145 index++; 146 } 147 148 public void previousPanel() { 149 if (!hasPrevious()) { 150 throw new NoSuchElementException (); 151 } 152 index--; 153 } 154 155 public WizardDescriptor.Panel current() { 156 return panels[index]; 157 } 158 159 public final void addChangeListener(ChangeListener l) {} 161 public final void removeChangeListener(ChangeListener l) {} 162 163 private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException { 164 try { 165 ZipInputStream str = new ZipInputStream (source); 166 ZipEntry entry; 167 while ((entry = str.getNextEntry()) != null) { 168 if (entry.isDirectory()) { 169 FileUtil.createFolder(projectRoot, entry.getName()); 170 } else { 171 FileObject fo = FileUtil.createData(projectRoot, entry.getName()); 172 FileLock lock = fo.lock(); 173 try { 174 OutputStream out = fo.getOutputStream(lock); 175 try { 176 FileUtil.copy(str, out); 177 } finally { 178 out.close(); 179 } 180 } finally { 181 lock.releaseLock(); 182 } 183 } 184 } 185 } finally { 186 source.close(); 187 } 188 } 189 190 } 191 | Popular Tags |