KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.templates.ide;
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.templates.IHelpContextIds;
27 import org.eclipse.pde.internal.ui.templates.PDETemplateMessages;
28 import org.eclipse.pde.internal.ui.templates.PDETemplateSection;
29 import org.eclipse.pde.internal.ui.templates.PluginReference;
30 import org.eclipse.pde.ui.IFieldData;
31 import org.eclipse.pde.ui.templates.BooleanOption;
32
33 public class BuilderTemplate extends PDETemplateSection {
34
35     private static final String JavaDoc KEY_BUILDER_CLASS_NAME = "builderClassName"; //$NON-NLS-1$
36

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

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

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

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

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

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

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

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

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

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

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

233     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
234         ArrayList JavaDoc result = new ArrayList JavaDoc();
235         result.add(new PluginReference("org.eclipse.core.resources", null, 0)); //$NON-NLS-1$
236
if (schemaVersion != null)
237             result.add(new PluginReference("org.eclipse.core.runtime", null, //$NON-NLS-1$
238
0));
239         if (actionOption.isSelected())
240             result.add(new PluginReference("org.eclipse.ui", null, 0)); //$NON-NLS-1$
241

242         return (IPluginReference[]) result.toArray(new IPluginReference[result
243                 .size()]);
244     }
245
246     /*
247      * (non-Javadoc)
248      *
249      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
250      */

251     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
252         String JavaDoc packageName = super.getFormattedPackageName(id);
253         if (packageName.length() != 0)
254             return packageName + ".builder"; //$NON-NLS-1$
255
return "builder"; //$NON-NLS-1$
256
}
257
258     /**
259      * @see AbstractTemplateSection#isOkToCreateFile(File)
260      */

261     protected boolean isOkToCreateFile(File JavaDoc sourceFile) {
262         String JavaDoc fileName = sourceFile.getName();
263         if (fileName.equals("ToggleNatureAction.java")) { //$NON-NLS-1$
264
return actionOption.isSelected();
265         }
266         return true;
267     }
268     
269     /* (non-Javadoc)
270      * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getLabel()
271      */

272     public String JavaDoc getLabel() {
273         return getPluginResourceString("newExtension.templates.builder.name"); //$NON-NLS-1$
274
}
275     
276     /* (non-Javadoc)
277      * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getDescription()
278      */

279     public String JavaDoc getDescription() {
280         return getPluginResourceString("newExtension.templates.builder.desc"); //$NON-NLS-1$
281
}
282     
283 }
284
Popular Tags