samples/apifest1/infrastructure/testing-template/src/org/netbeans/apifest/testingtemplate/TestingTemplateWizardIterator.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/apifest1/infrastructure/testing-template/src/org/netbeans/apifest/testingtemplate/TestingTemplateWizardIterator.java Sat Jun 14 09:52:45 2008 +0200
1.3 @@ -0,0 +1,171 @@
1.4 +package org.netbeans.apifest.testingtemplate;
1.5 +
1.6 +import java.awt.Component;
1.7 +import java.io.File;
1.8 +import java.io.IOException;
1.9 +import java.io.InputStream;
1.10 +import java.io.OutputStream;
1.11 +import java.text.MessageFormat;
1.12 +import java.util.Enumeration;
1.13 +import java.util.LinkedHashSet;
1.14 +import java.util.NoSuchElementException;
1.15 +import java.util.Set;
1.16 +import java.util.zip.ZipEntry;
1.17 +import java.util.zip.ZipInputStream;
1.18 +import javax.swing.JComponent;
1.19 +import javax.swing.event.ChangeListener;
1.20 +import org.netbeans.api.project.ProjectManager;
1.21 +import org.netbeans.spi.project.ui.support.ProjectChooser;
1.22 +import org.netbeans.spi.project.ui.templates.support.Templates;
1.23 +import org.openide.WizardDescriptor;
1.24 +import org.openide.filesystems.FileLock;
1.25 +import org.openide.filesystems.FileObject;
1.26 +import org.openide.filesystems.FileUtil;
1.27 +import org.openide.util.NbBundle;
1.28 +
1.29 +public class TestingTemplateWizardIterator implements WizardDescriptor.InstantiatingIterator {
1.30 +
1.31 + private int index;
1.32 + private WizardDescriptor.Panel[] panels;
1.33 + private WizardDescriptor wiz;
1.34 +
1.35 + public TestingTemplateWizardIterator() {}
1.36 +
1.37 + public static TestingTemplateWizardIterator createIterator() {
1.38 + return new TestingTemplateWizardIterator();
1.39 + }
1.40 +
1.41 + private WizardDescriptor.Panel[] createPanels() {
1.42 + return new WizardDescriptor.Panel[] {
1.43 + new TestingTemplateWizardPanel(),
1.44 + };
1.45 + }
1.46 +
1.47 + private String[] createSteps() {
1.48 + return new String[] {
1.49 + NbBundle.getMessage(TestingTemplateWizardIterator.class, "LBL_CreateProjectStep")
1.50 + };
1.51 + }
1.52 +
1.53 + public Set/*<FileObject>*/ instantiate() throws IOException {
1.54 + Set resultSet = new LinkedHashSet();
1.55 + File dirF = FileUtil.normalizeFile((File) wiz.getProperty("projdir"));
1.56 + dirF.mkdirs();
1.57 +
1.58 + FileObject template = Templates.getTemplate(wiz);
1.59 + FileObject dir = FileUtil.toFileObject(dirF);
1.60 + unZipFile(template.getInputStream(), dir);
1.61 +
1.62 + // Always open top dir as a project:
1.63 + resultSet.add(dir);
1.64 + // Look for nested projects to open as well:
1.65 + Enumeration e = dir.getFolders(true);
1.66 + while (e.hasMoreElements()) {
1.67 + FileObject subfolder = (FileObject) e.nextElement();
1.68 + if (ProjectManager.getDefault().isProject(subfolder)) {
1.69 + resultSet.add(subfolder);
1.70 + }
1.71 + }
1.72 +
1.73 + File parent = dirF.getParentFile();
1.74 + if (parent != null && parent.exists()) {
1.75 + ProjectChooser.setProjectsFolder(parent);
1.76 + }
1.77 +
1.78 + return resultSet;
1.79 + }
1.80 +
1.81 + public void initialize(WizardDescriptor wiz) {
1.82 + this.wiz = wiz;
1.83 + index = 0;
1.84 + panels = createPanels();
1.85 + // Make sure list of steps is accurate.
1.86 + String[] steps = createSteps();
1.87 + for (int i = 0; i < panels.length; i++) {
1.88 + Component c = panels[i].getComponent();
1.89 + if (steps[i] == null) {
1.90 + // Default step name to component name of panel.
1.91 + // Mainly useful for getting the name of the target
1.92 + // chooser to appear in the list of steps.
1.93 + steps[i] = c.getName();
1.94 + }
1.95 + if (c instanceof JComponent) { // assume Swing components
1.96 + JComponent jc = (JComponent) c;
1.97 + // Step #.
1.98 + jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
1.99 + // Step name (actually the whole list for reference).
1.100 + jc.putClientProperty("WizardPanel_contentData", steps);
1.101 + }
1.102 + }
1.103 + }
1.104 +
1.105 + public void uninitialize(WizardDescriptor wiz) {
1.106 + this.wiz.putProperty("projdir",null);
1.107 + this.wiz.putProperty("name",null);
1.108 + this.wiz = null;
1.109 + panels = null;
1.110 + }
1.111 +
1.112 + public String name() {
1.113 + return MessageFormat.format("{0} of {1}",
1.114 + new Object[] {new Integer(index + 1), new Integer(panels.length)});
1.115 + }
1.116 +
1.117 + public boolean hasNext() {
1.118 + return index < panels.length - 1;
1.119 + }
1.120 +
1.121 + public boolean hasPrevious() {
1.122 + return index > 0;
1.123 + }
1.124 +
1.125 + public void nextPanel() {
1.126 + if (!hasNext()) {
1.127 + throw new NoSuchElementException();
1.128 + }
1.129 + index++;
1.130 + }
1.131 +
1.132 + public void previousPanel() {
1.133 + if (!hasPrevious()) {
1.134 + throw new NoSuchElementException();
1.135 + }
1.136 + index--;
1.137 + }
1.138 +
1.139 + public WizardDescriptor.Panel current() {
1.140 + return panels[index];
1.141 + }
1.142 +
1.143 + // If nothing unusual changes in the middle of the wizard, simply:
1.144 + public final void addChangeListener(ChangeListener l) {}
1.145 + public final void removeChangeListener(ChangeListener l) {}
1.146 +
1.147 + private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException {
1.148 + try {
1.149 + ZipInputStream str = new ZipInputStream(source);
1.150 + ZipEntry entry;
1.151 + while ((entry = str.getNextEntry()) != null) {
1.152 + if (entry.isDirectory()) {
1.153 + FileUtil.createFolder(projectRoot, entry.getName());
1.154 + } else {
1.155 + FileObject fo = FileUtil.createData(projectRoot, entry.getName());
1.156 + FileLock lock = fo.lock();
1.157 + try {
1.158 + OutputStream out = fo.getOutputStream(lock);
1.159 + try {
1.160 + FileUtil.copy(str, out);
1.161 + } finally {
1.162 + out.close();
1.163 + }
1.164 + } finally {
1.165 + lock.releaseLock();
1.166 + }
1.167 + }
1.168 + }
1.169 + } finally {
1.170 + source.close();
1.171 + }
1.172 + }
1.173 +
1.174 +}