KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > templates > ide > 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.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.core.plugin.IPluginReference;
23 import org.eclipse.pde.internal.ui.templates.IHelpContextIds;
24 import org.eclipse.pde.internal.ui.templates.PDETemplateMessages;
25 import org.eclipse.pde.internal.ui.templates.PDETemplateSection;
26 import org.eclipse.pde.internal.ui.templates.PluginReference;
27 import org.eclipse.pde.ui.IFieldData;
28
29 public class PopupMenuTemplate extends PDETemplateSection {
30
31     public static final String JavaDoc KEY_TARGET_OBJECT = "objectClass"; //$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(PDETemplateMessages.PopupMenuTemplate_title);
48         page.setDescription(PDETemplateMessages.PopupMenuTemplate_desc);
49         wizard.addPage(page);
50         markPagesAdded();
51     }
52
53     private void createOptions() {
54         addOption(
55             KEY_TARGET_OBJECT,
56             PDETemplateMessages.PopupMenuTemplate_targetClass,
57             "org.eclipse.core.resources.IFile", //$NON-NLS-1$
58
0);
59         addOption(
60             KEY_SUBMENU_LABEL,
61             PDETemplateMessages.PopupMenuTemplate_submenuName,
62             PDETemplateMessages.PopupMenuTemplate_defaultSubmenuName,
63             0);
64         addOption(
65             KEY_ACTION_LABEL,
66             PDETemplateMessages.PopupMenuTemplate_actionLabel,
67             PDETemplateMessages.PopupMenuTemplate_defaultActionName,
68             0);
69         addOption(
70             KEY_PACKAGE_NAME,
71             PDETemplateMessages.PopupMenuTemplate_packageName,
72             (String JavaDoc) null,
73             0);
74         addOption(
75             KEY_ACTION_CLASS,
76             PDETemplateMessages.PopupMenuTemplate_actionClass,
77             PDETemplateMessages.PopupMenuTemplate_newAction,
78             0);
79         addOption(
80             KEY_SELECTION,
81             PDETemplateMessages.PopupMenuTemplate_enabledFor,
82             new String JavaDoc[][] {
83                 { "singleSelection", PDETemplateMessages.PopupMenuTemplate_singleSelection}, //$NON-NLS-1$
84
{
85                 "multipleSelection", PDETemplateMessages.PopupMenuTemplate_multipleSelection //$NON-NLS-1$
86
}
87         }, "singleSelection", 0); //$NON-NLS-1$
88
}
89     /**
90      * @see PDETemplateSection#getSectionId()
91      */

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

117     protected void updateModel(IProgressMonitor monitor) throws CoreException {
118         IPluginBase plugin = model.getPluginBase();
119         IPluginExtension extension = createExtension(getUsedExtensionPoint(), true);
120         IPluginModelFactory factory = model.getPluginFactory();
121
122         IPluginElement objectContributionElement = factory.createElement(extension);
123         objectContributionElement.setName("objectContribution"); //$NON-NLS-1$
124
objectContributionElement.setAttribute(
125             "objectClass", //$NON-NLS-1$
126
getStringOption(KEY_TARGET_OBJECT));
127         objectContributionElement.setAttribute(
128             "id", //$NON-NLS-1$
129
model.getPluginBase().getId() + ".contribution1"); //$NON-NLS-1$
130

131         IPluginElement menuElement = factory.createElement(objectContributionElement);
132         menuElement.setName("menu"); //$NON-NLS-1$
133
menuElement.setAttribute("label", getStringOption(KEY_SUBMENU_LABEL)); //$NON-NLS-1$
134
menuElement.setAttribute("path", "additions"); //$NON-NLS-1$ //$NON-NLS-2$
135
menuElement.setAttribute("id", model.getPluginBase().getId() + ".menu1"); //$NON-NLS-1$ //$NON-NLS-2$
136

137         IPluginElement separatorElement = factory.createElement(menuElement);
138         separatorElement.setName("separator"); //$NON-NLS-1$
139
separatorElement.setAttribute("name", "group1"); //$NON-NLS-1$ //$NON-NLS-2$
140
menuElement.add(separatorElement);
141         objectContributionElement.add(menuElement);
142
143         IPluginElement actionElement = factory.createElement(objectContributionElement);
144         actionElement.setName("action"); //$NON-NLS-1$
145
actionElement.setAttribute("label", getStringOption(KEY_ACTION_LABEL)); //$NON-NLS-1$
146
actionElement.setAttribute(
147             "class", //$NON-NLS-1$
148
getStringOption(KEY_PACKAGE_NAME) + "." + getStringOption(KEY_ACTION_CLASS)); //$NON-NLS-1$
149
actionElement.setAttribute(
150             "menubarPath", //$NON-NLS-1$
151
model.getPluginBase().getId() + ".menu1/group1"); //$NON-NLS-1$
152
actionElement.setAttribute(
153             "enablesFor", //$NON-NLS-1$
154
getValue(KEY_SELECTION).toString().equals("singleSelection") //$NON-NLS-1$
155
? "1" //$NON-NLS-1$
156
: "multiple"); //$NON-NLS-1$
157
actionElement.setAttribute("id", model.getPluginBase().getId() + ".newAction"); //$NON-NLS-1$ //$NON-NLS-2$
158
objectContributionElement.add(actionElement);
159
160         extension.add(objectContributionElement);
161         if (!extension.isInTheModel())
162             plugin.add(extension);
163     }
164
165     /**
166      * @see ITemplateSection#getUsedExtensionPoint()
167      */

168     public String JavaDoc getUsedExtensionPoint() {
169         return "org.eclipse.ui.popupMenus"; //$NON-NLS-1$
170
}
171     
172     /* (non-Javadoc)
173      * @see org.eclipse.pde.internal.ui.wizards.templates.PDETemplateSection#formatPackageName(java.lang.String)
174      */

175     protected String JavaDoc getFormattedPackageName(String JavaDoc id) {
176         String JavaDoc packageName = super.getFormattedPackageName(id);
177         if (packageName.length() !=0)
178             return packageName + ".popup.actions"; //$NON-NLS-1$
179
return "popup.actions"; //$NON-NLS-1$
180
}
181     
182     public IPluginReference[] getDependencies(String JavaDoc schemaVersion) {
183         IPluginReference[] result = new IPluginReference[2];
184         result[0] = new PluginReference("org.eclipse.ui", null, 0); //$NON-NLS-1$
185
result[1] = new PluginReference("org.eclipse.core.resources", null, 0); //$NON-NLS-1$
186
return result;
187     }
188     
189 }
190
Popular Tags