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.feedreader;
042:
043: import java.awt.Component;
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.text.MessageFormat;
048: import java.util.Enumeration;
049: import java.util.LinkedHashSet;
050: import java.util.NoSuchElementException;
051: import java.util.Set;
052: import javax.swing.JComponent;
053: import javax.swing.event.ChangeListener;
054: import org.netbeans.api.project.ProjectManager;
055: import org.netbeans.spi.project.ui.support.ProjectChooser;
056: import org.netbeans.spi.project.ui.templates.support.Templates;
057: import org.openide.WizardDescriptor;
058: import org.openide.filesystems.FileObject;
059: import org.openide.filesystems.FileUtil;
060: import org.openide.util.NbBundle;
061:
062: public class FeedReaderWizardIterator implements
063: WizardDescriptor.InstantiatingIterator {
064:
065: private static final long serialVersionUID = 1L;
066:
067: private transient int index;
068: private transient WizardDescriptor.Panel[] panels;
069: private transient WizardDescriptor wiz;
070:
071: public static FeedReaderWizardIterator createIterator() {
072: return new FeedReaderWizardIterator();
073: }
074:
075: private WizardDescriptor.Panel[] createPanels() {
076: return new WizardDescriptor.Panel[] { new FeedReaderWizardPanel(), };
077: }
078:
079: private String[] createSteps() {
080: return new String[] { NbBundle.getMessage(
081: FeedReaderWizardPanel.class, "LBL_Step1"), };
082: }
083:
084: public Set/*<FileObject>*/instantiate() throws IOException {
085: Set resultSet = new LinkedHashSet();
086: File dirF = FileUtil.normalizeFile((File) wiz
087: .getProperty("projdir"));
088: dirF.mkdirs();
089:
090: FileObject template = Templates.getTemplate(wiz);
091: FileObject dir = FileUtil.toFileObject(dirF);
092: InputStream is = template.getInputStream();
093: try {
094: FileUtil.extractJar(dir, is);
095: } finally {
096: is.close();
097: }
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 MessageFormat.format("{0} of {1}", new Object[] {
154: new Integer(index + 1), new Integer(panels.length) });
155: }
156:
157: public boolean hasNext() {
158: return index < panels.length - 1;
159: }
160:
161: public boolean hasPrevious() {
162: return index > 0;
163: }
164:
165: public void nextPanel() {
166: if (!hasNext()) {
167: throw new NoSuchElementException();
168: }
169: index++;
170: }
171:
172: public void previousPanel() {
173: if (!hasPrevious()) {
174: throw new NoSuchElementException();
175: }
176: index--;
177: }
178:
179: public WizardDescriptor.Panel current() {
180: return panels[index];
181: }
182:
183: // If nothing unusual changes in the middle of the wizard, simply:
184: public final void addChangeListener(ChangeListener l) {
185: }
186:
187: public final void removeChangeListener(ChangeListener l) {
188: }
189:
190: }
|