KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > templates > BuilderTemplate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards.templates;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.wizard.Wizard;
19 import org.eclipse.jface.wizard.WizardPage;
20 import org.eclipse.pde.core.plugin.IPluginBase;
21 import org.eclipse.pde.core.plugin.IPluginElement;
22 import org.eclipse.pde.core.plugin.IPluginExtension;
23 import org.eclipse.pde.core.plugin.IPluginModelBase;
24 import org.eclipse.pde.core.plugin.IPluginModelFactory;
25 import org.eclipse.pde.core.plugin.IPluginReference;
26 import org.eclipse.pde.internal.ui.IHelpContextIds;
27 import org.eclipse.pde.internal.ui.PDEUIMessages;
28 import org.eclipse.pde.ui.IFieldData;
29 import org.eclipse.pde.ui.templates.BooleanOption;
30 import org.eclipse.pde.ui.templates.TemplateOption;
31
32 public class BuilderTemplate extends PDETemplateSection {
33
34     private static final String JavaDoc KEY_BUILDER_CLASS_NAME = "builderClassName"; //$NON-NLS-1$
35

36     private static final String JavaDoc KEY_BUILDER_ID = "builderId"; //$NON-NLS-1$
37

38     private static final String JavaDoc KEY_BUILDER_NAME = "builderName"; //$NON-NLS-1$
39

40     private static final String JavaDoc KEY_NATURE_CLASS_NAME = "natureClassName"; //$NON-NLS-1$
41

42     private static final String JavaDoc KEY_NATURE_ID = "natureId"; //$NON-NLS-1$
43

44     private static final String JavaDoc KEY_NATURE_NAME = "natureName"; //$NON-NLS-1$
45

46     private static final String JavaDoc KEY_GEN_ACTION = "genAction"; //$NON-NLS-1$
47

48     private BooleanOption actionOption;
49
50     /**
51      * Constructor for BuilderTemplate.
52      */

53     public BuilderTemplate() {
54         setPageCount(1);
55         createOptions();
56     }
57
58     /*
59      * (non-Javadoc)
60      *
61      * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getSectionId()
62      */

63     public String JavaDoc getSectionId() {
64         return "builder"; //$NON-NLS-1$
65
}
66
67     /*
68      * @see ITemplateSection#getNumberOfWorkUnits()
69      */

70     public int getNumberOfWorkUnits() {
71         return super.getNumberOfWorkUnits() + 1;
72     }
73
74     private void createOptions() {
75         addOption(KEY_PACKAGE_NAME, PDEUIMessages.BuilderTemplate_packageLabel, (String JavaDoc) null, 0);
76
77         addOption(KEY_BUILDER_CLASS_NAME, PDEUIMessages.BuilderTemplate_builderClass, "SampleBuilder", 0); //$NON-NLS-1$
78
addOption(KEY_BUILDER_ID, PDEUIMessages.BuilderTemplate_builderId,
79                 "sampleBuilder", 0); //$NON-NLS-1$
80
addOption(KEY_BUILDER_NAME, PDEUIMessages.BuilderTemplate_builderName, PDEUIMessages.BuilderTemplate_defaultBuilderName, 0);
81
82         addOption(KEY_NATURE_CLASS_NAME, PDEUIMessages.BuilderTemplate_natureClass, "SampleNature", 0); //$NON-NLS-1$
83
addOption(KEY_NATURE_ID, PDEUIMessages.BuilderTemplate_natureId,
84                 "sampleNature", 0); //$NON-NLS-1$
85
addOption(KEY_NATURE_NAME, PDEUIMessages.BuilderTemplate_natureName,
86                 PDEUIMessages.BuilderTemplate_defaultNatureName, 0);
87
88         actionOption = (BooleanOption) addOption(KEY_GEN_ACTION, PDEUIMessages.BuilderTemplate_generateAction, true, 0);
89     }
90
91     public void addPages(Wizard wizard) {
92         WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_BUILDER);
93         page.setTitle(PDEUIMessages.BuilderTemplate_title);
94         page.setDescription(PDEUIMessages.BuilderTemplate_desc);
95         wizard.addPage(page);
96         markPagesAdded();
97     }
98
99     public void validateOptions(TemplateOption source) {
100         if (source.isRequired() && source.isEmpty()) {
101             flagMissingRequiredOption(source);
102         } else {
103             validateContainerPage(source);
104         }
105     }
106
107     private void validateContainerPage(TemplateOption source) {
108         TemplateOption[] allPageOptions = getOptions(0);
109         for (int i = 0; i < allPageOptions.length; i++) {
110             TemplateOption nextOption = allPageOptions[i];
111             if (nextOption.isRequired() && nextOption.isEmpty()) {
112                 flagMissingRequiredOption(nextOption);
113                 return;
114             }
115         }
116         resetPageState();
117     }
118
119     public boolean isDependentOnParentWizard() {
120         return true;
121     }
122
123     protected void initializeFields(IFieldData data) {
124         // In a new project wizard, we don't know this yet - the
125
// model has not been created
126
String JavaDoc id = data.getId();
127         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
128     }
129
130     public void initializeFields(IPluginModelBase model) {
131         // In the new extension wizard, the model exists so
132
// we can initialize directly from it
133
String JavaDoc pluginId = model.getPluginBase().getId();
134         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId));
135     }
136
137     /*
138      * (non-Javadoc)
139      *
140      * @see org.eclipse.pde.ui.templates.ITemplateSection#getUsedExtensionPoint()
141      */

