KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > samples > JavaEESamplesWizardIterator


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