1 11 package org.eclipse.pde.internal.ui.templates.ide; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IProgressMonitor; 15 import org.eclipse.jface.wizard.Wizard; 16 import org.eclipse.jface.wizard.WizardPage; 17 import org.eclipse.pde.core.plugin.IPluginBase; 18 import org.eclipse.pde.core.plugin.IPluginElement; 19 import org.eclipse.pde.core.plugin.IPluginExtension; 20 import org.eclipse.pde.core.plugin.IPluginModelBase; 21 import org.eclipse.pde.core.plugin.IPluginModelFactory; 22 import org.eclipse.pde.internal.ui.templates.IHelpContextIds; 23 import org.eclipse.pde.internal.ui.templates.PDETemplateMessages; 24 import org.eclipse.pde.internal.ui.templates.PDETemplateSection; 25 import org.eclipse.pde.ui.IFieldData; 26 import org.eclipse.ui.contexts.IContextService; 27 import org.eclipse.ui.keys.IBindingService; 28 29 public class HelloWorldCmdTemplate extends PDETemplateSection { 30 public static final String KEY_CLASS_NAME = "className"; public static final String KEY_MESSAGE = "message"; public static final String CLASS_NAME = "SampleHandler"; 34 37 public HelloWorldCmdTemplate() { 38 setPageCount(1); 39 createOptions(); 40 } 41 42 public String getSectionId() { 43 return "helloWorldCmd"; } 45 46 49 public int getNumberOfWorkUnits() { 50 return super.getNumberOfWorkUnits() + 1; 51 } 52 53 private void createOptions() { 54 addOption(KEY_PACKAGE_NAME, 55 PDETemplateMessages.HelloWorldCmdTemplate_packageName, 56 (String ) null, 0); 57 addOption(KEY_CLASS_NAME, 58 PDETemplateMessages.HelloWorldCmdTemplate_className, 59 CLASS_NAME, 0); 60 addOption(KEY_MESSAGE, 61 PDETemplateMessages.HelloWorldCmdTemplate_messageText, 62 PDETemplateMessages.HelloWorldCmdTemplate_defaultMessage, 0); 63 } 64 65 public void addPages(Wizard wizard) { 66 WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_HELLO_WORLD); 67 page.setTitle(PDETemplateMessages.HelloWorldCmdTemplate_title); 68 page.setDescription(PDETemplateMessages.HelloWorldCmdTemplate_desc); 69 wizard.addPage(page); 70 markPagesAdded(); 71 } 72 73 public boolean isDependentOnParentWizard() { 74 return true; 75 } 76 77 protected void initializeFields(IFieldData data) { 78 String id = data.getId(); 81 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id)); 82 } 83 84 public void initializeFields(IPluginModelBase model) { 85 String pluginId = model.getPluginBase().getId(); 88 initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId)); 89 } 90 91 public String getUsedExtensionPoint() { 92 return "org.eclipse.ui.commands"; } 94 95 protected void updateModel(IProgressMonitor monitor) throws CoreException { 96 IPluginBase plugin = model.getPluginBase(); 97 IPluginExtension commandsExtension = createExtension( 98 "org.eclipse.ui.commands", true); IPluginModelFactory factory = model.getPluginFactory(); 100 101 IPluginElement category = factory.createElement(commandsExtension); 102 category.setName("category"); String categoryId = plugin.getId() + ".commands.category"; category.setAttribute("id", categoryId); category 106 .setAttribute( 107 "name", PDETemplateMessages.HelloWorldCmdTemplate_sampleCategory); commandsExtension.add(category); 109 110 IPluginElement command = factory.createElement(commandsExtension); 111 command.setName("command"); command.setAttribute("categoryId", categoryId); command.setAttribute("name", PDETemplateMessages.HelloWorldCmdTemplate_sampleAction_name); 115 String commandId = plugin.getId() + ".commands.sampleCommand"; command.setAttribute("id", commandId); commandsExtension.add(command); 118 119 String fullClassName = getStringOption(KEY_PACKAGE_NAME) 120 + "." + getStringOption(KEY_CLASS_NAME); IPluginExtension handlersExtension = createExtension( 122 "org.eclipse.ui.handlers", true); IPluginElement handler = factory.createElement(handlersExtension); 124 handler.setName("handler"); handler.setAttribute("class", fullClassName); handler.setAttribute("commandId", commandId); handlersExtension.add(handler); 128 129 IPluginExtension bindingsExtension = createExtension( 130 "org.eclipse.ui.bindings", true); IPluginElement binding = factory.createElement(bindingsExtension); 132 binding.setName("key"); binding.setAttribute("commandId", commandId); binding.setAttribute("schemeId", IBindingService.DEFAULT_DEFAULT_ACTIVE_SCHEME_ID); binding.setAttribute("contextId", IContextService.CONTEXT_ID_WINDOW); binding.setAttribute("sequence", "M1+6"); bindingsExtension.add(binding); 138 139 IPluginExtension menusExtension = createExtension( 140 "org.eclipse.ui.menus", true); IPluginElement menuAddition = factory.createElement(menusExtension); 142 menuAddition.setName("menuContribution"); menuAddition.setAttribute("locationURI", "menu:org.eclipse.ui.main.menu?after=additions"); IPluginElement menu = factory.createElement(menuAddition); 146 menu.setName("menu"); String menuId = plugin.getId() + ".menus.sampleMenu"; menu.setAttribute("id", menuId); menu.setAttribute("label", PDETemplateMessages.HelloWorldCmdTemplate_sampleMenu_name); 151 menu.setAttribute("mnemonic", PDETemplateMessages.HelloWorldCmdTemplate_sampleMenu_mnemonic); 153 IPluginElement menuCommand = factory.createElement(menu); 154 menuCommand.setName("command"); menuCommand.setAttribute("commandId", commandId); menuCommand.setAttribute("id", plugin.getId() + ".menus.sampleCommand"); menuCommand 158 .setAttribute( 159 "mnemonic", PDETemplateMessages.HelloWorldCmdTemplate_sampleAction_mnemonic); 161 menu.add(menuCommand); 162 menuAddition.add(menu); 163 menusExtension.add(menuAddition); 164 165 IPluginElement toolbarAddition = factory.createElement(menusExtension); 166 toolbarAddition.setName("menuContribution"); toolbarAddition.setAttribute("locationURI", "toolbar:org.eclipse.ui.main.toolbar?after=additions"); IPluginElement toolbar = factory.createElement(toolbarAddition); 170 toolbar.setName("toolbar"); String toolbarId = plugin.getId() + ".toolbars.sampleToolbar"; toolbar.setAttribute("id", toolbarId); IPluginElement toolbarCommand = factory.createElement(toolbar); 174 toolbarCommand.setName("command"); toolbarCommand.setAttribute("id", plugin.getId() + ".toolbars.sampleCommand"); toolbarCommand.setAttribute("commandId", commandId); toolbarCommand.setAttribute("icon", "icons/sample.gif"); toolbarCommand.setAttribute("tooltip", PDETemplateMessages.HelloWorldCmdTemplate_sampleAction_tooltip); 180 toolbar.add(toolbarCommand); 181 toolbarAddition.add(toolbar); 182 menusExtension.add(toolbarAddition); 183 184 if (!commandsExtension.isInTheModel()) { 185 plugin.add(commandsExtension); 186 } 187 if (!handlersExtension.isInTheModel()) { 188 plugin.add(handlersExtension); 189 } 190 if (!bindingsExtension.isInTheModel()) { 191 plugin.add(bindingsExtension); 192 } 193 if (!menusExtension.isInTheModel()) { 194 plugin.add(menusExtension); 195 } 196 } 197 198 203 public String [] getNewFiles() { 204 return new String [] { "icons/" }; } 206 207 212 protected String getFormattedPackageName(String id) { 213 String packageName = super.getFormattedPackageName(id); 214 if (packageName.length() != 0) 215 return packageName + ".handlers"; return "handlers"; } 218 } 219 | Popular Tags |