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.spi.project.ui.templates.support;
043:
044: import java.io.IOException;
045: import org.netbeans.api.project.Project;
046: import org.netbeans.api.project.SourceGroup;
047: import org.netbeans.modules.project.uiapi.ProjectChooserFactory;
048: import org.netbeans.modules.project.uiapi.Utilities;
049: import org.netbeans.spi.project.ui.support.CommonProjectActions;
050: import org.openide.WizardDescriptor;
051: import org.openide.filesystems.FileObject;
052: import org.openide.loaders.DataFolder;
053: import org.openide.loaders.DataObject;
054: import org.openide.loaders.TemplateWizard;
055:
056: /**
057: * Default implementations of template UI.
058: * @author Jesse Glick et al.
059: */
060: public class Templates {
061:
062: private Templates() {
063: }
064:
065: /**
066: * Find the project selected for a custom template wizard iterator.
067: * <p class="nonnormative">
068: * If the user selects File | New File, this will be the project chosen in the first panel.
069: * If the user selects New from {@link org.netbeans.spi.project.ui.support.CommonProjectActions#newFileAction}, this will
070: * be the project on which the context menu was invoked.
071: * </p>
072: * @param wizardDescriptor the wizard as passed to {@link WizardDescriptor.InstantiatingIterator#initialize}
073: * or {@link TemplateWizard.Iterator#initialize}
074: * @return the project into which the user has requested this iterator create a file (or null if not set)
075: */
076: public static Project getProject(WizardDescriptor wizardDescriptor) {
077: return (Project) wizardDescriptor
078: .getProperty(ProjectChooserFactory.WIZARD_KEY_PROJECT);
079: }
080:
081: /**
082: * Find the template with which a custom template wizard iterator is associated.
083: * <p class="nonnormative">
084: * If the user selects File | New File, this will be the template chosen in the first panel.
085: * If the user selects New from {@link org.netbeans.spi.project.ui.support.CommonProjectActions#newFileAction}, this will
086: * be the template selected from the context submenu.
087: * </p>
088: * @param wizardDescriptor the wizard as passed to {@link WizardDescriptor.InstantiatingIterator#initialize}
089: * or {@link TemplateWizard.Iterator#initialize}
090: * @return the corresponding template marker file (or null if not set)
091: */
092: public static FileObject getTemplate(
093: WizardDescriptor wizardDescriptor) {
094: if (wizardDescriptor == null) {
095: throw new IllegalArgumentException(
096: "Cannot pass a null wizardDescriptor"); // NOI18N
097: }
098: if (wizardDescriptor instanceof TemplateWizard) {
099: DataObject template = ((TemplateWizard) wizardDescriptor)
100: .getTemplate();
101: if (template != null) {
102: return template.getPrimaryFile();
103: }
104: }
105: return (FileObject) wizardDescriptor
106: .getProperty(ProjectChooserFactory.WIZARD_KEY_TEMPLATE);
107: }
108:
109: /**
110: * Find the target folder selected for a custom template wizard iterator.
111: * <p class="nonnormative">
112: * If the user selects File | New File
113: * this may not be set, unless you have called {@link #setTargetFolder}
114: * in an earlier panel (such as that created by {@link #createSimpleTargetChooser(Project,SourceGroup[])}).
115: * It may however have a preselected folder, e.g. if the user invoked New from
116: * the context menu of a folder.
117: * </p>
118: * @param wizardDescriptor the wizard as passed to {@link WizardDescriptor.InstantiatingIterator#initialize}
119: * or {@link TemplateWizard.Iterator#initialize}
120: * @return the folder into which the user has requested this iterator create a file (or null if not set)
121: */
122: public static FileObject getTargetFolder(
123: WizardDescriptor wizardDescriptor) {
124:
125: if (wizardDescriptor instanceof TemplateWizard) {
126: try {
127: return ((TemplateWizard) wizardDescriptor)
128: .getTargetFolder().getPrimaryFile();
129: } catch (IOException e) {
130: return null;
131: }
132: } else {
133: return (FileObject) wizardDescriptor
134: .getProperty(ProjectChooserFactory.WIZARD_KEY_TARGET_FOLDER);
135: }
136: }
137:
138: /**
139: * Find the existing sources folder selected for a custom template wizard iterator.
140: * <p class="nonnormative">
141: * This may not be set, unless you have CommonProjectActions.newProjectAction
142: * with CommonProjectActions.EXISTING_SOURCES_FOLDER value.
143: * <p>
144: *
145: * @param wizardDescriptor the wizard as passed to {@link WizardDescriptor.InstantiatingIterator#initialize}
146: * or {@link TemplateWizard.Iterator#initialize}
147: * @return the existing sources folder from which the user has requested this iterator to create a project
148: *
149: * @since 1.3 (17th May 2005)
150: */
151: public static FileObject getExistingSourcesFolder(
152: WizardDescriptor wizardDescriptor) {
153: return (FileObject) wizardDescriptor
154: .getProperty(CommonProjectActions.EXISTING_SOURCES_FOLDER);
155: }
156:
157: /**
158: * Stores a target folder so that it can be remembered later using {@link #getTargetFolder}.
159: * @param wizardDescriptor a template wizard
160: * @param folder a target folder to remember
161: */
162: public static void setTargetFolder(
163: WizardDescriptor wizardDescriptor, FileObject folder) {
164:
165: if (wizardDescriptor instanceof TemplateWizard) {
166: if (folder == null) {
167: //#103971
168: ((TemplateWizard) wizardDescriptor)
169: .setTargetFolder(null);
170: } else {
171: DataFolder dataFolder = DataFolder.findFolder(folder);
172: ((TemplateWizard) wizardDescriptor)
173: .setTargetFolder(dataFolder);
174: }
175: } else {
176: wizardDescriptor.putProperty(
177: ProjectChooserFactory.WIZARD_KEY_TARGET_FOLDER,
178: folder);
179: }
180: }
181:
182: /** Method to communicate current choice of target name to a custom
183: * {@link WizardDescriptor.InstantiatingIterator} associated with particular template.
184: * <p>XXX why is this public? only used from NewFileIterator in projectui?
185: * @param wizardDescriptor a file wizard
186: * @return the selected target name (could be null?)
187: * @see TemplateWizard#getTargetName
188: * @see ProjectChooserFactory#WIZARD_KEY_TARGET_NAME
189: */
190: public static String getTargetName(WizardDescriptor wizardDescriptor) {
191: if (wizardDescriptor instanceof TemplateWizard) {
192: return ((TemplateWizard) wizardDescriptor).getTargetName();
193: } else {
194: return (String) wizardDescriptor
195: .getProperty(ProjectChooserFactory.WIZARD_KEY_TARGET_NAME);
196: }
197: }
198:
199: /** Sets the target name for given WizardDescriptor to be used from
200: * custom target choosers
201: * <p>XXX why is this public? only used from SimpleTargetChooserPanel in projectui?
202: * @param wizardDescriptor a file wizard
203: * @param targetName a desired target name
204: * @see TemplateWizard#setTargetName
205: * @see ProjectChooserFactory#WIZARD_KEY_TARGET_NAME
206: */
207: public static void setTargetName(WizardDescriptor wizardDescriptor,
208: String targetName) {
209: if (wizardDescriptor instanceof TemplateWizard) {
210: ((TemplateWizard) wizardDescriptor)
211: .setTargetName(targetName);
212: } else {
213: wizardDescriptor.putProperty(
214: ProjectChooserFactory.WIZARD_KEY_TARGET_NAME,
215: targetName);
216: }
217: }
218:
219: /**
220: * Create a basic target chooser suitable for many kinds of templates.
221: * The user is prompted to choose a location for the new file and a (base) name.
222: * Instantiation is handled by {@link DataObject#createFromTemplate}.
223: * @param project The project to work on.
224: * @param folders a list of possible roots to create the new file in
225: * @return a wizard panel(s) prompting the user to choose a name and location
226: */
227: public static WizardDescriptor.Panel<WizardDescriptor> createSimpleTargetChooser(
228: Project project, SourceGroup[] folders) {
229: return createSimpleTargetChooser(project, folders, null);
230: }
231:
232: /**
233: * Create a basic target chooser suitable for many kinds of templates.
234: * The user is prompted to choose a location for the new file and a (base) name.
235: * Instantiation is handled by {@link DataObject#createFromTemplate}.
236: * Resulting panel can be decorated with additional panel placed below the standard target
237: * chooser.
238: * @param project The project to work on.
239: * @param folders a list of possible roots to create the new file in
240: * @param bottomPanel panel which should be placed underneth the default chooser
241: * @return a wizard panel(s) prompting the user to choose a name and location
242: */
243: public static WizardDescriptor.Panel<WizardDescriptor> createSimpleTargetChooser(
244: Project project, SourceGroup[] folders,
245: WizardDescriptor.Panel<WizardDescriptor> bottomPanel) {
246: return Utilities.getProjectChooserFactory()
247: .createSimpleTargetChooser(project, folders,
248: bottomPanel);
249: }
250:
251: }
|