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-2006 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.apisupport.paintapp;
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: import org.netbeans.api.project.ProjectManager;
057: import org.netbeans.spi.project.ui.support.ProjectChooser;
058: import org.netbeans.spi.project.ui.templates.support.Templates;
059: import org.openide.WizardDescriptor;
060: import org.openide.filesystems.FileLock;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.util.NbBundle;
064:
065: public class PaintAppWizardIterator implements
066: WizardDescriptor.InstantiatingIterator {
067:
068: private static final long serialVersionUID = 1L;
069:
070: private transient int index;
071: private transient WizardDescriptor.Panel[] panels;
072: private transient WizardDescriptor wiz;
073:
074: public PaintAppWizardIterator() {
075: }
076:
077: public static PaintAppWizardIterator createIterator() {
078: return new PaintAppWizardIterator();
079: }
080:
081: private WizardDescriptor.Panel[] createPanels() {
082: return new WizardDescriptor.Panel[] { new PaintAppWizardPanel(), };
083: }
084:
085: private String[] createSteps() {
086: return new String[] { NbBundle.getMessage(
087: PaintAppWizardIterator.class, "LBL_CreateProjectStep") };
088: }
089:
090: public Set/*<FileObject>*/instantiate() throws IOException {
091: Set resultSet = new LinkedHashSet();
092: File dirF = FileUtil.normalizeFile((File) wiz
093: .getProperty("projdir")); // NOI18N
094: dirF.mkdirs();
095:
096: FileObject template = Templates.getTemplate(wiz);
097: FileObject dir = FileUtil.toFileObject(dirF);
098: unZipFile(template.getInputStream(), dir);
099:
100: // Always open top dir as a project:
101: resultSet.add(dir);
102: // Look for nested projects to open as well:
103: Enumeration e = dir.getFolders(true);
104: while (e.hasMoreElements()) {
105: FileObject subfolder = (FileObject) e.nextElement();
106: if (ProjectManager.getDefault().isProject(subfolder)) {
107: resultSet.add(subfolder);
108: }
109: }
110:
111: File parent = dirF.getParentFile();
112: if (parent != null && parent.exists()) {
113: ProjectChooser.setProjectsFolder(parent);
114: }
115:
116: return resultSet;
117: }
118:
119: public void initialize(WizardDescriptor wiz) {
120: this .wiz = wiz;
121: index = 0;
122: panels = createPanels();
123: // Make sure list of steps is accurate.
124: String[] steps = createSteps();
125: for (int i = 0; i < panels.length; i++) {
126: Component c = panels[i].getComponent();
127: if (steps[i] == null) {
128: // Default step name to component name of panel.
129: // Mainly useful for getting the name of the target
130: // chooser to appear in the list of steps.
131: steps[i] = c.getName();
132: }
133: if (c instanceof JComponent) { // assume Swing components
134: JComponent jc = (JComponent) c;
135: // Step #.
136: jc.putClientProperty(
137: "WizardPanel_contentSelectedIndex",
138: new Integer(i)); // NOI18N
139: // Step name (actually the whole list for reference).
140: jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
141: }
142: }
143: }
144:
145: public void uninitialize(WizardDescriptor wiz) {
146: this .wiz.putProperty("projdir", null); // NOI18N
147: this .wiz.putProperty("name", null); // NOI18N
148: this .wiz = null;
149: panels = null;
150: }
151:
152: public String name() {
153: return NbBundle.getMessage(PaintAppWizardIterator.class,
154: "PaintAppWizardIterator.name.format", new Object[] {
155: new Integer(index + 1),
156: new Integer(panels.length) });
157: }
158:
159: public boolean hasNext() {
160: return index < panels.length - 1;
161: }
162:
163: public boolean hasPrevious() {
164: return index > 0;
165: }
166:
167: public void nextPanel() {
168: if (!hasNext()) {
169: throw new NoSuchElementException();
170: }
171: index++;
172: }
173:
174: public void previousPanel() {
175: if (!hasPrevious()) {
176: throw new NoSuchElementException();
177: }
178: index--;
179: }
180:
181: public WizardDescriptor.Panel current() {
182: return panels[index];
183: }
184:
185: // If nothing unusual changes in the middle of the wizard, simply:
186: public final void addChangeListener(ChangeListener l) {
187: }
188:
189: public final void removeChangeListener(ChangeListener l) {
190: }
191:
192: private static void unZipFile(InputStream source,
193: FileObject projectRoot) throws IOException {
194: try {
195: ZipInputStream str = new ZipInputStream(source);
196: ZipEntry entry;
197: while ((entry = str.getNextEntry()) != null) {
198: if (entry.isDirectory()) {
199: FileUtil.createFolder(projectRoot, entry.getName());
200: } else {
201: FileObject fo = FileUtil.createData(projectRoot,
202: entry.getName());
203: FileLock lock = fo.lock();
204: try {
205: OutputStream out = fo.getOutputStream(lock);
206: try {
207: FileUtil.copy(str, out);
208: } finally {
209: out.close();
210: }
211: } finally {
212: lock.releaseLock();
213: }
214: }
215: }
216: } finally {
217: source.close();
218: }
219: }
220:
221: }
|