KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > templates > ide > HelloWorldCmdTemplate


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc KEY_CLASS_NAME = "className"; //$NON-NLS-1$
31
public static final String JavaDoc KEY_MESSAGE = "message"; //$NON-NLS-1$
32
public static final String JavaDoc CLASS_NAME = "SampleHandler"; //$NON-NLS-1$
33

34     /**
35      * Constructor for HelloWorldTemplate.
36      */

37     public HelloWorldCmdTemplate() {
38         setPageCount(1);
39         createOptions();
40     }
41
42     public String JavaDoc getSectionId() {
43         return "helloWorldCmd"; //$NON-NLS-1$
44
}
45
46     /*
47      * @see ITemplateSection#getNumberOfWorkUnits()
48      */

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 JavaDoc) 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         // In a new project wizard, we don't know this yet - the
79
// model has not been created
80
String JavaDoc id = data.getId();
81         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
82     }
83
84     public void initializeFields(IPluginModelBase model) {
85         // In the new extension wizard, the model exists so
86
// we can initialize directly from it
87
String JavaDoc pluginId = model.getPluginBase().getId();
88         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId));
89     }
90
91     public String JavaDoc getUsedExtensionPoint() {
92         return "org.eclipse.ui.commands"; //$NON-NLS-1$
93
}
94
95     protected void updateModel(IProgressMonitor monitor) throws CoreException {
96         IPluginBase plugin = model.getPluginBase();
97         IPluginExtension commandsExtension = createExtension(
98                 "org.eclipse.ui.commands", true); //$NON-NLS-1$
99
IPluginModelFactory factory = model.getPluginFactory();
100
101         IPluginElement category = factory.createElement(commandsExtension);
102         category.setName("category"); //$NON-NLS-1$
103
String JavaDoc categoryId = plugin.getId() + ".commands.category"; //$NON-NLS-1$
104
category.setAttribute("id", categoryId); //$NON-NLS-1$
105
category
106                 .setAttribute(
107                         "name", PDETemplateMessages.HelloWorldCmdTemplate_sampleCategory); //$NON-NLS-1$
108
commandsExtension.add(category);
109
110         IPluginElement command = factory.createElement(commandsExtension);
111         command.setName("command"); //$NON-NLS-1$
112
command.setAttribute("categoryId", categoryId); //$NON-NLS-1$
113
command.setAttribute("name", //$NON-NLS-1$
114
PDETemplateMessages.HelloWorldCmdTemplate_sampleAction_name);
115         String JavaDoc commandId = plugin.getId() + ".commands.sampleCommand"; //$NON-NLS-1$
116
command.setAttribute("id", commandId); //$NON-NLS-1$
117
commandsExtension.add(command);
118
119         String JavaDoc fullClassName = getStringOption(KEY_PACKAGE_NAME)
120                 + "." + getStringOption(KEY_CLASS_NAME); //$NON-NLS-1$
121
IPluginExtension handlersExtension = createExtension(
122                 "org.eclipse.ui.handlers", true); //$NON-NLS-1$
123
IPluginElement handler = factory.createElement(handlersExtension);
124         handler.setName("handler"); //$NON-NLS-1$
125
handler.setAttribute("class", fullClassName); //$NON-NLS-1$
126
handler.setAttribute("commandId", commandId); //$NON-NLS-1$
127
handlersExtension.add(handler);
128         
129         IPluginExtension bindingsExtension = createExtension(
130                 "org.eclipse.ui.bindings", true); //$NON-NLS-1$
131
IPluginElement binding = factory.createElement(bindingsExtension);
132         binding.setName("key"); //$NON-NLS-1$
133
binding.setAttribute("commandId", commandId); //$NON-NLS-1$
134
binding.setAttribute("schemeId", IBindingService.DEFAULT_DEFAULT_ACTIVE_SCHEME_ID); //$NON-NLS-1$
135
binding.setAttribute("contextId", IContextService.CONTEXT_ID_WINDOW); //$NON-NLS-1$
136
binding.setAttribute("sequence", "M1+6"); //$NON-NLS-1$ //$NON-NLS-2$
137
bindingsExtension.add(binding);
138         
139         IPluginExtension menusExtension = createExtension(
140                 "org.eclipse.ui.menus", true); //$NON-NLS-1$
141
IPluginElement menuAddition = factory.createElement(menusExtension);
142         menuAddition.setName("menuContribution"); //$NON-NLS-1$
143
menuAddition.setAttribute("locationURI", //$NON-NLS-1$
144
"menu:org.eclipse.ui.main.menu?after=additions"); //$NON-NLS-1$
145
IPluginElement menu = factory.createElement(menuAddition);
146         menu.setName("menu"); //$NON-NLS-1$
147
String JavaDoc menuId = plugin.getId() + ".menus.sampleMenu"; //$NON-NLS-1$
148
menu.setAttribute("id", menuId); //$NON-NLS-1$
149
menu.setAttribute("label", //$NON-NLS-1$
150
PDETemplateMessages.HelloWorldCmdTemplate_sampleMenu_name);
151         menu.setAttribute("mnemonic", //$NON-NLS-1$
152
PDETemplateMessages.HelloWorldCmdTemplate_sampleMenu_mnemonic);
153         IPluginElement menuCommand = factory.createElement(menu);
154         menuCommand.setName("command"); //$NON-NLS-1$
155
menuCommand.setAttribute("commandId", commandId); //$NON-NLS-1$
156
menuCommand.setAttribute("id", plugin.getId() + ".menus.sampleCommand"); //$NON-NLS-1$ //$NON-NLS-2$
157
menuCommand
158                 .setAttribute(
159                         "mnemonic", //$NON-NLS-1$
160
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"); //$NON-NLS-1$
167
toolbarAddition.setAttribute("locationURI", //$NON-NLS-1$
168
"toolbar:org.eclipse.ui.main.toolbar?after=additions"); //$NON-NLS-1$
169
IPluginElement toolbar = factory.createElement(toolbarAddition);
170         toolbar.setName("toolbar"); //$NON-NLS-1$
171
String JavaDoc toolbarId = plugin.getId() + ".toolbars.sampleToolbar"; //$NON-NLS-1$
172
toolbar.setAttribute("id", toolbarId); //$NON-NLS-1$
173
IPluginElement toolbarCommand = factory.createElement(toolbar);
174         toolbarCommand.setName("command"); //$NON-NLS-1$
175
toolbarCommand.setAttribute("id", plugin.getId() + ".toolbars.sampleCommand"); //$NON-NLS-1$ //$NON-NLS-2$
176
toolbarCommand.setAttribute("commandId", commandId); //$NON-NLS-1$
177
toolbarCommand.setAttribute("icon", "icons/sample.gif"); //$NON-NLS-1$ //$NON-NLS-2$
178
toolbarCommand.setAttribute("tooltip", //$NON-NLS-1$
179
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     /*
199      * (non-Javadoc)
200      *
201      * @see org.eclipse.pde.ui.templates.ITemplateSection#getFoldersToInclude()
202      */

203     public String JavaDoc[] getNewFiles() {
204         return new String JavaDoc[] { "icons/" }; //$NON-NLS-1$
205
}
206
207     /*
208      * (non-Javadoc)
209      *
210      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
211      */

212     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
213         String JavaDoc packageName = super.getFormattedPackageName(id);
214         if (packageName.length() != 0)
215             return packageName + ".handlers"; //$NON-NLS-1$
216
return "handlers"; //$NON-NLS-1$
217
}
218 }
219
Popular Tags