KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > templates > PopupMenuTemplate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.wizards.templates;
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.core.plugin.IPluginReference;
23 import org.eclipse.pde.internal.ui.IHelpContextIds;
24 import org.eclipse.pde.internal.ui.PDEUIMessages;
25 import org.eclipse.pde.ui.IFieldData;
26 import org.eclipse.pde.ui.templates.TemplateOption;
27
28 public class PopupMenuTemplate extends PDETemplateSection {
29
30     public static final String JavaDoc KEY_TARGET_OBJECT = "objectClass"; //$NON-NLS-1$
31
public static final String JavaDoc KEY_NAME_FILTER = "nameFilter"; //$NON-NLS-1$
32
public static final String JavaDoc KEY_SUBMENU_LABEL = "subMenuLabel"; //$NON-NLS-1$
33
public static final String JavaDoc KEY_ACTION_LABEL = "actionLabel"; //$NON-NLS-1$
34
public static final String JavaDoc KEY_ACTION_CLASS = "actionClass"; //$NON-NLS-1$
35
public static final String JavaDoc KEY_SELECTION = "selection"; //$NON-NLS-1$
36

37     /**
38      * Constructor for PropertyPageTemplate.
39      */

40     public PopupMenuTemplate() {
41         setPageCount(1);
42         createOptions();
43     }
44
45     public void addPages(Wizard wizard) {
46         WizardPage page = createPage(0, IHelpContextIds.TEMPLATE_POPUP_MENU);
47         page.setTitle(PDEUIMessages.PopupMenuTemplate_title);
48         page.setDescription(PDEUIMessages.PopupMenuTemplate_desc);
49         wizard.addPage(page);
50         markPagesAdded();
51     }
52
53     private void createOptions() {
54         addOption(
55             KEY_TARGET_OBJECT,
56             PDEUIMessages.PopupMenuTemplate_targetClass,
57             "org.eclipse.core.resources.IFile", //$NON-NLS-1$
58
0);
59         addOption(
60             KEY_NAME_FILTER,
61             PDEUIMessages.PopupMenuTemplate_nameFilter,
62             "plugin.xml", //$NON-NLS-1$
63
0);
64         addOption(
65             KEY_SUBMENU_LABEL,
66             PDEUIMessages.PopupMenuTemplate_submenuName,
67             PDEUIMessages.PopupMenuTemplate_defaultSubmenuName,
68             0);
69         addOption(
70             KEY_ACTION_LABEL,
71             PDEUIMessages.PopupMenuTemplate_actionLabel,
72             PDEUIMessages.PopupMenuTemplate_defaultActionName,
73             0);
74         addOption(
75             KEY_PACKAGE_NAME,
76             PDEUIMessages.PopupMenuTemplate_packageName,
77             (String JavaDoc) null,
78             0);
79         addOption(
80             KEY_ACTION_CLASS,
81             PDEUIMessages.PopupMenuTemplate_actionClass,
82             PDEUIMessages.PopupMenuTemplate_newAction,
83             0);
84         addOption(
85             KEY_SELECTION,
86             PDEUIMessages.PopupMenuTemplate_enabledFor,
87             new String JavaDoc[][] {
88                 { "singleSelection", PDEUIMessages.PopupMenuTemplate_singleSelection}, //$NON-NLS-1$
89
{
90                 "multipleSelection", PDEUIMessages.PopupMenuTemplate_multipleSelection //$NON-NLS-1$
91
}
92         }, "singleSelection", 0); //$NON-NLS-1$
93
}
94     /**
95      * @see PDETemplateSection#getSectionId()
96      */

97     public String JavaDoc getSectionId() {
98         return "popupMenus"; //$NON-NLS-1$
99
}
100
101     public boolean isDependentOnParentWizard() {
102         return true;
103     }
104
105     protected void initializeFields(IFieldData data) {
106         // In a new project wizard, we don't know this yet - the
107
// model has not been created
108
String JavaDoc id = data.getId();
109         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(id));
110     }
111
112     public void initializeFields(IPluginModelBase model) {
113         // In the new extension wizard, the model exists so
114
// we can initialize directly from it
115
String JavaDoc pluginId = model.getPluginBase().getId();
116         initializeOption(KEY_PACKAGE_NAME, getFormattedPackageName(pluginId));
117     }
118
119     /**
120      * @see GenericTemplateSection#validateOptions(TemplateOption)
121      */

