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 java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IProgressMonitor;
016: import org.eclipse.jface.wizard.Wizard;
017: import org.eclipse.jface.wizard.WizardPage;
018: import org.eclipse.pde.core.plugin.IPluginAttribute;
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.IPluginObject;
025: import org.eclipse.pde.core.plugin.IPluginReference;
026: import org.eclipse.pde.internal.ui.templates.IHelpContextIds;
027: import org.eclipse.pde.internal.ui.templates.PDETemplateMessages;
028: import org.eclipse.pde.internal.ui.templates.PDETemplateSection;
029: import org.eclipse.pde.internal.ui.templates.PluginReference;
030: import org.eclipse.pde.ui.IFieldData;
031:
032: public class NewWizardTemplate extends PDETemplateSection {
033: public NewWizardTemplate() {
034: setPageCount(1);
035: createOptions();
036: }
037:
038: public String getSectionId() {
039: return "newWizard"; //$NON-NLS-1$
040: }
041:
042: /*
043: * @see ITemplateSection#getNumberOfWorkUnits()
044: */
045: public int getNumberOfWorkUnits() {
046: return super .getNumberOfWorkUnits() + 1;
047: }
048:
049: private void createOptions() {
050: // first page
051: addOption(KEY_PACKAGE_NAME,
052: PDETemplateMessages.NewWizardTemplate_packageName,
053: (String) null, 0);
054: addOption(
055: "categoryId", PDETemplateMessages.NewWizardTemplate_categoryId, (String) null, 0); //$NON-NLS-1$
056: addOption(
057: "categoryName", PDETemplateMessages.NewWizardTemplate_categoryName, "Sample Wizards", 0); //$NON-NLS-1$ //$NON-NLS-2$
058: addOption(
059: "wizardClassName", PDETemplateMessages.NewWizardTemplate_className, "SampleNewWizard", 0); //$NON-NLS-1$ //$NON-NLS-2$
060: addOption(
061: "wizardPageClassName", PDETemplateMessages.NewWizardTemplate_pageClassName, "SampleNewWizardPage", 0); //$NON-NLS-1$ //$NON-NLS-2$
062: addOption(
063: "wizardName", PDETemplateMessages.NewWizardTemplate_wizardName, PDETemplateMessages.NewWizardTemplate_defaultName, 0); //$NON-NLS-1$
064: addOption(
065: "extension", PDETemplateMessages.NewWizardTemplate_extension, "mpe", 0); //$NON-NLS-1$ //$NON-NLS-2$
066: addOption(
067: "initialFileName", PDETemplateMessages.NewWizardTemplate_fileName, "new_file.mpe", 0); //$NON-NLS-1$ //$NON-NLS-2$
068: }
069:
070: protected void initializeFields(IFieldData data) {
071: // In a new project wizard, we don't know this yet - the
072: // model has not been created
073: String id = data.getId();
074: initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
075: initializeOption("categoryId", id); //$NON-NLS-1$
076: }
077:
078: public void initializeFields(IPluginModelBase model) {
079: // In the new extension wizard, the model exists so
080: // we can initialize directly from it
081: String pluginId = model.getPluginBase().getId();
082: initializeOption(KEY_PACKAGE_NAME,
083: getFormattedPackageName(pluginId));
084: initializeOption("categoryId", pluginId); //$NON-NLS-1$
085: }
086:
087: /* (non-Javadoc)
088: * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
089: */
090: public IPluginReference[] getDependencies(String schemaVersion) {
091: ArrayList result = new ArrayList();
092: result.add(new PluginReference(
093: "org.eclipse.core.resources", null, 0)); //$NON-NLS-1$
094: result.add(new PluginReference("org.eclipse.ui", null, 0)); //$NON-NLS-1$
095: if (schemaVersion != null) {
096: result.add(new PluginReference(
097: "org.eclipse.ui.ide", null, 0)); //$NON-NLS-1$
098: result.add(new PluginReference(
099: "org.eclipse.core.runtime", null, 0)); //$NON-NLS-1$
100: }
101: return (IPluginReference[]) result
102: .toArray(new IPluginReference[result.size()]);
103: }
104:
105: public boolean isDependentOnParentWizard() {
106: return true;
107: }
108:
109: public void addPages(Wizard wizard) {
110: WizardPage page = createPage(0,
111: IHelpContextIds.TEMPLATE_NEW_WIZARD);
112: page.setTitle(PDETemplateMessages.NewWizardTemplate_title);
113: page.setDescription(PDETemplateMessages.NewWizardTemplate_desc);
114: wizard.addPage(page);
115: markPagesAdded();
116: }
117:
118: public String getUsedExtensionPoint() {
119: return "org.eclipse.ui.newWizards"; //$NON-NLS-1$
120: }
121:
122: protected void updateModel(IProgressMonitor monitor)
123: throws CoreException {
124: IPluginBase plugin = model.getPluginBase();
125: IPluginExtension extension = createExtension(
126: "org.eclipse.ui.newWizards", true); //$NON-NLS-1$
127: IPluginModelFactory factory = model.getPluginFactory();
128:
129: String cid = getStringOption("categoryId"); //$NON-NLS-1$
130:
131: createCategory(extension, cid);
132: String fullClassName = getStringOption(KEY_PACKAGE_NAME)
133: + "." + getStringOption("wizardClassName"); //$NON-NLS-1$ //$NON-NLS-2$
134:
135: IPluginElement viewElement = factory.createElement(extension);
136: viewElement.setName("wizard"); //$NON-NLS-1$
137: viewElement.setAttribute("id", fullClassName); //$NON-NLS-1$
138: viewElement.setAttribute("name", getStringOption("wizardName")); //$NON-NLS-1$ //$NON-NLS-2$
139: viewElement.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
140: viewElement.setAttribute("class", fullClassName); //$NON-NLS-1$
141: viewElement.setAttribute("category", cid); //$NON-NLS-1$
142: extension.add(viewElement);
143: if (!extension.isInTheModel())
144: plugin.add(extension);
145: }
146:
147: private void createCategory(IPluginExtension extension, String id)
148: throws CoreException {
149: IPluginObject[] elements = extension.getChildren();
150: for (int i = 0; i < elements.length; i++) {
151: IPluginElement element = (IPluginElement) elements[i];
152: if (element.getName().equalsIgnoreCase("category")) { //$NON-NLS-1$
153: IPluginAttribute att = element.getAttribute("id"); //$NON-NLS-1$
154: if (att != null) {
155: String cid = att.getValue();
156: if (cid != null && cid.equals(id))
157: return;
158: }
159: }
160: }
161: IPluginElement categoryElement = model.getFactory()
162: .createElement(extension);
163: categoryElement.setName("category"); //$NON-NLS-1$
164: categoryElement.setAttribute(
165: "name", getStringOption("categoryName")); //$NON-NLS-1$ //$NON-NLS-2$
166: categoryElement.setAttribute("id", id); //$NON-NLS-1$
167: extension.add(categoryElement);
168: }
169:
170: /* (non-Javadoc)
171: * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getFoldersToInclude()
172: */
173: public String[] getNewFiles() {
174: return new String[] { "icons/" }; //$NON-NLS-1$
175: }
176:
177: /* (non-Javadoc)
178: * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
179: */
180: protected String getFormattedPackageName(String id) {
181: String packageName = super .getFormattedPackageName(id);
182: if (packageName.length() != 0)
183: return packageName + ".wizards"; //$NON-NLS-1$
184: return "wizards"; //$NON-NLS-1$
185: }
186: }
|