2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.groovy.groovyproject.ui.wizards;
44
import java.awt.Component;
46
import java.io.IOException;
47
import java.io.OutputStream;
48
import java.io.PrintWriter;
49
import java.text.MessageFormat;
50
import java.util.HashSet;
51
import java.util.NoSuchElementException;
53
import javax.swing.JComponent;
54
import javax.swing.event.ChangeListener;
55
import org.netbeans.api.project.Project;
56
import org.netbeans.api.project.ProjectManager;
57
import org.netbeans.modules.groovy.groovyproject.GroovyProjectGenerator;
58
import org.netbeans.modules.groovy.groovyproject.ui.FoldersListSettings;
59
import org.netbeans.spi.project.support.ant.AntProjectHelper;
60
import org.netbeans.spi.project.ui.support.ProjectChooser;
61
import org.openide.ErrorManager;
62
import org.openide.WizardDescriptor;
63
import org.openide.filesystems.FileLock;
64
import org.openide.filesystems.FileObject;
65
import org.openide.filesystems.FileUtil;
66
import org.openide.util.NbBundle;
70
* Wizard to create a new J2SE project.
72
public class NewGroovyProjectWizardIterator implements WizardDescriptor.InstantiatingIterator {
74
static final int TYPE_APP = 0;
75
static final int TYPE_LIB = 1;
77
static final String PROP_NAME_INDEX = "nameIndex"; //NOI18N
79
private static final String MANIFEST_FILE = "manifest.mf"; // NOI18N
81
private static final long serialVersionUID = 1L;
85
/** Create a new wizard iterator. */
86
public NewGroovyProjectWizardIterator() {
90
public NewGroovyProjectWizardIterator(int type) {
94
public static NewGroovyProjectWizardIterator library() {
95
return new NewGroovyProjectWizardIterator( TYPE_LIB );
98
private WizardDescriptor.Panel[] createPanels () {
99
return new WizardDescriptor.Panel[] {
100
new PanelConfigureProject( this.type )
104
private String[] createSteps() {
105
return new String[] {
106
NbBundle.getMessage(NewGroovyProjectWizardIterator.class,"LAB_ConfigureProject"),
111
public Set/*<FileObject>*/ instantiate () throws IOException {
112
Set resultSet = new HashSet ();
113
File dirF = (File)wiz.getProperty("projdir"); //NOI18N
115
dirF = FileUtil.normalizeFile(dirF);
117
String name = (String)wiz.getProperty("name"); //NOI18N
118
String mainScript = (String)wiz.getProperty("mainScript"); //NOI18N
119
AntProjectHelper h = GroovyProjectGenerator.createProject(dirF, name, mainScript, type == TYPE_APP ? MANIFEST_FILE : null);
120
if (mainScript != null && mainScript.length () > 0) {
122
//String sourceRoot = "src"; //(String)groovyProperties.get (J2SEProjectProperties.SRC_DIR);
123
FileObject sourcesRoot = h.getProjectDirectory ().getFileObject ("src"); //NOI18N
124
FileObject mainScriptFo = getMainScriptFO (sourcesRoot, mainScript);
125
assert mainScriptFo != null : "sourcesRoot: " + sourcesRoot + ", mainScript: " + mainScript; //NOI18N
126
// Returning FileObject of main script, will be called its preferred action
127
resultSet.add (mainScriptFo);
128
} catch (Exception x) {
129
ErrorManager.getDefault().notify(x);
132
if ( type == TYPE_LIB ) {
133
resultSet.add( h.getProjectDirectory ().getFileObject ("src") ); //NOI18N
135
FileObject dir = FileUtil.toFileObject(dirF);
136
if (type == TYPE_APP) {
137
createManifest(dir, MANIFEST_FILE);
139
Project p = ProjectManager.getDefault().findProject(dir);
141
// Returning FileObject of project diretory.
142
// Project will be open and set as main
143
Integer index = (Integer) wiz.getProperty(PROP_NAME_INDEX);
146
FoldersListSettings.getDefault().setNewApplicationCount(index.intValue());
149
FoldersListSettings.getDefault().setNewLibraryCount(index.intValue());
154
dirF = (dirF != null) ? dirF.getParentFile() : null;
155
if (dirF != null && dirF.exists()) {
156
ProjectChooser.setProjectsFolder (dirF);
163
private transient int index;
164
private transient WizardDescriptor.Panel[] panels;
165
private transient WizardDescriptor wiz;
167
public void initialize(WizardDescriptor wiz) {
170
panels = createPanels();
171
// Make sure list of steps is accurate.
172
String[] steps = createSteps();
173
for (int i = 0; i < panels.length; i++) {
174
Component c = panels[i].getComponent();
175
if (steps[i] == null) {
176
// Default step name to component name of panel.
177
// Mainly useful for getting the name of the target
178
// chooser to appear in the list of steps.
179
steps[i] = c.getName();
181
if (c instanceof JComponent) { // assume Swing components
182
JComponent jc = (JComponent)c;
184
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i)); // NOI18N
185
// Step name (actually the whole list for reference).
186
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
190
public void uninitialize(WizardDescriptor wiz) {
191
this.wiz.putProperty("projdir",null); //NOI18N
192
this.wiz.putProperty("name",null); //NOI18N
193
this.wiz.putProperty("mainScript",null); //NOI18N
198
public String name() {
199
return MessageFormat.format (NbBundle.getMessage(NewGroovyProjectWizardIterator.class,"LAB_IteratorName"),
200
new Object[] {new Integer (index + 1), new Integer (panels.length) });
203
public boolean hasNext() {
204
return index < panels.length - 1;
206
public boolean hasPrevious() {
209
public void nextPanel() {
210
if (!hasNext()) throw new NoSuchElementException();
213
public void previousPanel() {
214
if (!hasPrevious()) throw new NoSuchElementException();
217
public WizardDescriptor.Panel current () {
218
return panels[index];
221
// If nothing unusual changes in the middle of the wizard, simply:
222
public final void addChangeListener(ChangeListener l) {}
223
public final void removeChangeListener(ChangeListener l) {}
225
// helper methods, finds mainclass's FileObject
226
private FileObject getMainScriptFO (FileObject sourcesRoot, String mainScript) {
227
// ignore unvalid mainScript ???
228
return sourcesRoot.getFileObject(mainScript, "groovy"); // NOI18N
231
static String getPackageName (String displayName) {
232
StringBuffer builder = new StringBuffer ();
233
boolean firstLetter = true;
234
for (int i=0; i< displayName.length(); i++) {
235
char c = displayName.charAt(i);
236
if ((!firstLetter && Character.isJavaIdentifierPart (c)) || (firstLetter && Character.isJavaIdentifierStart(c))) {
238
if (Character.isUpperCase(c)) {
239
c = Character.toLowerCase(c);
244
return builder.length() == 0 ? NbBundle.getMessage(NewGroovyProjectWizardIterator.class,"TXT_DefaultPackageName") : builder.toString();
248
* Create a new application manifest file with minimal initial contents.
249
* @param dir the directory to create it in
250
* @param path the relative path of the file
251
* @throws IOException in case of problems
253
private static void createManifest(FileObject dir, String path) throws IOException {
254
FileObject manifest = dir.createData(MANIFEST_FILE);
255
FileLock lock = manifest.lock();
257
OutputStream os = manifest.getOutputStream(lock);
259
PrintWriter pw = new PrintWriter(os);
260
pw.println("Manifest-Version: 1.0"); // NOI18N
261
pw.println("X-COMMENT: Main-Class will be added automatically by build"); // NOI18N
262
pw.println(); // safest to end in \n\n due to JRE parsing bug