KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > ui > templates > support > Templates


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.project.ui.templates.support;
21
22 import java.io.IOException JavaDoc;
23 import org.netbeans.api.project.Project;
24 import org.netbeans.api.project.SourceGroup;
25 import org.netbeans.modules.project.uiapi.ProjectChooserFactory;
26 import org.netbeans.modules.project.uiapi.Utilities;
27 import org.netbeans.spi.project.ui.support.CommonProjectActions;
28 import org.openide.WizardDescriptor;
29 import org.openide.filesystems.FileObject;
30 import org.openide.loaders.DataFolder;
31 import org.openide.loaders.DataObject;
32 import org.openide.loaders.TemplateWizard;
33
34 /**
35  * Default implementations of template UI.
36  * @author Jesse Glick et al.
37  */

38 public class Templates {
39     
40     private Templates() {}
41     
42     /**
43      * Find the project selected for a custom template wizard iterator.
44      * <p class="nonnormative">
45      * If the user selects File | New File, this will be the project chosen in the first panel.
46      * If the user selects New from {@link org.netbeans.spi.project.ui.support.CommonProjectActions#newFileAction}, this will
47      * be the project on which the context menu was invoked.
48      * </p>
49      * @param wizardDescriptor the wizard as passed to {@link WizardDescriptor.InstantiatingIterator#initialize}
50      * or {@link TemplateWizard.Iterator#initialize}
51      * @return the project into which the user has requested this iterator create a file (or null if not set)
52      */

53     public static Project getProject( WizardDescriptor wizardDescriptor ) {
54         return (Project) wizardDescriptor.getProperty( ProjectChooserFactory.WIZARD_KEY_PROJECT );
55     }
56     
57     /**
58      * Find the template with which a custom template wizard iterator is associated.
59      * <p class="nonnormative">
60      * If the user selects File | New File, this will be the template chosen in the first panel.
61      * If the user selects New from {@link org.netbeans.spi.project.ui.support.CommonProjectActions#newFileAction}, this will
62      * be the template selected from the context submenu.
63      * </p>
64      * @param wizardDescriptor the wizard as passed to {@link WizardDescriptor.InstantiatingIterator#initialize}
65      * or {@link TemplateWizard.Iterator#initialize}
66      * @return the corresponding template marker file (or null if not set)
67      */

68     public static FileObject getTemplate( WizardDescriptor wizardDescriptor ) {
69         if (wizardDescriptor == null) {
70             throw new IllegalArgumentException JavaDoc("Cannot pass a null wizardDescriptor"); // NOI18N
71
}
72         if ( wizardDescriptor instanceof TemplateWizard ) {
73             DataObject template = ((TemplateWizard)wizardDescriptor).getTemplate();
74             if (template != null) {
75                 return template.getPrimaryFile();
76             }
77         }
78         return (FileObject) wizardDescriptor.getProperty( ProjectChooserFactory.WIZARD_KEY_TEMPLATE );
79     }
80     
81     /**
82      * Find the target folder selected for a custom template wizard iterator.
83      * <p class="nonnormative">
84      * If the user selects File | New File
85      * this may not be set, unless you have called {@link #setTargetFolder}
86      * in an earlier panel (such as that created by {@link #createSimpleTargetChooser(Project,SourceGroup[])}).
87      * It may however have a preselected folder, e.g. if the user invoked New from
88      * the context menu of a folder.
89      * </p>
90      * @param wizardDescriptor the wizard as passed to {@link WizardDescriptor.InstantiatingIterator#initialize}
91      * or {@link TemplateWizard.Iterator#initialize}
92      * @return the folder into which the user has requested this iterator create a file (or null if not set)
93      */

94     public static FileObject getTargetFolder( WizardDescriptor wizardDescriptor ) {
95         
96         if ( wizardDescriptor instanceof TemplateWizard ) {
97             try {
98                 return ((TemplateWizard)wizardDescriptor).getTargetFolder().getPrimaryFile();
99             }
100             catch ( IOException JavaDoc e ) {
101                 return null;
102             }
103         }
104         else {
105             return (FileObject) wizardDescriptor.getProperty( ProjectChooserFactory.WIZARD_KEY_TARGET_FOLDER );
106         }
107     }
108
109     /**
110      * Find the existing sources folder selected for a custom template wizard iterator.
111      * <p class="nonnormative">
112      * This may not be set, unless you have CommonProjectActions.newProjectAction
113      * with CommonProjectActions.EXISTING_SOURCES_FOLDER value.
114      * <p>
115      *
116      * @param wizardDescriptor the wizard as passed to {@link WizardDescriptor.InstantiatingIterator#initialize}
117      * or {@link TemplateWizard.Iterator#initialize}
118      * @return the existing sources folder from which the user has requested this iterator to create a project
119      *
120      * @since 1.3 (17th May 2005)
121      */

122     public static FileObject getExistingSourcesFolder( WizardDescriptor wizardDescriptor ) {
123         return (FileObject) wizardDescriptor.getProperty( CommonProjectActions.EXISTING_SOURCES_FOLDER );
124     }
125     /**
126      * Stores a target folder so that it can be remembered later using {@link #getTargetFolder}.
127      * @param wizardDescriptor a template wizard
128      * @param folder a target folder to remember
129      */

130     public static void setTargetFolder( WizardDescriptor wizardDescriptor, FileObject folder ) {
131         
132         if ( wizardDescriptor instanceof TemplateWizard ) {
133             DataFolder dataFolder = DataFolder.findFolder( folder );
134             ((TemplateWizard)wizardDescriptor).setTargetFolder( dataFolder );
135         }
136         else {
137             wizardDescriptor.putProperty( ProjectChooserFactory.WIZARD_KEY_TARGET_FOLDER, folder );
138         }
139     }
140
141     /** Method to communicate current choice of target name to a custom
142      * {@link WizardDescriptor.InstantiatingIterator} associated with particular template.
143      * <p>XXX why is this public? only used from NewFileIterator in projectui?
144      * @param wizardDescriptor a file wizard
145      * @return the selected target name (could be null?)
146      * @see TemplateWizard#getTargetName
147      * @see ProjectChooserFactory#WIZARD_KEY_TARGET_NAME
148      */

149     public static String JavaDoc getTargetName( WizardDescriptor wizardDescriptor ) {
150         if ( wizardDescriptor instanceof TemplateWizard ) {
151             return ((TemplateWizard)wizardDescriptor).getTargetName();
152         }
153         else {
154             return (String JavaDoc) wizardDescriptor.getProperty( ProjectChooserFactory.WIZARD_KEY_TARGET_NAME );
155         }
156     }
157     
158     /** Sets the target name for given WizardDescriptor to be used from
159      * custom target choosers
160      * <p>XXX why is this public? only used from SimpleTargetChooserPanel in projectui?
161      * @param wizardDescriptor a file wizard
162      * @param targetName a desired target name
163      * @see TemplateWizard#setTargetName
164      * @see ProjectChooserFactory#WIZARD_KEY_TARGET_NAME
165      */

166     public static void setTargetName( WizardDescriptor wizardDescriptor, String JavaDoc targetName ) {
167         if ( wizardDescriptor instanceof TemplateWizard ) {
168             ((TemplateWizard)wizardDescriptor).setTargetName( targetName );
169         }
170         else {
171             wizardDescriptor.putProperty( ProjectChooserFactory.WIZARD_KEY_TARGET_NAME, targetName );
172         }
173     }
174             
175     /**
176      * Create a basic target chooser suitable for many kinds of templates.
177      * The user is prompted to choose a location for the new file and a (base) name.
178      * Instantiation is handled by {@link DataObject#createFromTemplate}.
179      * @param project The project to work on.
180      * @param folders a list of possible roots to create the new file in
181      * @return a wizard panel(s) prompting the user to choose a name and location
182      */

183     public static WizardDescriptor.Panel createSimpleTargetChooser( Project project, SourceGroup[] folders ) {
184         return createSimpleTargetChooser( project, folders, null );
185     }
186     
187     /**
188      * Create a basic target chooser suitable for many kinds of templates.
189      * The user is prompted to choose a location for the new file and a (base) name.
190      * Instantiation is handled by {@link DataObject#createFromTemplate}.
191      * Resulting panel can be decorated with additional panel placed below the standard target
192      * chooser.
193      * @param project The project to work on.
194      * @param folders a list of possible roots to create the new file in
195      * @param bottomPanel panel which should be placed underneth the default chooser
196      * @return a wizard panel(s) prompting the user to choose a name and location
197      */

198     public static WizardDescriptor.Panel createSimpleTargetChooser( Project project, SourceGroup[] folders, WizardDescriptor.Panel bottomPanel ) {
199         return Utilities.getProjectChooserFactory().createSimpleTargetChooser( project, folders, bottomPanel );
200     }
201         
202 }
203
Popular Tags