001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.compapp.projects.wizard;
021:
022: import java.awt.Component;
023: import java.io.IOException;
024: import java.util.Collections;
025: import java.util.Set;
026: import javax.swing.JComponent;
027: import javax.swing.event.ChangeListener;
028: import org.netbeans.api.project.Project;
029: import org.netbeans.api.project.ProjectUtils;
030: import org.netbeans.api.project.Sources;
031: import org.netbeans.api.project.SourceGroup;
032: import org.netbeans.spi.project.ui.templates.support.Templates;
033: import org.openide.WizardDescriptor;
034: import org.openide.filesystems.FileObject;
035: import org.openide.loaders.DataFolder;
036: import org.openide.loaders.DataObject;
037:
038: /**
039: * Iterator useful for NewFileWizard. Implements WD.InstantiatingIterator,
040: * rest of methods delegates to WD.ArrayIterator created only with SimpleTargetChooserPanel.
041: *
042: * @author Tientien Li
043: */
044: public class NewFileIterator implements
045: WizardDescriptor.InstantiatingIterator {
046:
047: private static final long serialVersionUID = 1L;
048:
049: public static final String SOURCE_TYPE_BIZPRO = "BIZPRO";
050:
051: private transient boolean isLibrary;
052: private transient WizardDescriptor.Iterator simpleIterator;
053: private transient WizardDescriptor.Panel[] panels;
054: private transient WizardDescriptor wiz;
055: private transient Project currentProject;
056:
057: private transient boolean isFolder;
058:
059: /** Create a new generic iterator. */
060: private NewFileIterator(boolean isFolderIterator) {
061: isFolder = isFolderIterator;
062: }
063:
064: public static NewFileIterator genericFileIterator() {
065: return new NewFileIterator(false);
066: }
067:
068: public static NewFileIterator folderIterator() {
069: return new NewFileIterator(true);
070: }
071:
072: public static NewFileIterator emptyFileIterator() {
073: return new NewFileIterator(false);
074: }
075:
076: private WizardDescriptor.Iterator getSimpleIterator() {
077: if (simpleIterator == null) {
078: assert panels != null && panels.length > 0;
079: simpleIterator = new WizardDescriptor.ArrayIterator(panels);
080: }
081: return simpleIterator;
082: }
083:
084: private WizardDescriptor.Panel[] getPanels(
085: WizardDescriptor wizardDescriptor) {
086: Project project = Templates.getProject(wizardDescriptor);
087: assert project != null : wizardDescriptor;
088: if (!project.equals(currentProject) || panels == null) {
089: currentProject = project;
090: Sources sources = ProjectUtils.getSources(project);
091: SourceGroup[] groups = sources
092: .getSourceGroups(NewFileIterator.SOURCE_TYPE_BIZPRO /* Sources.TYPE_GENERIC */);
093: if ((groups == null) || (groups.length < 1)) {
094: groups = sources.getSourceGroups(Sources.TYPE_GENERIC);
095: }
096:
097: if (isFolder) {
098: panels = new WizardDescriptor.Panel[] { new SimpleTargetChooserPanel(
099: project, groups, null, true) };
100: } else {
101: panels = new WizardDescriptor.Panel[] { new SimpleTargetChooserPanel(
102: project, groups, null, false)
103: // Templates.createSimpleTargetChooser (project, sources.getSourceGroups (Sources.TYPE_GENERIC))
104: };
105: }
106: }
107: return panels;
108: }
109:
110: private String[] createSteps(String[] before) {
111: assert panels != null;
112:
113: if (before == null) {
114: before = new String[0];
115: }
116:
117: String[] res = new String[(before.length - 1) + panels.length];
118: for (int i = 0; i < res.length; i++) {
119: if (i < (before.length - 1)) {
120: res[i] = before[i];
121: } else {
122: res[i] = panels[i - before.length + 1].getComponent()
123: .getName();
124: }
125: }
126: return res;
127: }
128:
129: public Set/*<FileObject>*/instantiate() throws IOException {
130: FileObject dir = Templates.getTargetFolder(wiz);
131:
132: DataFolder df = DataFolder.findFolder(dir);
133: FileObject template = Templates.getTemplate(wiz);
134:
135: DataObject dTemplate = DataObject.find(template);
136: DataObject dobj = dTemplate.createFromTemplate(df, Templates
137: .getTargetName(wiz));
138:
139: return Collections.singleton(dobj.getPrimaryFile());
140: }
141:
142: public void initialize(WizardDescriptor wiz) {
143: panels = getPanels(wiz);
144: this .wiz = wiz;
145:
146: // Make sure list of steps is accurate.
147: String[] beforeSteps = null;
148: Object prop = wiz.getProperty("WizardPanel_contentData"); // NOI18N
149: if (prop != null && prop instanceof String[]) {
150: beforeSteps = (String[]) prop;
151: }
152: String[] steps = createSteps(beforeSteps);
153: for (int i = 0; i < panels.length; i++) {
154: Component c = panels[i].getComponent();
155: if (steps[i] == null) {
156: // Default step name to component name of panel.
157: // Mainly useful for getting the name of the target
158: // chooser to appear in the list of steps.
159: steps[i] = c.getName();
160: }
161: if (c instanceof JComponent) { // assume Swing components
162: JComponent jc = (JComponent) c;
163: // Step #.
164: jc.putClientProperty(
165: "WizardPanel_contentSelectedIndex",
166: new Integer(i)); // NOI18N
167: // Step name (actually the whole list for reference).
168: jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
169: }
170: }
171: }
172:
173: public void uninitialize(WizardDescriptor wiz) {
174: this .simpleIterator = null;
175: this .wiz = null;
176: panels = null;
177: }
178:
179: public String name() {
180: return getSimpleIterator().name();
181: }
182:
183: public boolean hasNext() {
184: return getSimpleIterator().hasNext();
185: }
186:
187: public boolean hasPrevious() {
188: return getSimpleIterator().hasPrevious();
189: }
190:
191: public void nextPanel() {
192: getSimpleIterator().nextPanel();
193: }
194:
195: public void previousPanel() {
196: getSimpleIterator().previousPanel();
197: }
198:
199: public WizardDescriptor.Panel current() {
200: return getSimpleIterator().current();
201: }
202:
203: public final void addChangeListener(ChangeListener l) {
204: getSimpleIterator().addChangeListener(l);
205: }
206:
207: public final void removeChangeListener(ChangeListener l) {
208: getSimpleIterator().removeChangeListener(l);
209: }
210: }
|