142     public String JavaDoc getUsedExtensionPoint() {
143         return null;
144     }
145
146     protected void updateModel(IProgressMonitor monitor) throws CoreException {
147         IPluginBase plugin = model.getPluginBase();
148         IPluginModelFactory factory = model.getPluginFactory();
149
150         // Builder
151
IPluginExtension extension1 = createExtension(
152                 "org.eclipse.core.resources.builders", true); //$NON-NLS-1$
153
extension1.setId(getStringOption(KEY_BUILDER_ID));
154         extension1.setName(getStringOption(KEY_BUILDER_NAME));
155
156         IPluginElement builder = factory.createElement(extension1);
157         builder.setName("builder"); //$NON-NLS-1$
158
builder.setAttribute("hasNature", "true"); //$NON-NLS-1$ //$NON-NLS-2$
159
extension1.add(builder);
160
161         IPluginElement run = factory.createElement(builder);
162         run.setName("run"); //$NON-NLS-1$
163
run.setAttribute("class", getStringOption(KEY_PACKAGE_NAME) //$NON-NLS-1$
164
+ "." + getStringOption(KEY_BUILDER_CLASS_NAME)); //$NON-NLS-1$
165
builder.add(run);
166
167         if (!extension1.isInTheModel())
168             plugin.add(extension1);
169
170         // Nature
171
IPluginExtension extension2 = createExtension(
172                 "org.eclipse.core.resources.natures", true); //$NON-NLS-1$
173
extension2.setId(getStringOption(KEY_NATURE_ID));
174         extension2.setName(getStringOption(KEY_NATURE_NAME));
175
176         IPluginElement runtime = factory.createElement(extension2);
177         runtime.setName("runtime"); //$NON-NLS-1$
178
extension2.add(runtime);
179
180         IPluginElement run2 = factory.createElement(runtime);
181         run2.setName("run"); //$NON-NLS-1$
182
run2.setAttribute("class", getStringOption(KEY_PACKAGE_NAME) //$NON-NLS-1$
183
+ "." + getStringOption(KEY_NATURE_CLASS_NAME)); //$NON-NLS-1$
184
runtime.add(run2);
185
186         IPluginElement builder2 = factory.createElement(extension2);
187         builder2.setName("builder"); //$NON-NLS-1$
188
builder2.setAttribute("id", model.getPluginBase().getId() //$NON-NLS-1$
189
+ "." + getStringOption(KEY_BUILDER_ID)); //$NON-NLS-1$
190
extension2.add(builder2);
191
192         if (!extension2.isInTheModel())
193             plugin.add(extension2);
194
195         // Popup Action
196
if (actionOption.isSelected()) {
197             IPluginExtension extension3 = createExtension(
198                     "org.eclipse.ui.popupMenus", true); //$NON-NLS-1$
199
IPluginElement objectContribution = factory
200                     .createElement(extension3);
201             objectContribution.setName("objectContribution"); //$NON-NLS-1$
202
objectContribution.setAttribute("objectClass", //$NON-NLS-1$
203
"org.eclipse.core.resources.IProject"); //$NON-NLS-1$
204
objectContribution.setAttribute("adaptable", "true"); //$NON-NLS-1$ //$NON-NLS-2$
205
objectContribution.setAttribute("nameFilter", "*"); //$NON-NLS-1$ //$NON-NLS-2$
206
objectContribution.setAttribute("id", model.getPluginBase().getId() //$NON-NLS-1$
207
+ ".contribution1"); //$NON-NLS-1$
208
extension3.add(objectContribution);
209
210             IPluginElement action = factory.createElement(objectContribution);
211             action.setName("action"); //$NON-NLS-1$
212
action.setAttribute(
213                     "label", PDEUIMessages.BuilderTemplate_actionLabel); //$NON-NLS-1$
214
action.setAttribute("class", getStringOption(KEY_PACKAGE_NAME) //$NON-NLS-1$
215
+ ".ToggleNatureAction"); //$NON-NLS-1$
216
action.setAttribute("menubarPath", "additions"); //$NON-NLS-1$ //$NON-NLS-2$
217
action.setAttribute("enablesFor", "+"); //$NON-NLS-1$ //$NON-NLS-2$
218
action.setAttribute("id", model.getPluginBase().getId() //$NON-NLS-1$
219
+ ".addRemoveNatureAction"); //$NON-NLS-1$
220
objectContribution.add(action);
221
222             if (!extension3.isInTheModel())
223                 plugin.add(extension3);
224         }
225
226         // Marker
227
IPluginExtension extension4 = createExtension(
228                 "org.eclipse.core.resources.markers", false); //$NON-NLS-1$
229
extension4.setId("xmlProblem"); //$NON-NLS-1$
230
extension4.setName(PDEUIMessages.BuilderTemplate_markerName);
231
232         IPluginElement superElement = factory.createElement(extension4);
233         superElement.setName("super"); //$NON-NLS-1$
234
superElement.setAttribute("type", //$NON-NLS-1$
235
"org.eclipse.core.resources.problemmarker"); //$NON-NLS-1$
236
extension4.add(superElement);
237
238         IPluginElement persistent = factory.createElement(extension4);
239         persistent.setName("persistent"); //$NON-NLS-1$
240
persistent.setAttribute("value", "true"); //$NON-NLS-1$ //$NON-NLS-2$
241
extension4.add(persistent);
242
243         if (!extension4.isInTheModel())
244             plugin.add(extension4);
245     }
246
247     /*
248      * (non-Javadoc)
249      *
250      * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
251      */

