samples/apifest1/infrastructure/testing-template/src/org/netbeans/apifest/testingtemplate/TestingTemplateWizardIterator.java
Adding samples from API fest to the repository, including pieces of their code in the document, not just links
1 package org.netbeans.apifest.testingtemplate;
3 import java.awt.Component;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8 import java.text.MessageFormat;
9 import java.util.Enumeration;
10 import java.util.LinkedHashSet;
11 import java.util.NoSuchElementException;
13 import java.util.zip.ZipEntry;
14 import java.util.zip.ZipInputStream;
15 import javax.swing.JComponent;
16 import javax.swing.event.ChangeListener;
17 import org.netbeans.api.project.ProjectManager;
18 import org.netbeans.spi.project.ui.support.ProjectChooser;
19 import org.netbeans.spi.project.ui.templates.support.Templates;
20 import org.openide.WizardDescriptor;
21 import org.openide.filesystems.FileLock;
22 import org.openide.filesystems.FileObject;
23 import org.openide.filesystems.FileUtil;
24 import org.openide.util.NbBundle;
26 public class TestingTemplateWizardIterator implements WizardDescriptor.InstantiatingIterator {
29 private WizardDescriptor.Panel[] panels;
30 private WizardDescriptor wiz;
32 public TestingTemplateWizardIterator() {}
34 public static TestingTemplateWizardIterator createIterator() {
35 return new TestingTemplateWizardIterator();
38 private WizardDescriptor.Panel[] createPanels() {
39 return new WizardDescriptor.Panel[] {
40 new TestingTemplateWizardPanel(),
44 private String[] createSteps() {
46 NbBundle.getMessage(TestingTemplateWizardIterator.class, "LBL_CreateProjectStep")
50 public Set/*<FileObject>*/ instantiate() throws IOException {
51 Set resultSet = new LinkedHashSet();
52 File dirF = FileUtil.normalizeFile((File) wiz.getProperty("projdir"));
55 FileObject template = Templates.getTemplate(wiz);
56 FileObject dir = FileUtil.toFileObject(dirF);
57 unZipFile(template.getInputStream(), dir);
59 // Always open top dir as a project:
61 // Look for nested projects to open as well:
62 Enumeration e = dir.getFolders(true);
63 while (e.hasMoreElements()) {
64 FileObject subfolder = (FileObject) e.nextElement();
65 if (ProjectManager.getDefault().isProject(subfolder)) {
66 resultSet.add(subfolder);
70 File parent = dirF.getParentFile();
71 if (parent != null && parent.exists()) {
72 ProjectChooser.setProjectsFolder(parent);
78 public void initialize(WizardDescriptor wiz) {
81 panels = createPanels();
82 // Make sure list of steps is accurate.
83 String[] steps = createSteps();
84 for (int i = 0; i < panels.length; i++) {
85 Component c = panels[i].getComponent();
86 if (steps[i] == null) {
87 // Default step name to component name of panel.
88 // Mainly useful for getting the name of the target
89 // chooser to appear in the list of steps.
90 steps[i] = c.getName();
92 if (c instanceof JComponent) { // assume Swing components
93 JComponent jc = (JComponent) c;
95 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
96 // Step name (actually the whole list for reference).
97 jc.putClientProperty("WizardPanel_contentData", steps);
102 public void uninitialize(WizardDescriptor wiz) {
103 this.wiz.putProperty("projdir",null);
104 this.wiz.putProperty("name",null);
109 public String name() {
110 return MessageFormat.format("{0} of {1}",
111 new Object[] {new Integer(index + 1), new Integer(panels.length)});
114 public boolean hasNext() {
115 return index < panels.length - 1;
118 public boolean hasPrevious() {
122 public void nextPanel() {
124 throw new NoSuchElementException();
129 public void previousPanel() {
130 if (!hasPrevious()) {
131 throw new NoSuchElementException();
136 public WizardDescriptor.Panel current() {
137 return panels[index];
140 // If nothing unusual changes in the middle of the wizard, simply:
141 public final void addChangeListener(ChangeListener l) {}
142 public final void removeChangeListener(ChangeListener l) {}
144 private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException {
146 ZipInputStream str = new ZipInputStream(source);
148 while ((entry = str.getNextEntry()) != null) {
149 if (entry.isDirectory()) {
150 FileUtil.createFolder(projectRoot, entry.getName());
152 FileObject fo = FileUtil.createData(projectRoot, entry.getName());
153 FileLock lock = fo.lock();
155 OutputStream out = fo.getOutputStream(lock);
157 FileUtil.copy(str, out);