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.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.IPluginAttribute;
020: import org.eclipse.pde.core.plugin.IPluginBase;
021: import org.eclipse.pde.core.plugin.IPluginElement;
022: import org.eclipse.pde.core.plugin.IPluginExtension;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.core.plugin.IPluginModelFactory;
025: import org.eclipse.pde.core.plugin.IPluginObject;
026: import org.eclipse.pde.core.plugin.IPluginReference;
027: import org.eclipse.pde.internal.ui.templates.IHelpContextIds;
028: import org.eclipse.pde.internal.ui.templates.PDETemplateMessages;
029: import org.eclipse.pde.internal.ui.templates.PDETemplateSection;
030: import org.eclipse.pde.internal.ui.templates.PluginReference;
031: import org.eclipse.pde.ui.IFieldData;
032: import org.eclipse.pde.ui.templates.BooleanOption;
033:
034: public class ViewTemplate extends PDETemplateSection {
035: private BooleanOption addToPerspective;
036: private BooleanOption contextHelp;
037:
038: /**
039: * Constructor for HelloWorldTemplate.
040: */
041: public ViewTemplate() {
042: setPageCount(1);
043: createOptions();
044: }
045:
046: public String getSectionId() {
047: return "view"; //$NON-NLS-1$
048: }
049:
050: /*
051: * @see ITemplateSection#getNumberOfWorkUnits()
052: */
053: public int getNumberOfWorkUnits() {
054: return super .getNumberOfWorkUnits() + 1;
055: }
056:
057: private void createOptions() {
058: // first page
059: addOption(KEY_PACKAGE_NAME,
060: PDETemplateMessages.ViewTemplate_packageName,
061: (String) null, 0);
062: addOption(
063: "className", PDETemplateMessages.ViewTemplate_className, "SampleView", 0); //$NON-NLS-1$ //$NON-NLS-2$
064: addOption(
065: "viewName", PDETemplateMessages.ViewTemplate_name, PDETemplateMessages.ViewTemplate_defaultName, 0); //$NON-NLS-1$
066: addOption(
067: "viewCategoryId", PDETemplateMessages.ViewTemplate_categoryId, (String) null, 0); //$NON-NLS-1$
068: addOption(
069: "viewCategoryName", PDETemplateMessages.ViewTemplate_categoryName, PDETemplateMessages.ViewTemplate_defaultCategoryName, 0); //$NON-NLS-1$
070: addOption("viewType", PDETemplateMessages.ViewTemplate_select, //$NON-NLS-1$
071: new String[][] {
072: {
073: "tableViewer", PDETemplateMessages.ViewTemplate_table }, //$NON-NLS-1$
074: {
075: "treeViewer", PDETemplateMessages.ViewTemplate_tree } }, //$NON-NLS-1$
076: "tableViewer", 0); //$NON-NLS-1$
077: addToPerspective = (BooleanOption) addOption(
078: "addToPerspective", PDETemplateMessages.ViewTemplate_addToPerspective, true, 0); //$NON-NLS-1$
079: contextHelp = (BooleanOption) addOption(
080: "contextHelp", PDETemplateMessages.ViewTemplate_contextHelp, true, 0); //$NON-NLS-1$
081: }
082:
083: protected void initializeFields(IFieldData data) {
084: // In a new project wizard, we don't know this yet - the
085: // model has not been created
086: initializeFields(data.getId());
087:
088: }
089:
090: public void initializeFields(IPluginModelBase model) {
091: // In the new extension wizard, the model exists so
092: // we can initialize directly from it
093: initializeFields(model.getPluginBase().getId());
094: }
095:
096: public void initializeFields(String id) {
097: initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
098: initializeOption("viewCategoryId", id); //$NON-NLS-1$
099: }
100:
101: public boolean isDependentOnParentWizard() {
102: return true;
103: }
104:
105: public void addPages(Wizard wizard) {
106: WizardPage page0 = createPage(0, IHelpContextIds.TEMPLATE_VIEW);
107: page0.setTitle(PDETemplateMessages.ViewTemplate_title0);
108: page0.setDescription(PDETemplateMessages.ViewTemplate_desc0);
109: wizard.addPage(page0);
110:
111: markPagesAdded();
112: }
113:
114: /**
115: * @see AbstractTemplateSection#isOkToCreateFile(File)
116: */
117: protected boolean isOkToCreateFile(File sourceFile) {
118: boolean isOk = true;
119: String fileName = sourceFile.getName();
120: if (fileName.equals("contexts.xml")) { //$NON-NLS-1$
121: isOk = contextHelp.isSelected();
122: }
123: return isOk;
124: }
125:
126: public String getUsedExtensionPoint() {
127: return "org.eclipse.ui.views"; //$NON-NLS-1$
128: }
129:
130: protected void updateModel(IProgressMonitor monitor)
131: throws CoreException {
132: IPluginBase plugin = model.getPluginBase();
133: IPluginExtension extension = createExtension(
134: "org.eclipse.ui.views", true); //$NON-NLS-1$
135: IPluginModelFactory factory = model.getPluginFactory();
136:
137: String cid = getStringOption("viewCategoryId"); //$NON-NLS-1$
138:
139: createCategory(extension, cid);
140: String fullClassName = getStringOption(KEY_PACKAGE_NAME)
141: + "." + getStringOption("className"); //$NON-NLS-1$ //$NON-NLS-2$
142:
143: IPluginElement viewElement = factory.createElement(extension);
144: viewElement.setName("view"); //$NON-NLS-1$
145: viewElement.setAttribute("id", fullClassName); //$NON-NLS-1$
146: viewElement.setAttribute("name", getStringOption("viewName")); //$NON-NLS-1$ //$NON-NLS-2$
147: viewElement.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
148:
149: viewElement.setAttribute("class", fullClassName); //$NON-NLS-1$
150: viewElement.setAttribute("category", cid); //$NON-NLS-1$
151: extension.add(viewElement);
152: if (!extension.isInTheModel())
153: plugin.add(extension);
154:
155: if (addToPerspective.isSelected()) {
156: IPluginExtension perspectiveExtension = createExtension(
157: "org.eclipse.ui.perspectiveExtensions", true); //$NON-NLS-1$
158:
159: IPluginElement perspectiveElement = factory
160: .createElement(perspectiveExtension);
161: perspectiveElement.setName("perspectiveExtension"); //$NON-NLS-1$
162: perspectiveElement.setAttribute("targetID", //$NON-NLS-1$
163: "org.eclipse.jdt.ui.JavaPerspective"); //$NON-NLS-1$
164:
165: IPluginElement view = factory
166: .createElement(perspectiveElement);
167: view.setName("view"); //$NON-NLS-1$
168: view.setAttribute("id", fullClassName); //$NON-NLS-1$
169: view.setAttribute(
170: "relative", "org.eclipse.ui.views.TaskList"); //$NON-NLS-1$ //$NON-NLS-2$
171: view.setAttribute("relationship", "right"); //$NON-NLS-1$ //$NON-NLS-2$
172: view.setAttribute("ratio", "0.5"); //$NON-NLS-1$ //$NON-NLS-2$
173: perspectiveElement.add(view);
174:
175: perspectiveExtension.add(perspectiveElement);
176: if (!perspectiveExtension.isInTheModel())
177: plugin.add(perspectiveExtension);
178: }
179:
180: if (contextHelp.isSelected()) {
181: IPluginExtension contextExtension = createExtension(
182: "org.eclipse.help.contexts", true); //$NON-NLS-1$
183:
184: IPluginElement contextsElement = factory
185: .createElement(contextExtension);
186: contextsElement.setName("contexts"); //$NON-NLS-1$
187: contextsElement.setAttribute("file", "contexts.xml"); //$NON-NLS-1$ //$NON-NLS-2$
188: contextExtension.add(contextsElement);
189: if (!contextExtension.isInTheModel())
190: plugin.add(contextExtension);
191: }
192: }
193:
194: private void createCategory(IPluginExtension extension, String id)
195: throws CoreException {
196: IPluginObject[] elements = extension.getChildren();
197: for (int i = 0; i < elements.length; i++) {
198: IPluginElement element = (IPluginElement) elements[i];
199: if (element.getName().equalsIgnoreCase("category")) { //$NON-NLS-1$
200: IPluginAttribute att = element.getAttribute("id"); //$NON-NLS-1$
201: if (att != null) {
202: String cid = att.getValue();
203: if (cid != null && cid.equals(id))
204: return;
205: }
206: }
207: }
208: IPluginElement categoryElement = model.getFactory()
209: .createElement(extension);
210: categoryElement.setName("category"); //$NON-NLS-1$
211: categoryElement.setAttribute(
212: "name", getStringOption("viewCategoryName")); //$NON-NLS-1$ //$NON-NLS-2$
213: categoryElement.setAttribute("id", id); //$NON-NLS-1$
214: extension.add(categoryElement);
215: }
216:
217: /* (non-Javadoc)
218: * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#getFoldersToInclude()
219: */
220: public String[] getNewFiles() {
221: if (contextHelp.isSelected())
222: return new String[] { "icons/", "contexts.xml" }; //$NON-NLS-1$ //$NON-NLS-2$
223: return new String[] { "icons/" }; //$NON-NLS-1$
224: }
225:
226: /* (non-Javadoc)
227: * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getDependencies(java.lang.String)
228: */
229: public IPluginReference[] getDependencies(String schemaVersion) {
230: ArrayList result = new ArrayList();
231: if (schemaVersion != null)
232: result.add(new PluginReference(
233: "org.eclipse.core.runtime", null, 0)); //$NON-NLS-1$
234: result.add(new PluginReference("org.eclipse.ui", null, 0)); //$NON-NLS-1$
235: return (IPluginReference[]) result
236: .toArray(new IPluginReference[result.size()]);
237: }
238:
239: /* (non-Javadoc)
240: * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
241: */
242: protected String getFormattedPackageName(String id) {
243: String packageName = super .getFormattedPackageName(id);
244: if (packageName.length() != 0)
245: return packageName + ".views"; //$NON-NLS-1$
246: return "views"; //$NON-NLS-1$
247: }
248:
249: public Object getValue(String name) {
250: if (name.equals("useEnablement")) //$NON-NLS-1$
251: return new Boolean(getTargetVersion() >= 3.3);
252: return super.getValue(name);
253: }
254: }
|