KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > feedreader > FeedReaderWizardIterator


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.modules.apisupport.feedreader;
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.text.MessageFormat JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.LinkedHashSet JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import org.netbeans.api.project.ProjectManager;
33 import org.netbeans.spi.project.ui.support.ProjectChooser;
34 import org.netbeans.spi.project.ui.templates.support.Templates;
35 import org.openide.WizardDescriptor;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.util.NbBundle;
39
40 public class FeedReaderWizardIterator implements WizardDescriptor.InstantiatingIterator {
41     
42     private static final long serialVersionUID = 1L;
43     
44     private transient int index;
45     private transient WizardDescriptor.Panel[] panels;
46     private transient WizardDescriptor wiz;
47     
48     public static FeedReaderWizardIterator createIterator() {
49         return new FeedReaderWizardIterator();
50     }
51     
52     private WizardDescriptor.Panel[] createPanels() {
53         return new WizardDescriptor.Panel[] {
54             new FeedReaderWizardPanel(),
55         };
56     }
57     
58     private String JavaDoc[] createSteps() {
59         return new String JavaDoc[] {
60             NbBundle.getMessage(FeedReaderWizardPanel.class, "LBL_Step1"),
61         };
62     }
63     
64     public Set JavaDoc/*<FileObject>*/ instantiate() throws IOException JavaDoc {
65         Set JavaDoc resultSet = new LinkedHashSet JavaDoc();
66         File JavaDoc dirF = FileUtil.normalizeFile((File JavaDoc) wiz.getProperty("projdir"));
67         dirF.mkdirs();
68         
69         FileObject template = Templates.getTemplate(wiz);
70         FileObject dir = FileUtil.toFileObject(dirF);
71         InputStream JavaDoc is = template.getInputStream();
72         try {
73             FileUtil.extractJar(dir, is);
74         } finally {
75             is.close();
76         }
77         //unZipFile(template.getInputStream(), dir);
78

79         // Always open top dir as a project:
80
resultSet.add(dir);
81         // Look for nested projects to open as well:
82
Enumeration JavaDoc e = dir.getFolders(true);
83         while (e.hasMoreElements()) {
84             FileObject subfolder = (FileObject) e.nextElement();
85             if (ProjectManager.getDefault().isProject(subfolder)) {
86                 resultSet.add(subfolder);
87             }
88         }
89         
90         File JavaDoc parent = dirF.getParentFile();
91         if (parent != null && parent.exists()) {
92             ProjectChooser.setProjectsFolder(parent);
93         }
94         
95         return resultSet;
96     }
97     
98     public void initialize(WizardDescriptor wiz) {
99         this.wiz = wiz;
100         index = 0;
101         panels = createPanels();
102         // Make sure list of steps is accurate.
103
String JavaDoc[] steps = createSteps();
104         for (int i = 0; i < panels.length; i++) {
105             Component JavaDoc c = panels[i].getComponent();
106             if (steps[i] == null) {
107                 // Default step name to component name of panel.
108
// Mainly useful for getting the name of the target
109
// chooser to appear in the list of steps.
110
steps[i] = c.getName();
111             }
112             if (c instanceof JComponent JavaDoc) { // assume Swing components
113
JComponent JavaDoc jc = (JComponent JavaDoc)c;
114                 // Step #.
115
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
116
// Step name (actually the whole list for reference).
117
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
118
}
119         }
120     }
121     
122     public void uninitialize(WizardDescriptor wiz) {
123         this.wiz.putProperty("projdir", null); // NOI18N
124
this.wiz.putProperty("name", null); // NOI18N
125
this.wiz = null;
126         panels = null;
127     }
128     
129     public String JavaDoc name() {
130         return MessageFormat.format("{0} of {1}",
131                 new Object JavaDoc[] {new Integer JavaDoc(index + 1), new Integer JavaDoc(panels.length) });
132     }
133     
134     public boolean hasNext() {
135         return index < panels.length - 1;
136     }
137     
138     public boolean hasPrevious() {
139         return index > 0;
140     }
141     
142     public void nextPanel() {
143         if (!hasNext()) {
144             throw new NoSuchElementException JavaDoc();
145         }
146         index++;
147     }
148     
149     public void previousPanel() {
150         if (!hasPrevious()) {
151             throw new NoSuchElementException JavaDoc();
152         }
153         index--;
154     }
155     
156     public WizardDescriptor.Panel current() {
157         return panels[index];
158     }
159     
160     // If nothing unusual changes in the middle of the wizard, simply:
161
public final void addChangeListener(ChangeListener JavaDoc l) {}
162     public final void removeChangeListener(ChangeListener JavaDoc l) {}
163     
164 }
165
Popular Tags