001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.samples.bundled.wizard;
042:
043: import java.awt.Component;
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.OutputStream;
048: import java.util.Enumeration;
049: import java.util.LinkedHashSet;
050: import java.util.NoSuchElementException;
051: import java.util.Set;
052: import java.util.zip.ZipEntry;
053: import java.util.zip.ZipInputStream;
054: import javax.swing.JComponent;
055: import javax.swing.event.ChangeListener;
056:
057: import org.netbeans.api.project.ProjectManager;
058: import org.netbeans.spi.project.ui.support.ProjectChooser;
059: import org.netbeans.spi.project.ui.templates.support.Templates;
060: import org.openide.WizardDescriptor;
061: import org.openide.cookies.OpenCookie;
062: import org.openide.filesystems.FileLock;
063: import org.openide.filesystems.FileObject;
064: import org.openide.filesystems.FileUtil;
065: import org.openide.loaders.DataObject;
066:
067: public final class SamplesWebWizardIterator implements
068: WizardDescriptor.InstantiatingIterator {
069:
070: public SamplesWebWizardIterator() {
071: }
072:
073: public static SamplesWebWizardIterator createIterator() {
074: return new SamplesWebWizardIterator();
075: }
076:
077: private int index;
078: private WizardDescriptor.Panel[] panels;
079: protected WizardDescriptor wizard;
080:
081: /**
082: * Initialize panels representing individual wizard's steps and sets
083: * various properties for them influencing wizard appearance.
084: */
085: private WizardDescriptor.Panel[] getPanels() {
086: if (panels == null) {
087: panels = new WizardDescriptor.Panel[] { new SamplesWebWizardPanel() };
088: String[] steps = new String[panels.length];
089: for (int i = 0; i < panels.length; i++) {
090: Component c = panels[i].getComponent();
091: // Default step name to component name of panel.
092: steps[i] = c.getName();
093: if (c instanceof JComponent) { // assume Swing components
094: JComponent jc = (JComponent) c;
095: // Sets step number of a component
096: jc.putClientProperty(
097: "WizardPanel_contentSelectedIndex",
098: new Integer(i));
099: // Sets steps names for a panel
100: jc.putClientProperty("WizardPanel_contentData",
101: steps);
102: // Turn on subtitle creation on each step
103: jc.putClientProperty("WizardPanel_autoWizardStyle",
104: Boolean.TRUE);
105: // Show steps on the left side with the image on the background
106: jc.putClientProperty(
107: "WizardPanel_contentDisplayed",
108: Boolean.TRUE);
109: // Turn on numbering of all steps
110: jc.putClientProperty("WizardPanel_contentNumbered",
111: Boolean.TRUE);
112: }
113: }
114: }
115: return panels;
116: }
117:
118: public Set instantiate() throws IOException {
119: Set<FileObject> resultSet = new LinkedHashSet<FileObject>();
120: File projectDirectoryFile = FileUtil
121: .normalizeFile((File) this .wizard
122: .getProperty(WizardProperties.PROJ_DIR));
123: FileUtil.createFolder(projectDirectoryFile);
124:
125: FileObject template = Templates.getTemplate(this .wizard);
126: FileObject projectDirectoryFileObject = FileUtil
127: .toFileObject(projectDirectoryFile);
128: unZipFile(template.getInputStream(), projectDirectoryFileObject);
129: ProjectManager.getDefault().clearNonProjectCache();
130:
131: resultSet.add(projectDirectoryFileObject);
132: DataObject projectDirectoryDataObject = DataObject
133: .find(projectDirectoryFileObject);
134: Enumeration e = projectDirectoryFileObject.getFolders(true);
135: while (e.hasMoreElements()) {
136: FileObject subfolder = (FileObject) e.nextElement();
137: if (ProjectManager.getDefault().isProject(subfolder)) {
138: resultSet.add(subfolder);
139: }
140: }
141: Boolean isSetAsMainProject = (Boolean) wizard
142: .getProperty(WizardProperties.SET_MAIN_PROJ);
143: File parent = projectDirectoryFile.getParentFile();
144: if (isSetAsMainProject.booleanValue() && parent != null
145: && parent.exists()) {
146: ProjectChooser.setProjectsFolder(parent);
147: }
148: // Open the new project
149: OpenCookie openCookie = (OpenCookie) projectDirectoryDataObject
150: .getCookie(OpenCookie.class);
151: if (openCookie != null) {
152: openCookie.open();
153: }
154: return resultSet;
155: }
156:
157: private static void unZipFile(InputStream source,
158: FileObject projectRoot) throws IOException {
159: try {
160: ZipInputStream zipInputStream = new ZipInputStream(source);
161: ZipEntry entry;
162: while ((entry = zipInputStream.getNextEntry()) != null) {
163: if (entry.isDirectory()) {
164: FileUtil.createFolder(projectRoot, entry.getName());
165: } else {
166: FileObject fileObject = FileUtil.createData(
167: projectRoot, entry.getName());
168: FileLock lock = fileObject.lock();
169: try {
170: OutputStream outputStream = fileObject
171: .getOutputStream(lock);
172: try {
173: FileUtil.copy(zipInputStream, outputStream);
174: } finally {
175: outputStream.close();
176: }
177: } finally {
178: lock.releaseLock();
179: }
180: }
181: }
182: } finally {
183: source.close();
184: }
185: }
186:
187: public void initialize(WizardDescriptor wizard) {
188: this .wizard = wizard;
189: FileObject template = Templates.getTemplate(wizard);
190: wizard.putProperty(WizardProperties.NAME, template.getName());
191: wizard.putProperty(WizardProperties.SET_MAIN_PROJ, Boolean
192: .valueOf(true));
193: }
194:
195: public void uninitialize(WizardDescriptor wizard) {
196: this .wizard.putProperty(WizardProperties.NAME, null);
197: this .wizard.putProperty(WizardProperties.PROJ_DIR, null);
198: this .wizard.putProperty(WizardProperties.SET_MAIN_PROJ, null);
199: this .wizard = null;
200: panels = null;
201: }
202:
203: public WizardDescriptor.Panel current() {
204: return getPanels()[index];
205: }
206:
207: public String name() {
208: return index + 1 + ". from " + getPanels().length;
209: }
210:
211: public boolean hasNext() {
212: return index < getPanels().length - 1;
213: }
214:
215: public boolean hasPrevious() {
216: return index > 0;
217: }
218:
219: public void nextPanel() {
220: if (!hasNext()) {
221: throw new NoSuchElementException();
222: }
223: index++;
224: }
225:
226: public void previousPanel() {
227: if (!hasPrevious()) {
228: throw new NoSuchElementException();
229: }
230: index--;
231: }
232:
233: // If nothing unusual changes in the middle of the wizard, simply:
234: public void addChangeListener(ChangeListener l) {
235: }
236:
237: public void removeChangeListener(ChangeListener l) {
238: }
239:
240: // If something changes dynamically (besides moving between panels), e.g.
241: // the number of panels changes in response to user input, then uncomment
242: // the following and call when needed: fireChangeEvent();
243: /*
244: private Set<ChangeListener> listeners = new HashSet<ChangeListener>(1);
245: public final void addChangeListener(ChangeListener l) {
246: synchronized (listeners) {
247: listeners.add(l);
248: }
249: }
250: public final void removeChangeListener(ChangeListener l) {
251: synchronized (listeners) {
252: listeners.remove(l);
253: }
254: }
255: protected final void fireChangeEvent() {
256: Iterator<ChangeListener> it;
257: synchronized (listeners) {
258: it = new HashSet<ChangeListener>(listeners).iterator();
259: }
260: ChangeEvent ev = new ChangeEvent(this);
261: while (it.hasNext()) {
262: it.next().stateChanged(ev);
263: }
264: }
265: */
266:
267: // You could safely ignore this method. Is is here to keep steps which were
268: // there before this wizard was instantiated. It should be better handled
269: // by NetBeans Wizard API itself rather than needed to be implemented by a
270: // client code.
271: private String[] createSteps() {
272: String[] beforeSteps = null;
273: Object prop = wizard.getProperty("WizardPanel_contentData");
274: if (prop != null && prop instanceof String[]) {
275: beforeSteps = (String[]) prop;
276: }
277:
278: if (beforeSteps == null) {
279: beforeSteps = new String[0];
280: }
281:
282: String[] res = new String[(beforeSteps.length - 1)
283: + panels.length];
284: for (int i = 0; i < res.length; i++) {
285: if (i < (beforeSteps.length - 1)) {
286: res[i] = beforeSteps[i];
287: } else {
288: res[i] = panels[i - beforeSteps.length + 1]
289: .getComponent().getName();
290: }
291: }
292: return res;
293: }
294:
295: }
|