samples/apifest1/infrastructure/testing-template/src/org/netbeans/apifest/testingtemplate/TestingTemplateWizardIterator.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:45 +0200
changeset 52 4257f4cf226b
permissions -rw-r--r--
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;
     2 
     3 import java.awt.Component;
     4 import java.io.File;
     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;
    12 import java.util.Set;
    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;
    25 
    26 public class TestingTemplateWizardIterator implements WizardDescriptor.InstantiatingIterator {
    27     
    28     private int index;
    29     private WizardDescriptor.Panel[] panels;
    30     private WizardDescriptor wiz;
    31     
    32     public TestingTemplateWizardIterator() {}
    33     
    34     public static TestingTemplateWizardIterator createIterator() {
    35         return new TestingTemplateWizardIterator();
    36     }
    37     
    38     private WizardDescriptor.Panel[] createPanels() {
    39         return new WizardDescriptor.Panel[] {
    40             new TestingTemplateWizardPanel(),
    41         };
    42     }
    43     
    44     private String[] createSteps() {
    45         return new String[] {
    46             NbBundle.getMessage(TestingTemplateWizardIterator.class, "LBL_CreateProjectStep")
    47         };
    48     }
    49     
    50     public Set/*<FileObject>*/ instantiate() throws IOException {
    51         Set resultSet = new LinkedHashSet();
    52         File dirF = FileUtil.normalizeFile((File) wiz.getProperty("projdir"));
    53         dirF.mkdirs();
    54         
    55         FileObject template = Templates.getTemplate(wiz);
    56         FileObject dir = FileUtil.toFileObject(dirF);
    57         unZipFile(template.getInputStream(), dir);
    58         
    59         // Always open top dir as a project:
    60         resultSet.add(dir);
    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);
    67             }
    68         }
    69         
    70         File parent = dirF.getParentFile();
    71         if (parent != null && parent.exists()) {
    72             ProjectChooser.setProjectsFolder(parent);
    73         }
    74         
    75         return resultSet;
    76     }
    77     
    78     public void initialize(WizardDescriptor wiz) {
    79         this.wiz = wiz;
    80         index = 0;
    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();
    91             }
    92             if (c instanceof JComponent) { // assume Swing components
    93                 JComponent jc = (JComponent) c;
    94                 // Step #.
    95                 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
    96                 // Step name (actually the whole list for reference).
    97                 jc.putClientProperty("WizardPanel_contentData", steps);
    98             }
    99         }
   100     }
   101     
   102     public void uninitialize(WizardDescriptor wiz) {
   103         this.wiz.putProperty("projdir",null);
   104         this.wiz.putProperty("name",null);
   105         this.wiz = null;
   106         panels = null;
   107     }
   108     
   109     public String name() {
   110         return MessageFormat.format("{0} of {1}",
   111             new Object[] {new Integer(index + 1), new Integer(panels.length)});
   112     }
   113     
   114     public boolean hasNext() {
   115         return index < panels.length - 1;
   116     }
   117     
   118     public boolean hasPrevious() {
   119         return index > 0;
   120     }
   121     
   122     public void nextPanel() {
   123         if (!hasNext()) {
   124             throw new NoSuchElementException();
   125         }
   126         index++;
   127     }
   128     
   129     public void previousPanel() {
   130         if (!hasPrevious()) {
   131             throw new NoSuchElementException();
   132         }
   133         index--;
   134     }
   135     
   136     public WizardDescriptor.Panel current() {
   137         return panels[index];
   138     }
   139     
   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) {}
   143     
   144     private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException {
   145         try {
   146             ZipInputStream str = new ZipInputStream(source);
   147             ZipEntry entry;
   148             while ((entry = str.getNextEntry()) != null) {
   149                 if (entry.isDirectory()) {
   150                     FileUtil.createFolder(projectRoot, entry.getName());
   151                 } else {
   152                     FileObject fo = FileUtil.createData(projectRoot, entry.getName());
   153                     FileLock lock = fo.lock();
   154                     try {
   155                         OutputStream out = fo.getOutputStream(lock);
   156                         try {
   157                             FileUtil.copy(str, out);
   158                         } finally {
   159                             out.close();
   160                         }
   161                     } finally {
   162                         lock.releaseLock();
   163                     }
   164                 }
   165             }
   166         } finally {
   167             source.close();
   168         }
   169     }
   170     
   171 }