1 19 20 package org.netbeans.modules.j2ee.samples; 21 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.util.Enumeration ; 29 import java.util.LinkedHashSet ; 30 import java.util.NoSuchElementException ; 31 import java.util.Set ; 32 import java.util.Stack ; 33 import java.util.zip.ZipEntry ; 34 import java.util.zip.ZipInputStream ; 35 import javax.swing.JComponent ; 36 import javax.swing.event.ChangeListener ; 37 import org.netbeans.api.project.ProjectManager; 38 import org.netbeans.spi.project.ui.support.ProjectChooser; 39 import org.netbeans.spi.project.ui.templates.support.Templates; 40 import org.openide.WizardDescriptor; 41 import org.openide.filesystems.FileLock; 42 import org.openide.filesystems.FileObject; 43 import org.openide.filesystems.FileStateInvalidException; 44 import org.openide.filesystems.FileUtil; 45 import org.openide.util.NbBundle; 46 47 public class JavaEESamplesWizardIterator implements WizardDescriptor.InstantiatingIterator { 48 49 private int index; 50 private WizardDescriptor.Panel[] panels; 51 protected WizardDescriptor wiz; 52 53 public JavaEESamplesWizardIterator() {} 54 55 public static JavaEESamplesWizardIterator createIterator() { 56 return new JavaEESamplesWizardIterator(); 57 } 58 59 protected WizardDescriptor.Panel[] createPanels() { 60 return new WizardDescriptor.Panel[] { 61 new JavaEESamplesWizardPanel(false) 62 }; 63 } 64 65 protected String [] createSteps() { 66 return new String [] { 67 NbBundle.getMessage(JavaEESamplesWizardIterator.class, "LBL_CreateProjectStep") 68 }; 69 } 70 71 public Set instantiate() throws IOException { 72 Set resultSet = new LinkedHashSet (); 73 File dirF = FileUtil.normalizeFile((File ) wiz.getProperty(WizardProperties.PROJ_DIR)); 74 createFolder(dirF); 75 76 FileObject template = Templates.getTemplate(wiz); 77 FileObject dir = FileUtil.toFileObject(dirF); 78 unZipFile(template.getInputStream(), dir); 79 ProjectManager.getDefault().clearNonProjectCache(); 80 81 resultSet.add(dir); 83 Enumeration e = dir.getFolders(true); 85 while (e.hasMoreElements()) { 86 FileObject subfolder = (FileObject) e.nextElement(); 87 if (ProjectManager.getDefault().isProject(subfolder)) { 88 resultSet.add(subfolder); 89 } 90 } 91 92 File parent = dirF.getParentFile(); 93 if (parent != null && parent.exists()) { 94 ProjectChooser.setProjectsFolder(parent); 95 } 96 97 return resultSet; 98 } 99 100 public void initialize(WizardDescriptor wiz) { 101 this.wiz = wiz; 102 index = 0; 103 panels = createPanels(); 104 String [] steps = createSteps(); 106 for (int i = 0; i < panels.length; i++) { 107 Component c = panels[i].getComponent(); 108 if (steps[i] == null) { 109 steps[i] = c.getName(); 113 } 114 if (c instanceof JComponent ) { JComponent jc = (JComponent ) c; 116 jc.putClientProperty(WizardProperties.SELECTED_INDEX, new Integer (i)); 118 jc.putClientProperty(WizardProperties.CONTENT_DATA, steps); 120 } 121 } 122 123 FileObject template = Templates.getTemplate(wiz); 124 125 wiz.putProperty(WizardProperties.NAME, template.getName()); 126 } 127 128 public void uninitialize(WizardDescriptor wiz) { 129 this.wiz.putProperty(WizardProperties.PROJ_DIR, null); 130 this.wiz.putProperty(WizardProperties.NAME, null); 131 this.wiz = null; 132 panels = null; 133 } 134 135 public String name() { 136 return NbBundle.getMessage(JavaEESamplesWizardIterator.class, "LBL_Order", 137 new Object [] {new Integer (index + 1), new Integer (panels.length)}); 138 } 139 140 public boolean hasNext() { 141 return index < panels.length - 1; 142 } 143 144 public boolean hasPrevious() { 145 return index > 0; 146 } 147 148 public void nextPanel() { 149 if (!hasNext()) { 150 throw new NoSuchElementException (); 151 } 152 index++; 153 } 154 155 public void previousPanel() { 156 if (!hasPrevious()) { 157 throw new NoSuchElementException (); 158 } 159 index--; 160 } 161 162 public WizardDescriptor.Panel current() { 163 return panels[index]; 164 } 165 166 public final void addChangeListener(ChangeListener l) {} 168 public final void removeChangeListener(ChangeListener l) {} 169 170 private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException { 171 try { 172 ZipInputStream str = new ZipInputStream (source); 173 ZipEntry entry; 174 while ((entry = str.getNextEntry()) != null) { 175 if (entry.isDirectory()) { 176 FileUtil.createFolder(projectRoot, entry.getName()); 177 } else { 178 FileObject fo = FileUtil.createData(projectRoot, entry.getName()); 179 FileLock lock = fo.lock(); 180 try { 181 OutputStream out = fo.getOutputStream(lock); 182 try { 183 FileUtil.copy(str, out); 184 } finally { 185 out.close(); 186 } 187 } finally { 188 lock.releaseLock(); 189 } 190 } 191 } 192 } finally { 193 source.close(); 194 } 195 } 196 197 198 private static FileObject createFolder(File dir) throws IOException { 199 Stack stack = new Stack (); 200 while (!dir.exists()) { 201 stack.push(dir.getName()); 202 dir = dir.getParentFile(); 203 } 204 FileObject dirFO = FileUtil.toFileObject(dir); 205 if (dirFO == null) { 206 refreshFileSystem(dir); 207 dirFO = FileUtil.toFileObject(dir); 208 } 209 assert dirFO != null; 210 while (!stack.isEmpty()) { 211 dirFO = dirFO.createFolder((String )stack.pop()); 212 } 213 return dirFO; 214 } 215 216 private static void refreshFileSystem(final File dir) throws FileStateInvalidException { 217 File rootF = dir; 218 while (rootF.getParentFile() != null) { 219 rootF = rootF.getParentFile(); 220 } 221 FileObject dirFO = FileUtil.toFileObject(rootF); 222 assert dirFO != null : "At least disk roots must be mounted! " + rootF; dirFO.getFileSystem().refresh(false); 224 } 225 226 } 227 | Popular Tags |