KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > wizard > EmptyBluejWizardIterator


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