raster@14352
|
1 |
/*
|
raster@14352
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
raster@14352
|
3 |
*
|
raster@14352
|
4 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
|
raster@14352
|
5 |
*
|
raster@14352
|
6 |
* The contents of this file are subject to the terms of either the GNU
|
raster@14352
|
7 |
* General Public License Version 2 only ("GPL") or the Common
|
raster@14352
|
8 |
* Development and Distribution License("CDDL") (collectively, the
|
raster@14352
|
9 |
* "License"). You may not use this file except in compliance with the
|
raster@14352
|
10 |
* License. You can obtain a copy of the License at
|
raster@14352
|
11 |
* http://www.netbeans.org/cddl-gplv2.html
|
raster@14352
|
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
raster@14352
|
13 |
* specific language governing permissions and limitations under the
|
raster@14352
|
14 |
* License. When distributing the software, include this License Header
|
raster@14352
|
15 |
* Notice in each file and include the License file at
|
raster@14352
|
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
|
raster@14352
|
17 |
* particular file as subject to the "Classpath" exception as provided
|
raster@14352
|
18 |
* by Sun in the GPL Version 2 section of the License file that
|
raster@14352
|
19 |
* accompanied this code. If applicable, add the following below the
|
raster@14352
|
20 |
* License Header, with the fields enclosed by brackets [] replaced by
|
raster@14352
|
21 |
* your own identifying information:
|
raster@14352
|
22 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
raster@14352
|
23 |
*
|
raster@14352
|
24 |
* If you wish your version of this file to be governed by only the CDDL
|
raster@14352
|
25 |
* or only the GPL Version 2, indicate your decision by adding
|
raster@14352
|
26 |
* "[Contributor] elects to include this software in this distribution
|
raster@14352
|
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
raster@14352
|
28 |
* single choice of license, a recipient has the option to distribute
|
raster@14352
|
29 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
raster@14352
|
30 |
* to extend the choice of license to its licensees as provided above.
|
raster@14352
|
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
raster@14352
|
32 |
* Version 2 license, then the option applies only if the new code is
|
raster@14352
|
33 |
* made subject to such option by the copyright holder.
|
raster@14352
|
34 |
*
|
raster@14352
|
35 |
* Contributor(s):
|
raster@14352
|
36 |
*
|
raster@14352
|
37 |
* Portions Copyrighted 2008 Sun Microsystems, Inc.
|
raster@14352
|
38 |
*/
|
raster@14352
|
39 |
package org.netbeans.modules.ada.project.ui.wizards;
|
raster@14352
|
40 |
|
raster@14352
|
41 |
import java.awt.Component;
|
raster@14352
|
42 |
import java.io.IOException;
|
raster@14352
|
43 |
import java.util.Collections;
|
raster@14352
|
44 |
import java.util.HashMap;
|
raster@14352
|
45 |
import java.util.Map;
|
raster@14352
|
46 |
import java.util.NoSuchElementException;
|
raster@14352
|
47 |
import java.util.Set;
|
raster@14352
|
48 |
import javax.swing.JComponent;
|
raster@14352
|
49 |
import javax.swing.event.ChangeListener;
|
raster@14352
|
50 |
import org.netbeans.api.project.Project;
|
raster@14352
|
51 |
import org.netbeans.api.project.SourceGroup;
|
raster@14352
|
52 |
import org.netbeans.modules.ada.project.AdaMimeResolver;
|
raster@14352
|
53 |
import org.netbeans.modules.ada.project.AdaProject;
|
raster@15779
|
54 |
import org.netbeans.modules.ada.project.options.AdaOptions;
|
raster@14352
|
55 |
import org.netbeans.modules.ada.project.ui.Utils;
|
raster@14352
|
56 |
import org.netbeans.modules.ada.project.ui.properties.AdaProjectProperties;
|
raster@14352
|
57 |
import org.netbeans.spi.project.support.ant.AntProjectHelper;
|
raster@14352
|
58 |
import org.netbeans.spi.project.ui.templates.support.Templates;
|
raster@14352
|
59 |
import org.openide.WizardDescriptor;
|
raster@14352
|
60 |
import org.openide.filesystems.FileObject;
|
raster@14352
|
61 |
import org.openide.filesystems.FileUtil;
|
raster@14352
|
62 |
import org.openide.loaders.DataFolder;
|
raster@14352
|
63 |
import org.openide.loaders.DataObject;
|
raster@14352
|
64 |
|
raster@14352
|
65 |
/**
|
raster@14352
|
66 |
*
|
raster@14352
|
67 |
* @author Andrea Lucarelli
|
raster@14352
|
68 |
*/
|
raster@14352
|
69 |
public final class NewFileWizardIterator implements WizardDescriptor.InstantiatingIterator {
|
raster@14352
|
70 |
|
raster@14352
|
71 |
private int index;
|
raster@14352
|
72 |
private WizardDescriptor wizard;
|
raster@14352
|
73 |
private WizardDescriptor.Panel<WizardDescriptor>[] wizardPanels;
|
raster@14352
|
74 |
|
raster@14352
|
75 |
/**
|
raster@14352
|
76 |
* Initialize panels representing individual wizard's steps and sets
|
raster@14352
|
77 |
* various properties for them influencing wizard appearance.
|
raster@14352
|
78 |
*/
|
raster@14352
|
79 |
private WizardDescriptor.Panel<WizardDescriptor>[] getPanels() {
|
raster@14352
|
80 |
Project p = Templates.getProject(wizard);
|
raster@14352
|
81 |
final SourceGroup[] groups = Utils.getSourceGroups(p);
|
raster@14352
|
82 |
WizardDescriptor.Panel<WizardDescriptor> simpleTargetChooserPanel = Templates.createSimpleTargetChooser(p, groups);
|
raster@14352
|
83 |
FileObject template = Templates.getTemplate(wizard);
|
raster@14352
|
84 |
|
raster@14352
|
85 |
return new WizardDescriptor.Panel[]{
|
raster@14352
|
86 |
simpleTargetChooserPanel
|
raster@14352
|
87 |
};
|
raster@14352
|
88 |
}
|
raster@14352
|
89 |
|
raster@14352
|
90 |
public Set instantiate() throws IOException {
|
raster@14352
|
91 |
FileObject dir = Templates.getTargetFolder(wizard);
|
raster@14352
|
92 |
FileObject template = Templates.getTemplate(wizard);
|
raster@14352
|
93 |
|
raster@14352
|
94 |
Map<String, Object> wizardProps = new HashMap<String, Object>();
|
raster@14352
|
95 |
|
raster@14352
|
96 |
DataFolder dataFolder = DataFolder.findFolder(dir);
|
raster@14352
|
97 |
DataObject dataTemplate = DataObject.find(template);
|
raster@14352
|
98 |
String fname = Templates.getTargetName(wizard);
|
raster@14352
|
99 |
String ext = FileUtil.getExtension(fname);
|
raster@14352
|
100 |
|
raster@14352
|
101 |
FileObject foo = FileUtil.createData(FileUtil.createMemoryFileSystem().getRoot(), fname);
|
raster@14525
|
102 |
if (foo == null || !AdaMimeResolver.ADA_MIME_TYPE.equals(FileUtil.getMIMEType(foo))) {
|
raster@14352
|
103 |
if (ext == null || ext.length() == 0) {
|
raster@14352
|
104 |
// TODO: get naming by options
|
raster@14352
|
105 |
Templates.setTargetName(this.wizard, fname); //NOI18N
|
raster@14352
|
106 |
fname = Templates.getTargetName(wizard);
|
raster@14352
|
107 |
ext = FileUtil.getExtension(fname);
|
raster@14352
|
108 |
}
|
raster@14352
|
109 |
}
|
raster@14352
|
110 |
|
raster@14352
|
111 |
// Remove the extension on File Name text field
|
raster@14352
|
112 |
if (ext != null && ext.length() > 0) {
|
raster@14352
|
113 |
wizardProps.put("name", fname.substring(0, fname.length() - ext.length() - 1));//NOI18N
|
raster@14352
|
114 |
}
|
raster@14352
|
115 |
|
raster@14352
|
116 |
DataObject createdFile = dataTemplate.createFromTemplate(dataFolder, Templates.getTargetName(wizard), wizardProps);
|
raster@14352
|
117 |
|
raster@14352
|
118 |
return Collections.<FileObject>singleton(createdFile.getPrimaryFile());
|
raster@14352
|
119 |
}
|
raster@14352
|
120 |
|
raster@14352
|
121 |
public void initialize(WizardDescriptor wizard) {
|
raster@14352
|
122 |
this.wizard = wizard;
|
raster@14352
|
123 |
FileObject targetFolder = Templates.getTargetFolder(wizard);
|
raster@14352
|
124 |
if (targetFolder == null) {
|
raster@14352
|
125 |
Project project = Templates.getProject(wizard);
|
raster@14352
|
126 |
assert project instanceof AdaProject;
|
raster@14352
|
127 |
AdaProject adaProject = (AdaProject) project;
|
raster@14352
|
128 |
// XXX - sources directory is remembered in project
|
raster@14352
|
129 |
FileObject srcDir = getFileObject(adaProject.getHelper(), AdaProjectProperties.SRC_DIR);
|
raster@14352
|
130 |
if (srcDir != null) {
|
raster@14352
|
131 |
targetFolder = srcDir;
|
raster@14352
|
132 |
Templates.setTargetFolder(wizard, srcDir);
|
raster@14352
|
133 |
}
|
raster@14352
|
134 |
}
|
raster@14352
|
135 |
FileObject template = Templates.getTemplate(this.wizard);
|
raster@15779
|
136 |
String ext = template.getExt();
|
raster@15779
|
137 |
String postfix = "";
|
raster@16367
|
138 |
|
raster@16367
|
139 |
Project project = Templates.getProject(wizard);
|
raster@16367
|
140 |
assert project instanceof AdaProject;
|
raster@16367
|
141 |
AdaProject adaProject = (AdaProject) project;
|
raster@16367
|
142 |
|
raster@16367
|
143 |
if (template.getName().startsWith("NewMain")) {
|
raster@16367
|
144 |
ext = adaProject.getEvaluator().getProperty(AdaOptions.SEPARATE_EXT);
|
raster@16367
|
145 |
postfix = adaProject.getEvaluator().getProperty(AdaOptions.SEPARATE_POSTFIX);
|
raster@16367
|
146 |
} else if (template.getName().startsWith("NewPackageSpec")) {
|
raster@16367
|
147 |
ext = adaProject.getEvaluator().getProperty(AdaOptions.PKG_SPEC_EXT);
|
raster@16367
|
148 |
postfix = adaProject.getEvaluator().getProperty(AdaOptions.PKG_SPEC_POSTFIX);
|
raster@16367
|
149 |
} else if (template.getName().startsWith("NewPackageBody")) {
|
raster@16367
|
150 |
ext = adaProject.getEvaluator().getProperty(AdaOptions.PKG_BODY_EXT);
|
raster@16367
|
151 |
postfix = adaProject.getEvaluator().getProperty(AdaOptions.PKG_BODY_POSTFIX);
|
raster@15779
|
152 |
}
|
raster@15779
|
153 |
String targetName = (targetFolder != null) ? FileUtil.findFreeFileName(targetFolder, template.getName(), ext) : template.getName();//NOI18N
|
raster@15779
|
154 |
// TODO: manage postfix naming
|
raster@16367
|
155 |
targetName += "." + ext;
|
raster@14352
|
156 |
Templates.setTargetName(this.wizard, targetName);//NOI18N
|
raster@14352
|
157 |
wizardPanels = getPanels();
|
raster@14352
|
158 |
|
raster@14352
|
159 |
// Make sure list of steps is accurate.
|
raster@14352
|
160 |
String[] beforeSteps = (String[]) wizard.getProperty(WizardDescriptor.PROP_CONTENT_DATA);
|
raster@14352
|
161 |
int beforeStepLength = beforeSteps.length - 1;
|
raster@14352
|
162 |
String[] steps = createSteps(beforeSteps);
|
raster@14352
|
163 |
for (int i = 0; i < wizardPanels.length; i++) {
|
raster@14352
|
164 |
Component c = wizardPanels[i].getComponent();
|
raster@14352
|
165 |
if (c instanceof JComponent) { // assume Swing components
|
raster@14352
|
166 |
JComponent jc = (JComponent) c;
|
raster@14352
|
167 |
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, new Integer(i + beforeStepLength - 1)); // NOI18N
|
raster@14352
|
168 |
jc.putClientProperty(WizardDescriptor.PROP_CONTENT_DATA, steps); // NOI18N
|
raster@14352
|
169 |
}
|
raster@14352
|
170 |
}
|
raster@14352
|
171 |
}
|
raster@14352
|
172 |
|
raster@14352
|
173 |
private String[] createSteps(String[] beforeSteps) {
|
raster@14352
|
174 |
int beforeStepLength = beforeSteps.length - 1;
|
raster@14352
|
175 |
String[] res = new String[beforeStepLength + wizardPanels.length];
|
raster@14352
|
176 |
for (int i = 0; i < res.length; i++) {
|
raster@14352
|
177 |
if (i < (beforeStepLength)) {
|
raster@14352
|
178 |
res[i] = beforeSteps[i];
|
raster@14352
|
179 |
} else {
|
raster@14352
|
180 |
res[i] = wizardPanels[i - beforeStepLength].getComponent().getName();
|
raster@14352
|
181 |
}
|
raster@14352
|
182 |
}
|
raster@14352
|
183 |
return res;
|
raster@14352
|
184 |
}
|
raster@14352
|
185 |
|
raster@14352
|
186 |
public void uninitialize(WizardDescriptor wizard) {
|
raster@14352
|
187 |
wizardPanels = null;
|
raster@14352
|
188 |
}
|
raster@14352
|
189 |
|
raster@14352
|
190 |
public WizardDescriptor.Panel current() {
|
raster@14352
|
191 |
return wizardPanels[index];
|
raster@14352
|
192 |
}
|
raster@14352
|
193 |
|
raster@14352
|
194 |
public String name() {
|
raster@14352
|
195 |
return index + 1 + ". from " + getPanels().length;
|
raster@14352
|
196 |
}
|
raster@14352
|
197 |
|
raster@14352
|
198 |
public boolean hasNext() {
|
raster@14352
|
199 |
return index < getPanels().length - 1;
|
raster@14352
|
200 |
}
|
raster@14352
|
201 |
|
raster@14352
|
202 |
public boolean hasPrevious() {
|
raster@14352
|
203 |
return index > 0;
|
raster@14352
|
204 |
}
|
raster@14352
|
205 |
|
raster@14352
|
206 |
public void nextPanel() {
|
raster@14352
|
207 |
if (!hasNext()) {
|
raster@14352
|
208 |
throw new NoSuchElementException();
|
raster@14352
|
209 |
}
|
raster@14352
|
210 |
index++;
|
raster@14352
|
211 |
}
|
raster@14352
|
212 |
|
raster@14352
|
213 |
public void previousPanel() {
|
raster@14352
|
214 |
if (!hasPrevious()) {
|
raster@14352
|
215 |
throw new NoSuchElementException();
|
raster@14352
|
216 |
}
|
raster@14352
|
217 |
index--;
|
raster@14352
|
218 |
}
|
raster@14352
|
219 |
|
raster@14352
|
220 |
// If nothing unusual changes in the middle of the wizard, simply:
|
raster@14352
|
221 |
public void addChangeListener(ChangeListener l) {
|
raster@14352
|
222 |
}
|
raster@14352
|
223 |
|
raster@14352
|
224 |
public void removeChangeListener(ChangeListener l) {
|
raster@14352
|
225 |
}
|
raster@14352
|
226 |
|
raster@14352
|
227 |
|
raster@14352
|
228 |
private FileObject getFileObject(AntProjectHelper helper, String propname) {
|
raster@14352
|
229 |
String prop = helper.getStandardPropertyEvaluator().getProperty(propname);
|
raster@14352
|
230 |
if (prop != null) {
|
raster@14352
|
231 |
return helper.resolveFileObject(prop);
|
raster@14352
|
232 |
}
|
raster@14352
|
233 |
return null;
|
raster@14352
|
234 |
}
|
raster@14352
|
235 |
}
|