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
jtulach@52
     1
package org.netbeans.apifest.testingtemplate;
jtulach@52
     2
jtulach@52
     3
import java.awt.Component;
jtulach@52
     4
import java.io.File;
jtulach@52
     5
import java.io.IOException;
jtulach@52
     6
import java.io.InputStream;
jtulach@52
     7
import java.io.OutputStream;
jtulach@52
     8
import java.text.MessageFormat;
jtulach@52
     9
import java.util.Enumeration;
jtulach@52
    10
import java.util.LinkedHashSet;
jtulach@52
    11
import java.util.NoSuchElementException;
jtulach@52
    12
import java.util.Set;
jtulach@52
    13
import java.util.zip.ZipEntry;
jtulach@52
    14
import java.util.zip.ZipInputStream;
jtulach@52
    15
import javax.swing.JComponent;
jtulach@52
    16
import javax.swing.event.ChangeListener;
jtulach@52
    17
import org.netbeans.api.project.ProjectManager;
jtulach@52
    18
import org.netbeans.spi.project.ui.support.ProjectChooser;
jtulach@52
    19
import org.netbeans.spi.project.ui.templates.support.Templates;
jtulach@52
    20
import org.openide.WizardDescriptor;
jtulach@52
    21
import org.openide.filesystems.FileLock;
jtulach@52
    22
import org.openide.filesystems.FileObject;
jtulach@52
    23
import org.openide.filesystems.FileUtil;
jtulach@52
    24
import org.openide.util.NbBundle;
jtulach@52
    25
jtulach@52
    26
