001: /*******************************************************************************
002: * Copyright (c) 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.internal.ui.templates.IHelpContextIds;
022: import org.eclipse.pde.internal.ui.templates.PDETemplateMessages;
023: import org.eclipse.pde.internal.ui.templates.PDETemplateSection;
024: import org.eclipse.pde.ui.IFieldData;
025: import org.eclipse.ui.contexts.IContextService;
026: import org.eclipse.ui.keys.IBindingService;
027:
028: public class HelloWorldCmdTemplate extends PDETemplateSection {
029: public static final String KEY_CLASS_NAME = "className"; //$NON-NLS-1$
030: public static final String KEY_MESSAGE = "message"; //$NON-NLS-1$
031: public static final String CLASS_NAME = "SampleHandler"; //$NON-NLS-1$
032:
033: /**
034: * Constructor for HelloWorldTemplate.
035: */
036: public HelloWorldCmdTemplate() {
037: setPageCount(1);
038: createOptions();
039: }
040:
041: public String getSectionId() {
042: return "helloWorldCmd"; //$NON-NLS-1$
043: }
044:
045: /*
046: * @see ITemplateSection#getNumberOfWorkUnits()
047: */
048: public int getNumberOfWorkUnits() {
049: return super .getNumberOfWorkUnits() + 1;
050: }
051:
052: private void createOptions() {
053: addOption(KEY_PACKAGE_NAME,
054: PDETemplateMessages.HelloWorldCmdTemplate_packageName,
055: (String) null, 0);
056: addOption(KEY_CLASS_NAME,
057: PDETemplateMessages.HelloWorldCmdTemplate_className,
058: CLASS_NAME, 0);
059: addOption(
060: KEY_MESSAGE,
061: PDETemplateMessages.HelloWorldCmdTemplate_messageText,
062: PDETemplateMessages.HelloWorldCmdTemplate_defaultMessage,
063: 0);
064: }
065:
066: public void addPages(Wizard wizard) {
067: WizardPage page = createPage(0,
068: IHelpContextIds.TEMPLATE_HELLO_WORLD);
069: page.setTitle(PDETemplateMessages.HelloWorldCmdTemplate_title);
070: page
071: .setDescription(PDETemplateMessages.HelloWorldCmdTemplate_desc);
072: wizard.addPage(page);
073: markPagesAdded();
074: }
075:
076: public boolean isDependentOnParentWizard() {
077: return true;
078: }
079:
080: protected void initializeFields(IFieldData data) {
081: // In a new project wizard, we don't know this yet - the
082: // model has not been created
083: String id = data.getId();
084: initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
085: }
086:
087: public void initializeFields(IPluginModelBase model) {
088: // In the new extension wizard, the model exists so
089: // we can initialize directly from it
090: String pluginId = model.getPluginBase().getId();
091: initializeOption(KEY_PACKAGE_NAME,
092: getFormattedPackageName(pluginId));
093: }
094:
095: public String getUsedExtensionPoint() {
096: return "org.eclipse.ui.commands"; //$NON-NLS-1$
097: }
098:
099: protected void updateModel(IProgressMonitor monitor)
100: throws CoreException {
101: IPluginBase plugin = model.getPluginBase();
102: IPluginExtension commandsExtension = createExtension(
103: "org.eclipse.ui.commands", true); //$NON-NLS-1$
104: IPluginModelFactory factory = model.getPluginFactory();
105:
106: IPluginElement category = factory
107: .createElement(commandsExtension);
108: category.setName("category"); //$NON-NLS-1$
109: String categoryId = plugin.getId() + ".commands.category"; //$NON-NLS-1$
110: category.setAttribute("id", categoryId); //$NON-NLS-1$
111: category
112: .setAttribute(
113: "name", PDETemplateMessages.HelloWorldCmdTemplate_sampleCategory); //$NON-NLS-1$
114: commandsExtension.add(category);
115:
116: IPluginElement command = factory
117: .createElement(commandsExtension);
118: command.setName("command"); //$NON-NLS-1$
119: command.setAttribute("categoryId", categoryId); //$NON-NLS-1$
120: command
121: .setAttribute(
122: "name", //$NON-NLS-1$
123: PDETemplateMessages.HelloWorldCmdTemplate_sampleAction_name);
124: String commandId = plugin.getId() + ".commands.sampleCommand"; //$NON-NLS-1$
125: command.setAttribute("id", commandId); //$NON-NLS-1$
126: commandsExtension.add(command);
127:
128: String fullClassName = getStringOption(KEY_PACKAGE_NAME)
129: + "." + getStringOption(KEY_CLASS_NAME); //$NON-NLS-1$
130: IPluginExtension handlersExtension = createExtension(
131: "org.eclipse.ui.handlers", true); //$NON-NLS-1$
132: IPluginElement handler = factory
133: .createElement(handlersExtension);
134: handler.setName("handler"); //$NON-NLS-1$
135: handler.setAttribute("class", fullClassName); //$NON-NLS-1$
136: handler.setAttribute("commandId", commandId); //$NON-NLS-1$
137: handlersExtension.add(handler);
138:
139: IPluginExtension bindingsExtension = createExtension(
140: "org.eclipse.ui.bindings", true); //$NON-NLS-1$
141: IPluginElement binding = factory
142: .createElement(bindingsExtension);
143: binding.setName("key"); //$NON-NLS-1$
144: binding.setAttribute("commandId", commandId); //$NON-NLS-1$
145: binding
146: .setAttribute(
147: "schemeId", IBindingService.DEFAULT_DEFAULT_ACTIVE_SCHEME_ID); //$NON-NLS-1$
148: binding.setAttribute(
149: "contextId", IContextService.CONTEXT_ID_WINDOW); //$NON-NLS-1$
150: binding.setAttribute("sequence", "M1+6"); //$NON-NLS-1$ //$NON-NLS-2$
151: bindingsExtension.add(binding);
152:
153: IPluginExtension menusExtension = createExtension(
154: "org.eclipse.ui.menus", true); //$NON-NLS-1$
155: IPluginElement menuAddition = factory
156: .createElement(menusExtension);
157: menuAddition.setName("menuContribution"); //$NON-NLS-1$
158: menuAddition.setAttribute("locationURI", //$NON-NLS-1$
159: "menu:org.eclipse.ui.main.menu?after=additions"); //$NON-NLS-1$
160: IPluginElement menu = factory.createElement(menuAddition);
161: menu.setName("menu"); //$NON-NLS-1$
162: String menuId = plugin.getId() + ".menus.sampleMenu"; //$NON-NLS-1$
163: menu.setAttribute("id", menuId); //$NON-NLS-1$
164: menu
165: .setAttribute(
166: "label", //$NON-NLS-1$
167: PDETemplateMessages.HelloWorldCmdTemplate_sampleMenu_name);
168: menu
169: .setAttribute(
170: "mnemonic", //$NON-NLS-1$
171: PDETemplateMessages.HelloWorldCmdTemplate_sampleMenu_mnemonic);
172: IPluginElement menuCommand = factory.createElement(menu);
173: menuCommand.setName("command"); //$NON-NLS-1$
174: menuCommand.setAttribute("commandId", commandId); //$NON-NLS-1$
175: menuCommand.setAttribute(
176: "id", plugin.getId() + ".menus.sampleCommand"); //$NON-NLS-1$ //$NON-NLS-2$
177: menuCommand
178: .setAttribute(
179: "mnemonic", //$NON-NLS-1$
180: PDETemplateMessages.HelloWorldCmdTemplate_sampleAction_mnemonic);
181: menu.add(menuCommand);
182: menuAddition.add(menu);
183: menusExtension.add(menuAddition);
184:
185: IPluginElement toolbarAddition = factory
186: .createElement(menusExtension);
187: toolbarAddition.setName("menuContribution"); //$NON-NLS-1$
188: toolbarAddition.setAttribute("locationURI", //$NON-NLS-1$
189: "toolbar:org.eclipse.ui.main.toolbar?after=additions"); //$NON-NLS-1$
190: IPluginElement toolbar = factory.createElement(toolbarAddition);
191: toolbar.setName("toolbar"); //$NON-NLS-1$
192: String toolbarId = plugin.getId() + ".toolbars.sampleToolbar"; //$NON-NLS-1$
193: toolbar.setAttribute("id", toolbarId); //$NON-NLS-1$
194: IPluginElement toolbarCommand = factory.createElement(toolbar);
195: toolbarCommand.setName("command"); //$NON-NLS-1$
196: toolbarCommand.setAttribute(
197: "id", plugin.getId() + ".toolbars.sampleCommand"); //$NON-NLS-1$ //$NON-NLS-2$
198: toolbarCommand.setAttribute("commandId", commandId); //$NON-NLS-1$
199: toolbarCommand.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
200: toolbarCommand
201: .setAttribute(
202: "tooltip", //$NON-NLS-1$
203: PDETemplateMessages.HelloWorldCmdTemplate_sampleAction_tooltip);
204: toolbar.add(toolbarCommand);
205: toolbarAddition.add(toolbar);
206: menusExtension.add(toolbarAddition);
207:
208: if (!commandsExtension.isInTheModel()) {
209: plugin.add(commandsExtension);
210: }
211: if (!handlersExtension.isInTheModel()) {
212: plugin.add(handlersExtension);
213: }
214: if (!bindingsExtension.isInTheModel()) {
215: plugin.add(bindingsExtension);
216: }
217: if (!menusExtension.isInTheModel()) {
218: plugin.add(menusExtension);
219: }
220: }
221:
222: /*
223: * (non-Javadoc)
224: *
225: * @see org.eclipse.pde.ui.templates.ITemplateSection#getFoldersToInclude()
226: */
227: public String[] getNewFiles() {
228: return new String[] { "icons/" }; //$NON-NLS-1$
229: }
230:
231: /*
232: * (non-Javadoc)
233: *
234: * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
235: */
236: protected String getFormattedPackageName(String id) {
237: String packageName = super .getFormattedPackageName(id);
238: if (packageName.length() != 0)
239: return packageName + ".handlers"; //$NON-NLS-1$
240: return "handlers"; //$NON-NLS-1$
241: }
242: }
|