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