public class TestingTemplateWizardIterator implements WizardDescriptor.InstantiatingIterator {
jtulach@52
    27
    
jtulach@52
    28
    private int index;
jtulach@52
    29
    private WizardDescriptor.Panel[] panels;
jtulach@52
    30
    private WizardDescriptor wiz;
jtulach@52
    31
    
jtulach@52
    32
    public TestingTemplateWizardIterator() {}
jtulach@52
    33
    
jtulach@52
    34
    public static TestingTemplateWizardIterator createIterator() {
jtulach@52
    35
        return new TestingTemplateWizardIterator();
jtulach@52
    36
    }
jtulach@52
    37
    
jtulach@52
    38
    private WizardDescriptor.Panel[] createPanels() {
jtulach@52
    39
        return new WizardDescriptor.Panel[] {
jtulach@52
    40
            new TestingTemplateWizardPanel(),
jtulach@52
    41
        };
jtulach@52
    42
    }
jtulach@52
    43
    
jtulach@52
    44
    private String[] createSteps() {
jtulach@52
    45
        return new String[] {
jtulach@52
    46
            NbBundle.getMessage(TestingTemplateWizardIterator.class, "LBL_CreateProjectStep")
jtulach@52
    47
        };
jtulach@52
    48
    }
jtulach@52
    49
    
jtulach@52
    50
    public Set/*<FileObject>*/ instantiate() throws IOException {
jtulach@52
    51
        Set resultSet = new LinkedHashSet();
jtulach@52
    52
        File dirF = FileUtil.normalizeFile((File) wiz.getProperty("projdir"));
jtulach@52
    53
        dirF.mkdirs();
jtulach@52
    54
        
jtulach@52
    55
        FileObject template = Templates.getTemplate(wiz);
jtulach@52
    56
        FileObject dir = FileUtil.toFileObject(dirF);
jtulach@52
    57
        unZipFile(template.getInputStream(), dir);
jtulach@52
    58
        
jtulach@52
    59
        // Always open top dir as a project:
jtulach@52
    60
        resultSet.add(dir);
jtulach@52
    61
        // Look for nested projects to open as well:
jtulach@52
    62
        Enumeration e = dir.getFolders(true);
jtulach@52
    63
        while (e.hasMoreElements()) {
jtulach@52
    64
            FileObject subfolder = (FileObject) e.nextElement();
jtulach@52
    65
            if (ProjectManager.getDefault().isProject(subfolder)) {
jtulach@52
    66
                resultSet.add(subfolder);
jtulach@52
    67
            }
jtulach@52
    68
        }
jtulach@52
    69
        
jtulach@52
    70
        File parent = dirF.getParentFile();
jtulach@52
    71
        if (parent != null && parent.exists()) {
jtulach@52
    72
            ProjectChooser.setProjectsFolder(parent);
jtulach@52
    73
        }
jtulach@52
    74
        
jtulach@52
    75
        return resultSet;
jtulach@52
    76
    }
jtulach@52
    77
    
jtulach@52
    78
    public void initialize(WizardDescriptor wiz) {
jtulach@52
    79
        this.wiz = wiz;
jtulach@52
    80
        index = 0;
jtulach@52
    81
        panels = createPanels();
jtulach@52
    82
        // Make sure list of steps is accurate.
jtulach@52
    83
        String[] steps = createSteps();
jtulach@52
    84
        for (int i = 0; i < panels.length; i++) {
jtulach@52
    85
            Component c = panels[i].getComponent();
jtulach@52
    86
            if (steps[i] == null) {
jtulach@52
    87
                // Default step name to component name of panel.
jtulach@52
    88
                // Mainly useful for getting the name of the target
jtulach@52
    89
                // chooser to appear in the list of steps.
jtulach@52
    90
                steps[i] = c.getName();
jtulach@52
    91
            }
jtulach@52
    92
            if (c instanceof JComponent) { // assume Swing components
jtulach@52
    93
                JComponent jc = (JComponent) c;
jtulach@52
    94
                // Step #.
jtulach@52
    95
                jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
jtulach@52
    96
                // Step name (actually the whole list for reference).
jtulach@52
    97
                jc.putClientProperty("WizardPanel_contentData", steps);
jtulach@52
    98
            }
jtulach@52
    99
        }
jtulach@52
   100
    }
jtulach@52
   101
    
jtulach@52
   102
    public void uninitialize(WizardDescriptor wiz) {
jtulach@52
   103
        this.wiz.putProperty("projdir",null);
jtulach@52
   104
        this.wiz.putProperty("name",null);
jtulach@52
   105
        this.wiz = null;
jtulach@52
   106
        panels = null;
jtulach@52
   107
    }
jtulach@52
   108
    
jtulach@52
   109
    public String name() {
jtulach@52
   110
        return MessageFormat.format("{0} of {1}",
jtulach@52
   111
            new Object[] {new Integer(index + 1), new Integer(panels.length)});
jtulach@52
   112
    }
jtulach@52
   113
    
jtulach@52
   114
    public boolean hasNext() {
jtulach@52
   115
        return index < panels.length - 1;
jtulach@52
   116
    }
jtulach@52
   117
    
jtulach@52
   118
    public boolean hasPrevious() {
jtulach@52
   119
        return index > 0;
jtulach@52
   120
    }
jtulach@52
   121
    
jtulach@52
   122
    public void nextPanel() {
jtulach@52
   123
        if (!hasNext()) {
jtulach@52
   124
            throw new NoSuchElementException();
jtulach@52
   125
        }
jtulach@52
   126
        index++;
jtulach@52
   127
    }
jtulach@52
   128
    
jtulach@52
   129
    public void previousPanel() {
jtulach@52
   130
        if (!hasPrevious()) {
jtulach@52
   131
            throw new NoSuchElementException();
jtulach@52
   132
        }
jtulach@52
   133
        index--;
jtulach@52
   134
    }
jtulach@52
   135
    
jtulach@52
   136
    public WizardDescriptor.Panel current() {
jtulach@52
   137
        return panels[index];
jtulach@52
   138
    }
jtulach@52
   139
    
jtulach@52
   140
    // If nothing unusual changes in the middle of the wizard, simply:
jtulach@52
   141
    public final void addChangeListener(ChangeListener l) {}
jtulach@52
   142
    public final void removeChangeListener(ChangeListener l) {}
jtulach@52
   143
    
jtulach@52
   144
    private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException {
jtulach@52
   145
        try {
jtulach@52
   146
            ZipInputStream str = new ZipInputStream(source);
jtulach@52
   147
            ZipEntry entry;
jtulach@52
   148
            while ((entry = str.getNextEntry()) != null) {
jtulach@52
   149
                if (entry.isDirectory()) {
jtulach@52
   150
                    FileUtil.createFolder(projectRoot, entry.getName());
jtulach@52
   151
                } else {
jtulach@52
   152
                    FileObject fo = FileUtil.createData(projectRoot, entry.getName());
jtulach@52
   153
                    FileLock lock = fo.lock();
jtulach@52
   154
                    try {
jtulach@52
   155
                        OutputStream out = fo.getOutputStream(lock);
jtulach@52
   156
                        try {
jtulach@52
   157
                            FileUtil.copy(str, out);
jtulach@52
   158
                        } finally {
jtulach@52
   159
                            out.close();
jtulach@52
   160
                        }
jtulach@52
   161
                    } finally {
jtulach@52
   162
                        lock.releaseLock();
jtulach@52
   163
                    }
jtulach@52
   164
                }
jtulach@52
   165
            }
jtulach@52
   166
        } finally {
jtulach@52
   167
            source.close();
jtulach@52
   168
        }
jtulach@52
   169
    }
jtulach@52
   170
    
jtulach@52
   171
}