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.mobility.project.ui.wizard;
043:
044: import java.text.MessageFormat;
045: import java.util.NoSuchElementException;
046:
047: import javax.swing.event.ChangeListener;
048:
049: import org.openide.ErrorManager;
050: import org.openide.filesystems.FileObject;
051: import org.openide.util.RequestProcessor;
052: import org.openide.util.Utilities;
053: import org.openide.util.NbBundle;
054:
055: import org.netbeans.api.java.classpath.ClassPath;
056:
057: import java.io.IOException;
058: import javax.swing.*;
059:
060: import org.netbeans.api.project.Project;
061: import org.netbeans.api.project.ProjectManager;
062: import org.netbeans.modules.mobility.project.J2MEProject;
063: import org.netbeans.modules.mobility.project.J2MEProjectGenerator;
064: import org.netbeans.spi.project.support.ant.AntProjectHelper;
065: import org.netbeans.spi.project.ui.templates.support.Templates;
066: import org.openide.WizardDescriptor;
067: import org.openide.loaders.DataFolder;
068: import org.openide.loaders.DataObject;
069: import org.openide.loaders.TemplateWizard;
070: import org.openide.util.Utilities;
071:
072: public class FileWizardIterator implements TemplateWizard.Iterator {
073: private static final long serialVersionUID = -1987345825459L;
074:
075: protected static final String JAVA_EXTENSION = "java"; // NOI18N
076:
077: /** Singleton instance of FileWizardIterator, should it be ever needed.
078: */
079: protected static FileWizardIterator instance;
080:
081: /** Index of the current panel. Panels are numbered from 0 to PANEL_COUNT - 1.
082: */
083: protected transient int panelIndex = 0;
084:
085: protected transient WizardDescriptor.Panel[] panels;
086:
087: protected transient TemplateWizard wizardInstance;
088:
089: /** Returns FileWizardIterator singleton instance. This method is used
090: * for constructing the instance from filesystem.attributes.
091: */
092: public static synchronized FileWizardIterator singleton() {
093: if (instance == null) {
094: instance = new FileWizardIterator();
095: }
096: return instance;
097: }
098:
099: // ========================= TemplateWizard.Iterator ============================
100:
101: /** Instantiates the template using informations provided by
102: * the wizard.
103: *
104: * @param wiz the wizard
105: * @return set of data objects that has been created (should contain
106: * at least one
107: * @exception IOException if the instantiation fails
108: */
109: public java.util.Set<DataObject> instantiate(
110: final TemplateWizard wiz) throws IOException,
111: IllegalArgumentException {
112: wizardInstance = wiz;
113: final DataObject obj = instantiateTemplate(wiz.getTemplate(),
114: wiz.getTargetFolder(), wiz.getTargetName());
115:
116: final Object isMidletObject = Templates.getTemplate(
117: wizardInstance).getAttribute(
118: MIDPTargetChooserPanelGUI.IS_MIDLET_TEMPLATE_ATTRIBUTE); // NOI18N
119: boolean isMIDlet = false;
120: if (isMidletObject instanceof Boolean)
121: isMIDlet = ((Boolean) isMidletObject).booleanValue();
122:
123: if (isMIDlet) {
124: final Project p = Templates.getProject(wizardInstance);
125: final AntProjectHelper h = p.getLookup().lookup(
126: AntProjectHelper.class);
127: if (p instanceof J2MEProject && h != null) {
128: RequestProcessor.getDefault().post(new Runnable() {
129: public void run() {
130: try {
131: J2MEProjectGenerator
132: .addMIDletProperty(
133: p,
134: h,
135: (String) wiz
136: .getProperty(MIDPTargetChooserPanel.MIDLET_NAME),
137: (String) wiz
138: .getProperty(MIDPTargetChooserPanel.MIDLET_CLASSNAME),
139: (String) wiz
140: .getProperty(MIDPTargetChooserPanel.MIDLET_ICON));
141: ProjectManager.getDefault().saveProject(p);
142: } catch (IOException e) {
143: e.printStackTrace(); // TODO
144: }
145: }
146: });
147: }
148: }
149: return java.util.Collections.singleton(obj);
150: }
151:
152: public WizardDescriptor.Panel current() {
153: return panels[panelIndex];
154: }
155:
156: public String name() {
157: return current().getComponent().getName();
158: }
159:
160: public boolean hasNext() {
161: return false;
162: }
163:
164: public boolean hasPrevious() {
165: return false;
166: }
167:
168: public void nextPanel() {
169: throw new NoSuchElementException();
170: }
171:
172: public void previousPanel() {
173: throw new NoSuchElementException();
174: }
175:
176: /** Add a listener to changes of the current panel.
177: * The listener is notified when the possibility to move forward/backward changes.
178: * @param l the listener to add
179: */
180: public void addChangeListener(@SuppressWarnings("unused")
181: final ChangeListener l) {
182: }
183:
184: /** Remove a listener to changes of the current panel.
185: * @param l the listener to remove
186: */
187: public void removeChangeListener(@SuppressWarnings("unused")
188: final ChangeListener l) {
189: }
190:
191: public void initialize(final TemplateWizard wizard) {
192: wizardInstance = wizard;
193: if (panels == null) {
194: panels = new WizardDescriptor.Panel[] { new MIDPTargetChooserPanel(), };
195: }
196: panelIndex = 0;
197: updateStepsList();
198: }
199:
200: public void uninitialize(@SuppressWarnings("unused")
201: final TemplateWizard wiz) {
202: wizardInstance = null;
203: panels = null;
204: panelIndex = -1;
205: }
206:
207: // ========================= IMPLEMENTATION ============================
208:
209: /** Instantiates the template. Currently it just delegates to the template DataObject's
210: * createFromTemplate method.
211: */
212: private DataObject instantiateTemplate(final DataObject tpl,
213: final DataFolder target, String name) throws IOException {
214: if (name == null) {
215: name = getDefaultName(tpl, target);
216: }
217:
218: String message;
219: message = checkValidPackageName(target);
220: if (message == null)
221: message = checkTargetName(target, name);
222: if (message != null)
223: throw (IllegalStateException) ErrorManager.getDefault()
224: .annotate(new IllegalStateException(message),
225: ErrorManager.USER, null, message, null,
226: null);
227:
228: return tpl.createFromTemplate(target, name);
229: }
230:
231: private static boolean isValidPackageName(final String s) {
232: // valid package is an empty one, or well-formed java identifier :-)
233: if ("".equals(s)) // NOI18N
234: return true;
235: try {
236: Utilities.isJavaIdentifier(s);
237: return true;
238: } catch (IllegalArgumentException ex) {
239: return false;
240: }
241: }
242:
243: public static String checkValidPackageName(
244: final DataFolder targetFolder) throws IllegalStateException {
245: final FileObject folder = targetFolder.getPrimaryFile();
246: final ClassPath cp = ClassPath.getClassPath(folder,
247: ClassPath.SOURCE);
248: String msg = null;
249: if (cp != null) {
250: final String fullTarget = cp.getResourceName(folder, '.',
251: false);
252: if (isValidPackageName(fullTarget)) {
253: return null;
254: }
255: msg = MessageFormat.format(
256: getString("ERR_File_IllegalFolderName"), // NOI18N
257: new Object[] { folder.getPath(), fullTarget });
258: } else {
259: msg = getString("ERR_File_NotInSourcePath"); // NOI18N
260: }
261: return msg;
262: // checking for java-compatible name - both the folder name and the target name
263: // must be acceptable.
264: }
265:
266: /**
267: * @param folder target folder for java file
268: * @param desiredName name to check
269: * @return true if the desiredName is OK
270: */
271: public static String checkTargetName(final DataFolder folder,
272: final String desiredName) {
273: if (!Utilities.isJavaIdentifier(desiredName)) {
274: final String msg = MessageFormat.format(
275: getString("ERR_File_IllegalTargetName"), // NOI18N
276: new Object[] { desiredName });
277: return msg;
278: }
279:
280: final FileObject f = folder.getPrimaryFile();
281: // check whether the name already exists:
282: if (f.getFileObject(desiredName, JAVA_EXTENSION) != null) {
283: final String msg = MessageFormat.format(
284: getString("ERR_File_TargetExists"), // NOI18N
285: new Object[] { desiredName });
286: return msg;
287: }
288: return null;
289: }
290:
291: private String getDefaultName(final DataObject template,
292: final DataFolder targetFolder) {
293: final String desiredName = org.openide.filesystems.FileUtil
294: .findFreeFileName(targetFolder.getPrimaryFile(),
295: template.getName(), JAVA_EXTENSION);
296: return desiredName;
297: }
298:
299: static String getString(final String key) {
300: return NbBundle.getMessage(FileWizardIterator.class, key);
301: }
302:
303: static char getMnemonic(final String key) {
304: return getString(key).charAt(0);
305: }
306:
307: private void updateStepsList() {
308: final JComponent component = (JComponent) current()
309: .getComponent();
310: component.putClientProperty("WizardPanel_contentData",
311: new String[] {// NOI18N
312: NbBundle.getMessage(MIDPTargetChooserPanel.class,
313: "TITLE_File"), // NOI18N
314: }); // NOI18N
315: component.putClientProperty("WizardPanel_contentSelectedIndex",
316: new Integer(panelIndex)); // NOI18N
317: }
318:
319: }
|