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 org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IProgressMonitor;
014: import org.eclipse.jface.wizard.Wizard;
015: import org.eclipse.jface.wizard.WizardPage;
016: import org.eclipse.pde.core.plugin.IPluginBase;
017: import org.eclipse.pde.core.plugin.IPluginElement;
018: import org.eclipse.pde.core.plugin.IPluginExtension;
019: import org.eclipse.pde.core.plugin.IPluginModelBase;
020: import org.eclipse.pde.core.plugin.IPluginModelFactory;
021: import org.eclipse.pde.core.plugin.IPluginReference;
022: import org.eclipse.pde.internal.ui.templates.IHelpContextIds;
023: import org.eclipse.pde.internal.ui.templates.PDETemplateMessages;
024: import org.eclipse.pde.internal.ui.templates.PluginReference;
025: import org.eclipse.pde.ui.IFieldData;
026: import org.eclipse.pde.ui.templates.TemplateOption;
027:
028: public class EditorTemplate extends BaseEditorTemplate {
029: public static final String EDITOR_CLASS_NAME = "editorClass"; //$NON-NLS-1$
030: public static final String EDITOR_NAME = "editorName"; //$NON-NLS-1$
031: public static final String EXTENSIONS = "extensions"; //$NON-NLS-1$
032:
033: /**
034: * Constructor for EditorTemplate.
035: */
036: public EditorTemplate() {
037: setPageCount(1);
038: createOptions();
039: }
040:
041: /* (non-Javadoc)
042: * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
043: */
044: public IPluginReference[] getDependencies(String schemaVersion) {
045: if (schemaVersion != null) {
046: IPluginReference[] dep = new IPluginReference[4];
047: dep[0] = new PluginReference(
048: "org.eclipse.core.runtime", null, 0); //$NON-NLS-1$
049: dep[1] = new PluginReference("org.eclipse.ui", null, 0); //$NON-NLS-1$
050: dep[2] = new PluginReference(
051: "org.eclipse.jface.text", null, 0); //$NON-NLS-1$
052: dep[3] = new PluginReference(
053: "org.eclipse.ui.editors", null, 0); //$NON-NLS-1$
054: return dep;
055: }
056: return super .getDependencies(schemaVersion);
057: }
058:
059: public void addPages(Wizard wizard) {
060: WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_EDITOR);
061: page.setTitle(PDETemplateMessages.EditorTemplate_title);
062: page.setDescription(PDETemplateMessages.EditorTemplate_desc);
063: wizard.addPage(page);
064: markPagesAdded();
065: }
066:
067: private void createOptions() {
068: // first page
069: addOption(KEY_PACKAGE_NAME,
070: PDETemplateMessages.EditorTemplate_packageName,
071: (String) null, 0);
072: addOption(EDITOR_CLASS_NAME,
073: PDETemplateMessages.EditorTemplate_editorClass,
074: "XMLEditor", //$NON-NLS-1$
075: 0);
076: addOption(EDITOR_NAME,
077: PDETemplateMessages.EditorTemplate_editorName,
078: PDETemplateMessages.EditorTemplate_defaultEditorName, 0);
079: addOption(EXTENSIONS,
080: PDETemplateMessages.EditorTemplate_fileExtension,
081: "xml", //$NON-NLS-1$
082: 0);
083: }
084:
085: public String getSectionId() {
086: return "editor"; //$NON-NLS-1$
087: }
088:
089: /*
090: * @see ITemplateSection#getNumberOfWorkUnits()
091: */
092: public int getNumberOfWorkUnits() {
093: return super .getNumberOfWorkUnits() + 1;
094: }
095:
096: protected void initializeFields(IFieldData data) {
097: // In a new project wizard, we don't know this yet - the
098: // model has not been created
099: String id = data.getId();
100: initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
101: }
102:
103: public void initializeFields(IPluginModelBase model) {
104: // In the new extension wizard, the model exists so
105: // we can initialize directly from it
106: String pluginId = model.getPluginBase().getId();
107: initializeOption(KEY_PACKAGE_NAME,
108: getFormattedPackageName(pluginId));
109: }
110:
111: public boolean isDependentOnParentWizard() {
112: return true;
113: }
114:
115: /**
116: * @see GenericTemplateSection#validateOptions(TemplateOption)
117: */
118: public void validateOptions(TemplateOption source) {
119: if (source.isRequired() && source.isEmpty()) {
120: flagMissingRequiredOption(source);
121: } else {
122: validateContainerPage(source);
123: }
124: }
125:
126: private void validateContainerPage(TemplateOption source) {
127: TemplateOption[] options = getOptions(0);
128: for (int i = 0; i < options.length; i++) {
129: TemplateOption nextOption = options[i];
130: if (nextOption.isRequired() && nextOption.isEmpty()) {
131: flagMissingRequiredOption(nextOption);
132: return;
133: }
134: }
135: resetPageState();
136: }
137:
138: protected void updateModel(IProgressMonitor monitor)
139: throws CoreException {
140: IPluginBase plugin = model.getPluginBase();
141: IPluginExtension extension = createExtension(
142: getUsedExtensionPoint(), true);
143: IPluginModelFactory factory = model.getPluginFactory();
144:
145: IPluginElement editorElement = factory.createElement(extension);
146: editorElement.setName("editor"); //$NON-NLS-1$
147: editorElement.setAttribute("id", //$NON-NLS-1$
148: getStringOption(KEY_PACKAGE_NAME)
149: + "." + getStringOption(EDITOR_CLASS_NAME)); //$NON-NLS-1$
150: editorElement
151: .setAttribute("name", getStringOption(EDITOR_NAME)); //$NON-NLS-1$
152: editorElement.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
153: editorElement.setAttribute(
154: "extensions", getStringOption(EXTENSIONS)); //$NON-NLS-1$
155:
156: editorElement.setAttribute("class", //$NON-NLS-1$
157: getStringOption(KEY_PACKAGE_NAME)
158: + "." + getStringOption(EDITOR_CLASS_NAME)); //$NON-NLS-1$
159: editorElement
160: .setAttribute("contributorClass", //$NON-NLS-1$
161: "org.eclipse.ui.texteditor.BasicTextEditorActionContributor"); //$NON-NLS-1$
162: extension.add(editorElement);
163: if (!extension.isInTheModel())
164: plugin.add(extension);
165: }
166:
167: /* (non-Javadoc)
168: * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
169: */
170: protected String getFormattedPackageName(String id) {
171: String packageName = super .getFormattedPackageName(id);
172: if (packageName.length() != 0)
173: return packageName + ".editors"; //$NON-NLS-1$
174: return "editors"; //$NON-NLS-1$
175: }
176: }
|