122     public void validateOptions(TemplateOption source) {
123         if (source.isRequired() && source.isEmpty()) {
124             flagMissingRequiredOption(source);
125         } else {
126             validateContainerPage(source);
127         }
128     }
129
130     private void validateContainerPage(TemplateOption source) {
131         TemplateOption[] allPageOptions = getOptions(0);
132         for (int i = 0; i < allPageOptions.length; i++) {
133             TemplateOption nextOption = allPageOptions[i];
134             if (nextOption.isRequired() && nextOption.isEmpty()) {
135                 flagMissingRequiredOption(nextOption);
136                 return;
137             }
138         }
139         resetPageState();
140     }
141
142     /**
143      * @see AbstractTemplateSection#updateModel(IProgressMonitor)
144      */

145     protected void updateModel(IProgressMonitor monitor) throws CoreException {
146         IPluginBase plugin = model.getPluginBase();
147         IPluginExtension extension = createExtension(getUsedExtensionPoint(), true);
148         IPluginModelFactory factory = model.getPluginFactory();
149
150         IPluginElement objectContributionElement = factory.createElement(extension);
151         objectContributionElement.setName("objectContribution"); //$NON-NLS-1$
152
objectContributionElement.setAttribute(
153             "objectClass", //$NON-NLS-1$
154
getStringOption(KEY_TARGET_OBJECT));
155         objectContributionElement.setAttribute(
156             "nameFilter", //$NON-NLS-1$
157
getStringOption(KEY_NAME_FILTER));
158         objectContributionElement.setAttribute(
159             "id", //$NON-NLS-1$
160
model.getPluginBase().getId() + ".contribution1"); //$NON-NLS-1$
161

162         IPluginElement menuElement = factory.createElement(objectContributionElement);
163         menuElement.setName("menu"); //$NON-NLS-1$
164
menuElement.setAttribute("label", getStringOption(KEY_SUBMENU_LABEL)); //$NON-NLS-1$
165
menuElement.setAttribute("path", "additions"); //$NON-NLS-1$ //$NON-NLS-2$
166
menuElement.setAttribute("id", model.getPluginBase().getId() + ".menu1"); //$NON-NLS-1$ //$NON-NLS-2$
167

168         IPluginElement separatorElement = factory.createElement(menuElement);
169         separatorElement.setName("separator"); //$NON-NLS-1$
170
separatorElement.setAttribute("name", "group1"); //$NON-NLS-1$ //$NON-NLS-2$
171
menuElement.add(separatorElement);
172         objectContributionElement.add(menuElement);
173
174         IPluginElement actionElement = factory.createElement(objectContributionElement);
175         actionElement.setName("action"); //$NON-NLS-1$
176
actionElement.setAttribute("label", getStringOption(KEY_ACTION_LABEL)); //$NON-NLS-1$
177
actionElement.setAttribute(
178             "class", //$NON-NLS-1$
179
getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_ACTION_CLASS)); //$NON-NLS-1$
180
actionElement.setAttribute(
181             "menubarPath", //$NON-NLS-1$
182
model.getPluginBase().getId() + ".menu1/group1"); //$NON-NLS-1$
183
actionElement.setAttribute(
184             "enablesFor", //$NON-NLS-1$
185
getValue(KEY_SELECTION).toString().equals("singleSelection") //$NON-NLS-1$
186
? "1" //$NON-NLS-1$
187
: "multiple"); //$NON-NLS-1$
188
actionElement.setAttribute("id", model.getPluginBase().getId() + ".newAction"); //$NON-NLS-1$ //$NON-NLS-2$
189
objectContributionElement.add(actionElement);
190
191         extension.add(objectContributionElement);
192         if (!extension.isInTheModel())
193             plugin.add(extension);
194     }
195
196     /**
197      * @see ITemplateSection#getUsedExtensionPoint()
198      */

199     public String JavaDoc getUsedExtensionPoint() {
200         return "org.eclipse.ui.popupMenus"; //$NON-NLS-1$
201
}
202     
203     /* (non-Javadoc)
204      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
205      */

206     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
207         String JavaDoc packageName = super.getFormattedPackageName(id);
208         if (packageName.length() !=0)
209             return packageName + ".popup.actions"; //$NON-NLS-1$
210
return "popup.actions"; //$NON-NLS-1$
211
}
212     
213     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
214         IPluginReference[] result = new IPluginReference[2];
215         result[0] = new PluginReference("org.eclipse.ui", null, 0); //$NON-NLS-1$
216
result[1] = new PluginReference("org.eclipse.core.resources", null, 0); //$NON-NLS-1$
217
return result;
218     }
219     
220 }
221
Popular Tags