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