1 19 20 package org.netbeans.modules.scripting.php.webproject.wizard; 21 22 import org.netbeans.modules.scripting.php.webproject.PhpProjectProperties; 23 import java.awt.Component ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.text.MessageFormat ; 29 import java.util.LinkedHashSet ; 30 import java.util.NoSuchElementException ; 31 import java.util.Set ; 32 import java.util.zip.ZipEntry ; 33 import java.util.zip.ZipInputStream ; 34 import javax.swing.JComponent ; 35 import javax.swing.event.ChangeListener ; 36 import org.netbeans.modules.scripting.php.webproject.PhpProject; 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.FileUtil; 44 import org.openide.util.NbBundle; 45 46 public class PHPWebProjectWizardIterator implements WizardDescriptor.InstantiatingIterator { 47 48 private int index; 49 private WizardDescriptor.Panel[] panels; 50 private WizardDescriptor wiz; 51 52 public PHPWebProjectWizardIterator() {} 53 54 public static PHPWebProjectWizardIterator createIterator() { 55 return new PHPWebProjectWizardIterator(); 56 } 57 58 private WizardDescriptor.Panel[] createPanels() { 59 return new WizardDescriptor.Panel[] { 60 new PHPWebProjectWizardPanel(), 61 }; 62 } 63 64 private String [] createSteps() { 65 return new String [] { 66 NbBundle.getMessage(PHPWebProjectWizardIterator.class, "LBL_CreateProjectStep") 67 }; 68 } 69 70 public Set <FileObject> instantiate() throws IOException { 71 Set <FileObject> resultSet = new LinkedHashSet <FileObject>(); 72 File dirF = FileUtil.normalizeFile((File ) wiz.getProperty(PhpProjectProperties.PROJECT_DIR)); 73 dirF.mkdirs(); 74 75 FileObject template = Templates.getTemplate(wiz); 76 FileObject dir = FileUtil.toFileObject(dirF); 77 unZipFile(template.getInputStream(), dir); 78 79 String welcomePage = (String )wiz.getProperty(PhpProjectProperties.WELCOME_PAGE); 81 82 if (welcomePage != null && !welcomePage.equals(PhpProjectProperties.DEFAULT_START_PAGE)) { 83 File welcomePageF = new File (dirF, PhpProjectProperties.DEFAULT_START_PAGE); 84 File newWelcomePageF = new File (dirF, welcomePage); 85 86 welcomePageF.renameTo(newWelcomePageF); 87 } 88 89 resultSet.add(dir); 91 92 PhpProject project = (PhpProject)ProjectManager.getDefault().findProject(dir); 93 94 project.setProperty(PhpProjectProperties.NAME, 95 (String )wiz.getProperty(PhpProjectProperties.NAME)); 96 project.setProperty(PhpProjectProperties.TARGET_DIR, 97 (String )wiz.getProperty(PhpProjectProperties.TARGET_DIR)); 98 project.setProperty(PhpProjectProperties.URL, 99 (String )wiz.getProperty(PhpProjectProperties.URL)); 100 project.setProperty(PhpProjectProperties.USE_SOURCE_DIR, 101 ((Boolean )wiz.getProperty(PhpProjectProperties.USE_SOURCE_DIR)).toString()); 102 project.setProperty(PhpProjectProperties.WELCOME_PAGE, welcomePage); 103 project.save(); 104 105 File parent = dirF.getParentFile(); 106 if (parent != null && parent.exists()) { 107 ProjectChooser.setProjectsFolder(parent); 108 } 109 110 return resultSet; 111 } 112 113 public void initialize(WizardDescriptor wiz) { 114 this.wiz = wiz; 115 index = 0; 116 panels = createPanels(); 117 String [] steps = createSteps(); 119 for (int i = 0; i < panels.length; i++) { 120 Component c = panels[i].getComponent(); 121 if (steps[i] == null) { 122 steps[i] = c.getName(); 126 } 127 if (c instanceof JComponent ) { JComponent jc = (JComponent ) c; 129 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); 131 jc.putClientProperty("WizardPanel_contentData", steps); 133 } 134 } 135 } 136 137 public void uninitialize(WizardDescriptor wiz) { 138 this.wiz.putProperty(PhpProjectProperties.PROJECT_DIR, null); 139 this.wiz.putProperty(PhpProjectProperties.NAME, null); 140 this.wiz = null; 141 panels = null; 142 } 143 144 public String name() { 145 return MessageFormat.format("{0} of {1}", 146 new Object [] {new Integer (index + 1), new Integer (panels.length)}); 147 } 148 149 public boolean hasNext() { 150 return index < panels.length - 1; 151 } 152 153 public boolean hasPrevious() { 154 return index > 0; 155 } 156 157 public void nextPanel() { 158 if (!hasNext()) { 159 throw new NoSuchElementException (); 160 } 161 index++; 162 } 163 164 public void previousPanel() { 165 if (!hasPrevious()) { 166 throw new NoSuchElementException (); 167 } 168 index--; 169 } 170 171 public WizardDescriptor.Panel current() { 172 return panels[index]; 173 } 174 175 public final void addChangeListener(ChangeListener l) {} 177 public final void removeChangeListener(ChangeListener l) {} 178 179 private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException { 180 try { 181 ZipInputStream str = new ZipInputStream (source); 182 ZipEntry entry; 183 while ((entry = str.getNextEntry()) != null) { 184 if (entry.isDirectory()) { 185 FileUtil.createFolder(projectRoot, entry.getName()); 186 } else { 187 FileObject fo = FileUtil.createData(projectRoot, entry.getName()); 188 FileLock lock = fo.lock(); 189 try { 190 OutputStream out = fo.getOutputStream(lock); 191 try { 192 FileUtil.copy(str, out); 193 } finally { 194 out.close(); 195 } 196 } finally { 197 lock.releaseLock(); 198 } 199 } 200 } 201 } finally { 202 source.close(); 203 } 204 } 205 206 } 207 | Popular Tags |