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.project.ui;
043:
044: import java.awt.Component;
045: import java.io.IOException;
046: import java.util.Collections;
047: import java.util.Set;
048: import javax.swing.JComponent;
049: import javax.swing.event.ChangeListener;
050: import org.netbeans.api.project.Project;
051: import org.netbeans.api.project.ProjectUtils;
052: import org.netbeans.api.project.Sources;
053: import org.netbeans.spi.project.ui.templates.support.Templates;
054: import org.openide.WizardDescriptor;
055: import org.openide.filesystems.FileObject;
056: import org.openide.loaders.DataFolder;
057: import org.openide.loaders.DataObject;
058:
059: /** Iterator useful for NewFileWizard. Implements WD.InstantiatingIterator,
060: * rest of methods delegates to WD.ArrayIterator created only with SimpleTargetChooserPanel.
061: *
062: * @author Jiri Rechtacek
063: */
064: public class NewFileIterator implements
065: WizardDescriptor.InstantiatingIterator<WizardDescriptor> {
066:
067: private static final long serialVersionUID = 1L;
068:
069: private transient WizardDescriptor.Iterator<WizardDescriptor> simpleIterator;
070: private transient WizardDescriptor.Panel<WizardDescriptor> panel;
071: private transient WizardDescriptor wiz;
072: private transient Project currentProject;
073:
074: private transient boolean isFolder;
075:
076: /** Create a new wizard iterator. */
077: private NewFileIterator(boolean isFolderIterator) {
078: isFolder = isFolderIterator;
079: }
080:
081: public static NewFileIterator genericFileIterator() {
082: return new NewFileIterator(false);
083: }
084:
085: public static NewFileIterator folderIterator() {
086: return new NewFileIterator(true);
087: }
088:
089: public static NewFileIterator emptyFileIterator() {
090: return new NewFileIterator(false);
091: }
092:
093: private WizardDescriptor.Iterator<WizardDescriptor> getSimpleIterator() {
094: if (simpleIterator == null) {
095: assert panel != null;
096: simpleIterator = new WizardDescriptor.ArrayIterator<WizardDescriptor>(
097: Collections.singletonList(panel));
098: }
099: return simpleIterator;
100: }
101:
102: private WizardDescriptor.Panel<WizardDescriptor> getPanel(
103: WizardDescriptor wizardDescriptor) {
104: Project project = Templates.getProject(wizardDescriptor);
105: assert project != null : wizardDescriptor;
106: if (!project.equals(currentProject) || panel == null) {
107: currentProject = project;
108: Sources sources = ProjectUtils.getSources(project);
109: if (isFolder) {
110: panel = new SimpleTargetChooserPanel(project, sources
111: .getSourceGroups(Sources.TYPE_GENERIC), null,
112: true);
113: } else {
114: panel = Templates.createSimpleTargetChooser(project,
115: sources.getSourceGroups(Sources.TYPE_GENERIC));
116: }
117: }
118: return panel;
119: }
120:
121: private String[] createSteps(String[] before) {
122: assert panel != null;
123:
124: if (before == null) {
125: before = new String[0];
126: }
127:
128: String[] res = new String[before.length];
129: for (int i = 0; i < res.length; i++) {
130: if (i < (before.length - 1)) {
131: res[i] = before[i];
132: } else {
133: res[i] = panel.getComponent().getName();
134: }
135: }
136: return res;
137: }
138:
139: public Set/*<FileObject>*/instantiate() throws IOException {
140: FileObject dir = Templates.getTargetFolder(wiz);
141:
142: DataFolder df = DataFolder.findFolder(dir);
143: FileObject template = Templates.getTemplate(wiz);
144:
145: DataObject dTemplate = DataObject.find(template);
146: DataObject dobj = dTemplate.createFromTemplate(df, Templates
147: .getTargetName(wiz));
148:
149: return Collections.singleton(dobj.getPrimaryFile());
150: }
151:
152: public void initialize(WizardDescriptor wiz) {
153: panel = getPanel(wiz);
154: this .wiz = wiz;
155:
156: // Make sure list of steps is accurate.
157: String[] beforeSteps = null;
158: Object prop = wiz.getProperty("WizardPanel_contentData"); // NOI18N
159: if (prop != null && prop instanceof String[]) {
160: beforeSteps = (String[]) prop;
161: }
162: String[] steps = createSteps(beforeSteps);
163: for (int i = 0; i < 1; i++) { // XXX what was this loop for, exactly? panels.length was always 1
164: Component c = panel.getComponent();
165: if (steps[i] == null) {
166: // Default step name to component name of panel.
167: // Mainly useful for getting the name of the target
168: // chooser to appear in the list of steps.
169: steps[i] = c.getName();
170: }
171: if (c instanceof JComponent) { // assume Swing components
172: JComponent jc = (JComponent) c;
173: // Step #.
174: jc.putClientProperty(
175: "WizardPanel_contentSelectedIndex",
176: new Integer(i)); // NOI18N
177: // Step name (actually the whole list for reference).
178: jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
179: }
180: }
181: }
182:
183: public void uninitialize(WizardDescriptor wiz) {
184: this .simpleIterator = null;
185: this .wiz = null;
186: panel = null;
187: }
188:
189: public String name() {
190: return getSimpleIterator().name();
191: }
192:
193: public boolean hasNext() {
194: return getSimpleIterator().hasNext();
195: }
196:
197: public boolean hasPrevious() {
198: return getSimpleIterator().hasPrevious();
199: }
200:
201: public void nextPanel() {
202: getSimpleIterator().nextPanel();
203: }
204:
205: public void previousPanel() {
206: getSimpleIterator().previousPanel();
207: }
208:
209: public WizardDescriptor.Panel<WizardDescriptor> current() {
210: return getSimpleIterator().current();
211: }
212:
213: public final void addChangeListener(ChangeListener l) {
214: getSimpleIterator().addChangeListener(l);
215: }
216:
217: public final void removeChangeListener(ChangeListener l) {
218: getSimpleIterator().removeChangeListener(l);
219: }
220: }
|