KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > samples > FreeformWizardIterator


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