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:
042: package org.netbeans.modules.websvc.rest.samples.ui;
043:
044: import java.awt.Component;
045: import java.io.BufferedReader;
046: import java.io.File;
047: import java.io.FileReader;
048: import java.io.IOException;
049: import java.io.OutputStreamWriter;
050: import java.text.MessageFormat;
051: import java.util.Enumeration;
052: import java.util.LinkedHashSet;
053: import java.util.NoSuchElementException;
054: import java.util.Set;
055: import javax.swing.JComponent;
056: import javax.swing.event.ChangeListener;
057: import org.netbeans.api.project.Project;
058: import org.netbeans.api.project.ProjectManager;
059: import org.netbeans.modules.websvc.rest.samples.ui.SampleWizardPanel;
060: import org.netbeans.modules.websvc.rest.samples.util.Log;
061: import org.netbeans.modules.websvc.rest.samples.util.RestSampleProjectProperties;
062: import org.netbeans.modules.websvc.rest.samples.util.RestSampleUtils;
063: import org.netbeans.spi.project.ui.support.ProjectChooser;
064: import org.netbeans.spi.project.ui.templates.support.Templates;
065: import org.openide.ErrorManager;
066: import org.openide.WizardDescriptor;
067: import org.openide.filesystems.FileLock;
068: import org.openide.filesystems.FileObject;
069: import org.openide.filesystems.FileUtil;
070: import org.openide.util.NbBundle;
071:
072: /**
073: * @author Peter Liu
074: */
075: public class SampleWizardIterator implements
076: WizardDescriptor.InstantiatingIterator {
077:
078: private static final long serialVersionUID = 1L;
079:
080: public static final String PROJDIR = "projdir"; // NOI18N
081: public static final String NAME = "name"; // NOI18N
082: private Project myProject;
083: private transient int index;
084: private transient WizardDescriptor.Panel[] panels;
085: public transient WizardDescriptor wiz;
086: private boolean addJerseyLibrary = true;
087: private String projectConfigNamespace = RestSampleProjectProperties.WEB_PROJECT_CONFIGURATION_NAMESPACE;
088:
089: public SampleWizardIterator() {
090: }
091:
092: public static SampleWizardIterator createIterator() {
093: return new SampleWizardIterator();
094: }
095:
096: protected WizardDescriptor.Panel[] createPanels() {
097: return new WizardDescriptor.Panel[] { new SampleWizardPanel(), };
098: }
099:
100: protected String[] createSteps() {
101: return new String[] { NbBundle.getMessage(
102: SampleWizardIterator.class, "MSG_SampleProject"), };
103: }
104:
105: public Project getProject() {
106: return myProject;
107: }
108:
109: public void setProjectConfigNamespace(String namespace) {
110: this .projectConfigNamespace = namespace;
111: }
112:
113: public void setAddJerseyLibrary(boolean flag) {
114: addJerseyLibrary = flag;
115: }
116:
117: public Set/*<FileObject>*/instantiate() throws IOException {
118: Set resultSet = new LinkedHashSet();
119: File dirF = FileUtil.normalizeFile((File) wiz
120: .getProperty(PROJDIR));
121: dirF.mkdirs();
122:
123: String name = (String) wiz.getProperty(NAME);
124: FileObject template = Templates.getTemplate(wiz);
125:
126: FileObject dir = FileUtil.toFileObject(dirF);
127:
128: // All projects associated with the sample are created within a
129: // top level directory.
130: dir = dir.createFolder(name);
131: dirF = FileUtil.toFile(dir);
132:
133: RestSampleUtils.unZipFile(template.getInputStream(), dir);
134:
135: if (projectConfigNamespace != null)
136: RestSampleUtils.setProjectName(dir, projectConfigNamespace,
137: name);
138:
139: Project p = ProjectManager.getDefault().findProject(dir);
140: myProject = p;
141:
142: if (addJerseyLibrary)
143: RestSampleUtils.addJerseyLibrary(myProject);
144:
145: // Always open top dir as a project:
146: resultSet.add(dir);
147: // Look for nested projects to open as well:
148: Enumeration e = dir.getFolders(true);
149: while (e.hasMoreElements()) {
150: FileObject subfolder = (FileObject) e.nextElement();
151: if (ProjectManager.getDefault().isProject(subfolder)) {
152: resultSet.add(subfolder);
153: }
154: }
155:
156: File parent = dirF.getParentFile();
157: if (parent != null && parent.exists()) {
158: ProjectChooser.setProjectsFolder(parent);
159: }
160:
161: return resultSet;
162: }
163:
164: public void initialize(WizardDescriptor wiz) {
165: this .wiz = wiz;
166: index = 0;
167: panels = createPanels();
168: // Make sure list of steps is accurate.
169: String[] steps = createSteps();
170: for (int i = 0; i < panels.length; i++) {
171: Component c = panels[i].getComponent();
172: if (steps[i] == null) {
173: // Default step name to component name of panel.
174: // Mainly useful for getting the name of the target
175: // chooser to appear in the list of steps.
176: steps[i] = c.getName();
177: }
178: if (c instanceof JComponent) { // assume Swing components
179: JComponent jc = (JComponent) c;
180: // Step #.
181: jc.putClientProperty(
182: "WizardPanel_contentSelectedIndex",
183: new Integer(i)); // NOI18N
184: // Step name (actually the whole list for reference).
185: jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
186: }
187: }
188: }
189:
190: public void uninitialize(WizardDescriptor wiz) {
191: this .wiz.putProperty(PROJDIR, null);
192: this .wiz.putProperty(NAME, null);
193: this .wiz = null;
194: panels = null;
195: }
196:
197: public String name() {
198: return MessageFormat.format("{0} of {1}", //NOI18N
199: new Object[] { new Integer(index + 1),
200: new Integer(panels.length) });
201: }
202:
203: public boolean hasNext() {
204: return index < panels.length - 1;
205: }
206:
207: public boolean hasPrevious() {
208: return index > 0;
209: }
210:
211: public void nextPanel() {
212: if (!hasNext()) {
213: throw new NoSuchElementException();
214: }
215: index++;
216: }
217:
218: public void previousPanel() {
219: if (!hasPrevious()) {
220: throw new NoSuchElementException();
221: }
222: index--;
223: }
224:
225: public WizardDescriptor.Panel current() {
226: return panels[index];
227: }
228:
229: // If nothing unusual changes in the middle of the wizard, simply:
230: public void addChangeListener(ChangeListener l) {
231: }
232:
233: public void removeChangeListener(ChangeListener l) {
234: }
235:
236: protected void replaceTokens(FileObject dir, String[] files,
237: String[][] tokens) throws IOException {
238: for (String file : files) {
239: replaceToken(dir.getFileObject(file), tokens); //NoI18n
240: }
241: }
242:
243: protected void replaceToken(FileObject fo, String[][] tokens)
244: throws IOException {
245: if (fo == null)
246: return;
247: FileLock lock = fo.lock();
248: try {
249: BufferedReader reader = new BufferedReader(new FileReader(
250: FileUtil.toFile(fo)));
251: String line;
252: StringBuffer sb = new StringBuffer();
253: while ((line = reader.readLine()) != null) {
254: for (int i = 0; i < tokens.length; i++)
255: line = line.replaceAll(tokens[i][0], tokens[i][1]);
256: sb.append(line);
257: sb.append("\n");
258: }
259: OutputStreamWriter writer = new OutputStreamWriter(fo
260: .getOutputStream(lock), "UTF-8");
261: try {
262: writer.write(sb.toString());
263: } finally {
264: writer.close();
265: }
266: } finally {
267: lock.releaseLock();
268: }
269: }
270: }
|