252     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
253         ArrayList JavaDoc result = new ArrayList JavaDoc();
254         result.add(new PluginReference("org.eclipse.core.resources", null, 0)); //$NON-NLS-1$
255
if (schemaVersion != null)
256             result.add(new PluginReference("org.eclipse.core.runtime", null, //$NON-NLS-1$
257
0));
258         if (actionOption.isSelected())
259             result.add(new PluginReference("org.eclipse.ui", null, 0)); //$NON-NLS-1$
260

261         return (IPluginReference[]) result.toArray(new IPluginReference[result
262                 .size()]);
263     }
264
265     /*
266      * (non-Javadoc)
267      *
268      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
269      */

270     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
271         String JavaDoc packageName = super.getFormattedPackageName(id);
272         if (packageName.length() != 0)
273             return packageName + ".builder"; //$NON-NLS-1$
274
return "builder"; //$NON-NLS-1$
275
}
276
277     /**
278      * @see AbstractTemplateSection#isOkToCreateFile(File)
279      */

280     protected boolean isOkToCreateFile(File JavaDoc sourceFile) {
281         String JavaDoc fileName = sourceFile.getName();
282         if (fileName.equals("ToggleNatureAction.java")) { //$NON-NLS-1$
283
return actionOption.isSelected();
284         }
285         return true;
286     }
287 }
288
Popular Tags