KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > paintapp > PaintAppWizardIterator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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