001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.templates.ide;
011:
012: import java.io.File;
013: import java.util.ArrayList;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.jface.wizard.Wizard;
018: import org.eclipse.jface.wizard.WizardPage;
019: import org.eclipse.pde.core.plugin.IPluginBase;
020: import org.eclipse.pde.core.plugin.IPluginElement;
021: import org.eclipse.pde.core.plugin.IPluginExtension;
022: import org.eclipse.pde.core.plugin.IPluginModelBase;
023: import org.eclipse.pde.core.plugin.IPluginModelFactory;
024: import org.eclipse.pde.core.plugin.IPluginReference;
025: import org.eclipse.pde.internal.ui.templates.IHelpContextIds;
026: import org.eclipse.pde.internal.ui.templates.PDETemplateMessages;
027: import org.eclipse.pde.internal.ui.templates.PDETemplateSection;
028: import org.eclipse.pde.internal.ui.templates.PluginReference;
029: import org.eclipse.pde.ui.IFieldData;
030: import org.eclipse.pde.ui.templates.BooleanOption;
031:
032: public class BuilderTemplate extends PDETemplateSection {
033:
034: private static final String KEY_BUILDER_CLASS_NAME = "builderClassName"; //$NON-NLS-1$
035:
036: private static final String KEY_BUILDER_ID = "builderId"; //$NON-NLS-1$
037:
038: private static final String KEY_BUILDER_NAME = "builderName"; //$NON-NLS-1$
039:
040: private static final String KEY_NATURE_CLASS_NAME = "natureClassName"; //$NON-NLS-1$
041:
042: private static final String KEY_NATURE_ID = "natureId"; //$NON-NLS-1$
043:
044: private static final String KEY_NATURE_NAME = "natureName"; //$NON-NLS-1$
045:
046: private static final String KEY_GEN_ACTION = "genAction"; //$NON-NLS-1$
047:
048: private BooleanOption actionOption;
049:
050: /**
051: * Constructor for BuilderTemplate.
052: */
053: public BuilderTemplate() {
054: setPageCount(1);
055: createOptions();
056: }
057:
058: /*
059: * (non-Javadoc)
060: *
061: * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getSectionId()
062: */
063: public String getSectionId() {
064: return "builder"; //$NON-NLS-1$
065: }
066:
067: /*
068: * @see ITemplateSection#getNumberOfWorkUnits()
069: */
070: public int getNumberOfWorkUnits() {
071: return super .getNumberOfWorkUnits() + 1;
072: }
073:
074: private void createOptions() {
075: addOption(KEY_PACKAGE_NAME,
076: PDETemplateMessages.BuilderTemplate_packageLabel,
077: (String) null, 0);
078:
079: addOption(KEY_BUILDER_CLASS_NAME,
080: PDETemplateMessages.BuilderTemplate_builderClass,
081: "SampleBuilder", 0); //$NON-NLS-1$
082: addOption(KEY_BUILDER_ID,
083: PDETemplateMessages.BuilderTemplate_builderId,
084: "sampleBuilder", 0); //$NON-NLS-1$
085: addOption(KEY_BUILDER_NAME,
086: PDETemplateMessages.BuilderTemplate_builderName,
087: PDETemplateMessages.BuilderTemplate_defaultBuilderName,
088: 0);
089:
090: addOption(KEY_NATURE_CLASS_NAME,
091: PDETemplateMessages.BuilderTemplate_natureClass,
092: "SampleNature", 0); //$NON-NLS-1$
093: addOption(KEY_NATURE_ID,
094: PDETemplateMessages.BuilderTemplate_natureId,
095: "sampleNature", 0); //$NON-NLS-1$
096: addOption(KEY_NATURE_NAME,
097: PDETemplateMessages.BuilderTemplate_natureName,
098: PDETemplateMessages.BuilderTemplate_defaultNatureName,
099: 0);
100:
101: actionOption = (BooleanOption) addOption(KEY_GEN_ACTION,
102: PDETemplateMessages.BuilderTemplate_generateAction,
103: true, 0);
104: }
105:
106: public void addPages(Wizard wizard) {
107: WizardPage page = createPage(0,
108: IHelpContextIds.TEMPLATE_BUILDER);
109: page.setTitle(PDETemplateMessages.BuilderTemplate_title);
110: page.setDescription(PDETemplateMessages.BuilderTemplate_desc);
111: wizard.addPage(page);
112: markPagesAdded();
113: }
114:
115: public boolean isDependentOnParentWizard() {
116: return true;
117: }
118:
119: protected void initializeFields(IFieldData data) {
120: // In a new project wizard, we don't know this yet - the
121: // model has not been created
122: String id = data.getId();
123: initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
124: }
125:
126: public void initializeFields(IPluginModelBase model) {
127: // In the new extension wizard, the model exists so
128: // we can initialize directly from it
129: String pluginId = model.getPluginBase().getId();
130: initializeOption(KEY_PACKAGE_NAME,
131: getFormattedPackageName(pluginId));
132: }
133:
134: /*
135: * (non-Javadoc)
136: *
137: * @see org.eclipse.pde.ui.templates.ITemplateSection#getUsedExtensionPoint()
138: */
139: public String getUsedExtensionPoint() {
140: return "org.eclipse.core.resources.builders"; //$NON-NLS-1$
141: }
142:
143: protected void updateModel(IProgressMonitor monitor)
144: throws CoreException {
145: IPluginBase plugin = model.getPluginBase();
146: IPluginModelFactory factory = model.getPluginFactory();
147:
148: // Builder
149: IPluginExtension extension1 = createExtension(
150: "org.eclipse.core.resources.builders", true); //$NON-NLS-1$
151: extension1.setId(getStringOption(KEY_BUILDER_ID));
152: extension1.setName(getStringOption(KEY_BUILDER_NAME));
153:
154: IPluginElement builder = factory.createElement(extension1);
155: builder.setName("builder"); //$NON-NLS-1$
156: builder.setAttribute("hasNature", "true"); //$NON-NLS-1$ //$NON-NLS-2$
157: extension1.add(builder);
158:
159: IPluginElement run = factory.createElement(builder);
160: run.setName("run"); //$NON-NLS-1$
161: run.setAttribute("class", getStringOption(KEY_PACKAGE_NAME) //$NON-NLS-1$
162: + "." + getStringOption(KEY_BUILDER_CLASS_NAME)); //$NON-NLS-1$
163: builder.add(run);
164:
165: if (!extension1.isInTheModel())
166: plugin.add(extension1);
167:
168: // Nature
169: IPluginExtension extension2 = createExtension(
170: "org.eclipse.core.resources.natures", true); //$NON-NLS-1$
171: extension2.setId(getStringOption(KEY_NATURE_ID));
172: extension2.setName(getStringOption(KEY_NATURE_NAME));
173:
174: IPluginElement runtime = factory.createElement(extension2);
175: runtime.setName("runtime"); //$NON-NLS-1$
176: extension2.add(runtime);
177:
178: IPluginElement run2 = factory.createElement(runtime);
179: run2.setName("run"); //$NON-NLS-1$
180: run2.setAttribute("class", getStringOption(KEY_PACKAGE_NAME) //$NON-NLS-1$
181: + "." + getStringOption(KEY_NATURE_CLASS_NAME)); //$NON-NLS-1$
182: runtime.add(run2);
183:
184: IPluginElement builder2 = factory.createElement(extension2);
185: builder2.setName("builder"); //$NON-NLS-1$
186: builder2.setAttribute("id", model.getPluginBase().getId() //$NON-NLS-1$
187: + "." + getStringOption(KEY_BUILDER_ID)); //$NON-NLS-1$
188: extension2.add(builder2);
189:
190: if (!extension2.isInTheModel())
191: plugin.add(extension2);
192:
193: // Popup Action
194: if (actionOption.isSelected()) {
195: IPluginExtension extension3 = createExtension(
196: "org.eclipse.ui.popupMenus", true); //$NON-NLS-1$
197: IPluginElement objectContribution = factory
198: .createElement(extension3);
199: objectContribution.setName("objectContribution"); //$NON-NLS-1$
200: objectContribution.setAttribute("objectClass", //$NON-NLS-1$
201: "org.eclipse.core.resources.IProject"); //$NON-NLS-1$
202: objectContribution.setAttribute("adaptable", "true"); //$NON-NLS-1$ //$NON-NLS-2$
203: objectContribution.setAttribute("nameFilter", "*"); //$NON-NLS-1$ //$NON-NLS-2$
204: objectContribution.setAttribute(
205: "id", model.getPluginBase().getId() //$NON-NLS-1$
206: + ".contribution1"); //$NON-NLS-1$
207: extension3.add(objectContribution);
208:
209: IPluginElement action = factory
210: .createElement(objectContribution);
211: action.setName("action"); //$NON-NLS-1$
212: action
213: .setAttribute(
214: "label", PDETemplateMessages.BuilderTemplate_actionLabel); //$NON-NLS-1$
215: action.setAttribute(
216: "class", getStringOption(KEY_PACKAGE_NAME) //$NON-NLS-1$
217: + ".ToggleNatureAction"); //$NON-NLS-1$
218: action.setAttribute("menubarPath", "additions"); //$NON-NLS-1$ //$NON-NLS-2$
219: action.setAttribute("enablesFor", "+"); //$NON-NLS-1$ //$NON-NLS-2$
220: action.setAttribute("id", model.getPluginBase().getId() //$NON-NLS-1$
221: + ".addRemoveNatureAction"); //$NON-NLS-1$
222: objectContribution.add(action);
223:
224: if (!extension3.isInTheModel())
225: plugin.add(extension3);
226: }
227:
228: // Marker
229: IPluginExtension extension4 = createExtension(
230: "org.eclipse.core.resources.markers", false); //$NON-NLS-1$
231: extension4.setId("xmlProblem"); //$NON-NLS-1$
232: extension4
233: .setName(PDETemplateMessages.BuilderTemplate_markerName);
234:
235: IPluginElement super Element = factory.createElement(extension4);
236: super Element.setName("super"); //$NON-NLS-1$
237: super Element.setAttribute("type", //$NON-NLS-1$
238: "org.eclipse.core.resources.problemmarker"); //$NON-NLS-1$
239: extension4.add(super Element);
240:
241: IPluginElement persistent = factory.createElement(extension4);
242: persistent.setName("persistent"); //$NON-NLS-1$
243: persistent.setAttribute("value", "true"); //$NON-NLS-1$ //$NON-NLS-2$
244: extension4.add(persistent);
245:
246: if (!extension4.isInTheModel())
247: plugin.add(extension4);
248: }
249:
250: /*
251: * (non-Javadoc)
252: *
253: * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
254: */
255: public IPluginReference[] getDependencies(String schemaVersion) {
256: ArrayList result = new ArrayList();
257: result.add(new PluginReference(
258: "org.eclipse.core.resources", null, 0)); //$NON-NLS-1$
259: if (schemaVersion != null)
260: result.add(new PluginReference(
261: "org.eclipse.core.runtime", null, //$NON-NLS-1$
262: 0));
263: if (actionOption.isSelected())
264: result.add(new PluginReference("org.eclipse.ui", null, 0)); //$NON-NLS-1$
265:
266: return (IPluginReference[]) result
267: .toArray(new IPluginReference[result.size()]);
268: }
269:
270: /*
271: * (non-Javadoc)
272: *
273: * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
274: */
275: protected String getFormattedPackageName(String id) {
276: String packageName = super .getFormattedPackageName(id);
277: if (packageName.length() != 0)
278: return packageName + ".builder"; //$NON-NLS-1$
279: return "builder"; //$NON-NLS-1$
280: }
281:
282: /**
283: * @see AbstractTemplateSection#isOkToCreateFile(File)
284: */
285: protected boolean isOkToCreateFile(File sourceFile) {
286: String fileName = sourceFile.getName();
287: if (fileName.equals("ToggleNatureAction.java")) { //$NON-NLS-1$
288: return actionOption.isSelected();
289: }
290: return true;
291: }
292:
293: /* (non-Javadoc)
294: * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getLabel()
295: */
296: public String getLabel() {
297: return getPluginResourceString("newExtension.templates.builder.name"); //$NON-NLS-1$
298: }
299:
300: /* (non-Javadoc)
301: * @see org.eclipse.pde.ui.templates.OptionTemplateSection#getDescription()
302: */
303: public String getDescription() {
304: return getPluginResourceString("newExtension.templates.builder.desc"); //$NON-NLS-1$
305: }
306:
307: }
|