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