001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.PDETemplateSection;
025: import org.eclipse.pde.internal.ui.templates.PluginReference;
026: import org.eclipse.pde.ui.IFieldData;
027:
028: public class PreferencePageTemplate extends PDETemplateSection {
029: private static final String KEY_PAGE_NAME = "pageName"; //$NON-NLS-1$
030: private static final String KEY_PAGE_CLASS_NAME = "pageClassName"; //$NON-NLS-1$
031:
032: public PreferencePageTemplate() {
033: setPageCount(1);
034: createOptions();
035: }
036:
037: public String getSectionId() {
038: return "preferences"; //$NON-NLS-1$
039: }
040:
041: /*
042: * @see ITemplateSection#getNumberOfWorkUnits()
043: */
044: public int getNumberOfWorkUnits() {
045: return super .getNumberOfWorkUnits() + 1;
046: }
047:
048: private void createOptions() {
049: // first page
050: addOption(KEY_PACKAGE_NAME,
051: PDETemplateMessages.PreferencePageTemplate_packageName,
052: (String) null, 0);
053: addOption(KEY_PAGE_CLASS_NAME,
054: PDETemplateMessages.PreferencePageTemplate_className,
055: "SamplePreferencePage", //$NON-NLS-1$
056: 0);
057: addOption(
058: KEY_PAGE_NAME,
059: PDETemplateMessages.PreferencePageTemplate_pageName,
060: PDETemplateMessages.PreferencePageTemplate_defaultPageName,
061: 0);
062: }
063:
064: protected void initializeFields(IFieldData data) {
065: // In a new project wizard, we don't know this yet - the
066: // model has not been created
067: String id = data.getId();
068: initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
069: }
070:
071: public void initializeFields(IPluginModelBase model) {
072: // In the new extension wizard, the model exists so
073: // we can initialize directly from it
074: String pluginId = model.getPluginBase().getId();
075: initializeOption(KEY_PACKAGE_NAME,
076: getFormattedPackageName(pluginId));
077: }
078:
079: protected String getTemplateDirectory() {
080: String schemaVersion = model.getPluginBase().getSchemaVersion();
081: return "templates_" + schemaVersion == null ? "3.0" : schemaVersion; //$NON-NLS-1$ //$NON-NLS-2$
082: }
083:
084: public boolean isDependentOnParentWizard() {
085: return true;
086: }
087:
088: /* (non-Javadoc)
089: * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
090: */
091: public IPluginReference[] getDependencies(String schemaVersion) {
092: if (schemaVersion == null)
093: return super .getDependencies(schemaVersion);
094: PluginReference[] deps = new PluginReference[2];
095: deps[0] = new PluginReference(
096: "org.eclipse.core.runtime", null, 0); //$NON-NLS-1$
097: deps[1] = new PluginReference("org.eclipse.ui", null, 0); //$NON-NLS-1$
098: return deps;
099: }
100:
101: public void addPages(Wizard wizard) {
102: WizardPage page = createPage(0,
103: IHelpContextIds.TEMPLATE_PREFERENCE_PAGE);
104: page.setTitle(PDETemplateMessages.PreferencePageTemplate_title);
105: page
106: .setDescription(PDETemplateMessages.PreferencePageTemplate_desc);
107: wizard.addPage(page);
108: markPagesAdded();
109: }
110:
111: public String getUsedExtensionPoint() {
112: return "org.eclipse.ui.preferencePages"; //$NON-NLS-1$
113: }
114:
115: protected void updateModel(IProgressMonitor monitor)
116: throws CoreException {
117: IPluginBase plugin = model.getPluginBase();
118: IPluginExtension extension = createExtension(
119: getUsedExtensionPoint(), true);
120: IPluginModelFactory factory = model.getPluginFactory();
121:
122: String fullClassName = getStringOption(KEY_PACKAGE_NAME)
123: + "." + getStringOption(KEY_PAGE_CLASS_NAME); //$NON-NLS-1$
124:
125: IPluginElement pageElement = factory.createElement(extension);
126: pageElement.setName("page"); //$NON-NLS-1$
127: pageElement.setAttribute("id", fullClassName); //$NON-NLS-1$
128: pageElement
129: .setAttribute("name", getStringOption(KEY_PAGE_NAME)); //$NON-NLS-1$
130: pageElement.setAttribute("class", fullClassName); //$NON-NLS-1$
131: extension.add(pageElement);
132: if (!extension.isInTheModel())
133: plugin.add(extension);
134:
135: IPluginExtension extension2 = createExtension(
136: "org.eclipse.core.runtime.preferences", true); //$NON-NLS-1$
137: IPluginElement prefElement = factory.createElement(extension);
138: prefElement.setName("initializer"); //$NON-NLS-1$
139: prefElement
140: .setAttribute(
141: "class", getStringOption(KEY_PACKAGE_NAME) + ".PreferenceInitializer"); //$NON-NLS-1$ //$NON-NLS-2$
142: extension2.add(prefElement);
143: if (!extension2.isInTheModel())
144: plugin.add(extension2);
145: }
146:
147: /* (non-Javadoc)
148: * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
149: */
150: protected String getFormattedPackageName(String id) {
151: String packageName = super .getFormattedPackageName(id);
152: if (packageName.length() != 0)
153: return packageName + ".preferences"; //$NON-NLS-1$
154: return "preferences"; //$NON-NLS-1$
155: }
156: }
|