Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 316303 | Differences between
and this patch

Collapse All | Expand All

(-)Eclipse (+133 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2010 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
12
package org.eclipse.ui.internal.menus;
13
14
import org.eclipse.core.runtime.IConfigurationElement;
15
import org.eclipse.core.runtime.Path;
16
import org.eclipse.e4.core.contexts.IEclipseContext;
17
import org.eclipse.e4.ui.model.application.MApplication;
18
import org.eclipse.e4.ui.model.application.ui.MElementContainer;
19
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
20
import org.eclipse.e4.ui.model.application.ui.menu.MMenuContribution;
21
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
22
import org.eclipse.e4.ui.model.application.ui.menu.impl.MenuFactoryImpl;
23
import org.eclipse.ui.IWorkbenchActionConstants;
24
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
25
26
/**
27
 * @since e4
28
 * 
29
 */
30
public class ActionSet {
31
32
	private IConfigurationElement configElement;
33
	private MMenuContribution menuContribution;
34
	private MApplication application;
35
36
	public ActionSet(MApplication application, IEclipseContext appContext,
37
			IConfigurationElement element) {
38
		this.application = application;
39
		this.configElement = element;
40
	}
41
42
	public void addToModel() {
43
		menuContribution = MenuFactoryImpl.eINSTANCE.createMenuContribution();
44
		String idContrib = MenuHelper.getId(configElement);
45
		if (idContrib != null && idContrib.length() > 0) {
46
			menuContribution.setElementId(idContrib);
47
		}
48
		System.out.println("actionSet: " + idContrib); //$NON-NLS-1$
49
		IConfigurationElement[] menus = configElement
50
				.getChildren(IWorkbenchRegistryConstants.TAG_MENU);
51
		for (IConfigurationElement element : menus) {
52
			addMenu(menuContribution, element);
53
		}
54
	}
55
56
	private void addMenu(MElementContainer<MMenuElement> menu, IConfigurationElement element) {
57
		final String elementId = MenuHelper.getId(element);
58
		String path = MenuHelper.getPath(element);
59
		if (path == null || path.length() == 0) {
60
			path = IWorkbenchActionConstants.MB_ADDITIONS;
61
		}
62
		Path menuPath = new Path(path);
63
		MElementContainer<MMenuElement> parentMenu = menu;
64
		if (menuPath.segmentCount() > 1) {
65
			parentMenu = findMenuFromPath(menu, menuPath, 0);
66
		}
67
		if (parentMenu == menu) {
68
			System.err.println("Using parent menu for menu " + elementId + ':' + path); //$NON-NLS-1$
69
		}
70
		if (parentMenu == null) {
71
			System.err.println("Unable to find group for menu " + elementId + ':' + path); //$NON-NLS-1$
72
			return;
73
		}
74
		String id = MenuHelper.getId(element);
75
		MMenu item = null;
76
		final int itemIdx = MenuHelper.indexForId(parentMenu, id);
77
		if (itemIdx == -1) {
78
			int idx = MenuHelper.indexForId(parentMenu, menuPath.lastSegment());
79
			if (idx == -1) {
80
				idx = MenuHelper.indexForId(parentMenu, IWorkbenchActionConstants.MB_ADDITIONS);
81
			}
82
			if (idx == -1) {
83
				System.err.println("Failed to find group for menu " + elementId + ':' + path); //$NON-NLS-1$
84
				return;
85
			}
86
			item = MenuHelper.createMenuAddition(element);
87
			parentMenu.getChildren().add(idx + 1, item);
88
		} else {
89
			item = (MMenu) parentMenu.getChildren().get(itemIdx);
90
		}
91
		processGroups(item, element);
92
	}
93
94
	private void processGroups(MMenu menu, IConfigurationElement element) {
95
		IConfigurationElement[] children = element.getChildren();
96
		for (IConfigurationElement sepAddition : children) {
97
			String name = sepAddition.getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
98
			if (MenuHelper.indexForId(menu, name) == -1) {
99
				MMenuElement sep = MenuFactoryImpl.eINSTANCE.createMenuSeparator();
100
				sep.setElementId(name);
101
				if (!MenuHelper.isSeparatorVisible(sepAddition)) {
102
					sep.setVisible(false);
103
				}
104
				menu.getChildren().add(sep);
105
			}
106
		}
107
	}
108
109
	private MElementContainer<MMenuElement> findMenuFromPath(MElementContainer<MMenuElement> menu,
110
			Path menuPath, int segment) {
111
		int idx = MenuHelper.indexForId(menu, menuPath.segment(segment));
112
		if (idx == -1) {
113
			if (segment + 1 < menuPath.segmentCount() || !menuPath.hasTrailingSeparator()) {
114
				return null;
115
			}
116
			return menu;
117
		}
118
		MElementContainer<MMenuElement> item = (MElementContainer<MMenuElement>) menu.getChildren()
119
				.get(idx);
120
		if (item.getChildren().size() == 0) {
121
			if (segment + 1 == menuPath.segmentCount()) {
122
				return menu;
123
			} else {
124
				return null;
125
			}
126
		}
127
		return findMenuFromPath(item, menuPath, segment + 1);
128
	}
129
130
	public void dispose() {
131
		application.getMenuContributions().remove(menuContribution);
132
	}
133
}
(-)Eclipse UI/org/eclipse/ui/internal/menus/MenuAdditionCacheEntry.java (-250 / +17 lines)
Lines 12-49 Link Here
12
12
13
package org.eclipse.ui.internal.menus;
13
package org.eclipse.ui.internal.menus;
14
14
15
import java.net.MalformedURLException;
16
import java.net.URL;
17
import java.util.HashMap;
18
import java.util.Map;
15
import java.util.Map;
19
import org.eclipse.core.expressions.Expression;
20
import org.eclipse.core.expressions.ExpressionConverter;
21
import org.eclipse.core.runtime.CoreException;
22
import org.eclipse.core.runtime.FileLocator;
23
import org.eclipse.core.runtime.IConfigurationElement;
16
import org.eclipse.core.runtime.IConfigurationElement;
24
import org.eclipse.core.runtime.InvalidRegistryObjectException;
25
import org.eclipse.e4.core.contexts.IEclipseContext;
17
import org.eclipse.e4.core.contexts.IEclipseContext;
26
import org.eclipse.e4.ui.model.application.MApplication;
18
import org.eclipse.e4.ui.model.application.MApplication;
27
import org.eclipse.e4.ui.model.application.commands.MCommand;
28
import org.eclipse.e4.ui.model.application.commands.MParameter;
19
import org.eclipse.e4.ui.model.application.commands.MParameter;
29
import org.eclipse.e4.ui.model.application.commands.impl.CommandsFactoryImpl;
20
import org.eclipse.e4.ui.model.application.commands.impl.CommandsFactoryImpl;
30
import org.eclipse.e4.ui.model.application.ui.MCoreExpression;
31
import org.eclipse.e4.ui.model.application.ui.MElementContainer;
21
import org.eclipse.e4.ui.model.application.ui.MElementContainer;
32
import org.eclipse.e4.ui.model.application.ui.MExpression;
33
import org.eclipse.e4.ui.model.application.ui.impl.UiFactoryImpl;
34
import org.eclipse.e4.ui.model.application.ui.menu.ItemType;
35
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
22
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
36
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
23
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
37
import org.eclipse.e4.ui.model.application.ui.menu.MMenuContribution;
24
import org.eclipse.e4.ui.model.application.ui.menu.MMenuContribution;
38
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
25
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
39
import org.eclipse.e4.ui.model.application.ui.menu.impl.MenuFactoryImpl;
26
import org.eclipse.e4.ui.model.application.ui.menu.impl.MenuFactoryImpl;
40
import org.eclipse.e4.ui.workbench.swt.modeling.MenuServiceFilter;
27
import org.eclipse.e4.ui.workbench.swt.modeling.MenuServiceFilter;
41
import org.eclipse.jface.resource.ImageDescriptor;
42
import org.eclipse.ui.internal.e4.compatibility.E4Util;
28
import org.eclipse.ui.internal.e4.compatibility.E4Util;
43
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
29
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
44
import org.eclipse.ui.internal.util.Util;
45
import org.eclipse.ui.menus.CommandContributionItem;
46
import org.eclipse.ui.plugin.AbstractUIPlugin;
47
30
48
/**
31
/**
49
 * @since 3.3
32
 * @since 3.3
Lines 116-163 Link Here
116
			return null;
99
			return null;
117
		}
100
		}
118
101
119
		MMenu element = MenuFactoryImpl.eINSTANCE.createMenu();
102
		return MenuHelper.createMenuAddition(menuAddition);
120
		String id = getId(menuAddition);
121
		element.setElementId(id);
122
		String text = getLabel(menuAddition);
123
		String mnemonic = getMnemonic(menuAddition);
124
		if (text != null && mnemonic != null) {
125
			E4Util.unsupported("mnemonic processing in menus: " + id + ": " + text); //$NON-NLS-1$//$NON-NLS-2$
126
			int idx = text.indexOf(mnemonic);
127
			if (idx != -1) {
128
				text = text.substring(0, idx) + '&' + text.substring(idx);
129
			}
130
		}
131
		element.setIconURI(getIconUrl(menuAddition, IWorkbenchRegistryConstants.ATT_ICON));
132
		element.setLabel(Util.safeString(text));
133
134
		return element;
135
	}
103
	}
136
104
137
	private MMenuElement createSeparatorAddition(final IConfigurationElement sepAddition) {
105
	private MMenuElement createSeparatorAddition(final IConfigurationElement sepAddition) {
138
		String name = getName(sepAddition);
106
		String name = MenuHelper.getName(sepAddition);
139
		MMenuElement element = MenuFactoryImpl.eINSTANCE.createMenuSeparator();
107
		MMenuElement element = MenuFactoryImpl.eINSTANCE.createMenuSeparator();
140
		element.setElementId(name);
108
		element.setElementId(name);
141
		if (!isSeparatorVisible(sepAddition)) {
109
		if (!MenuHelper.isSeparatorVisible(sepAddition)) {
142
			element.setVisible(false);
110
			element.setVisible(false);
143
		}
111
		}
144
		return element;
112
		return element;
145
	}
113
	}
146
114
147
	private MCommand findCommand(String id) {
148
		for (MCommand cmd : application.getCommands()) {
149
			if (id.equals(cmd.getElementId())) {
150
				return cmd;
151
			}
152
		}
153
		return null;
154
	}
155
156
	private MMenuElement createCommandAddition(final IConfigurationElement commandAddition) {
115
	private MMenuElement createCommandAddition(final IConfigurationElement commandAddition) {
157
		MHandledMenuItem item = MenuFactoryImpl.eINSTANCE.createHandledMenuItem();
116
		MHandledMenuItem item = MenuFactoryImpl.eINSTANCE.createHandledMenuItem();
158
		item.setElementId(getId(commandAddition));
117
		item.setElementId(MenuHelper.getId(commandAddition));
159
		item.setCommand(findCommand(getCommandId(commandAddition)));
118
		item.setCommand(MenuHelper.getCommandById(application,
160
		Map parms = getParameters(commandAddition);
119
				MenuHelper.getCommandId(commandAddition)));
120
		Map parms = MenuHelper.getParameters(commandAddition);
161
		for (Object obj : parms.entrySet()) {
121
		for (Object obj : parms.entrySet()) {
162
			Map.Entry e = (Map.Entry) obj;
122
			Map.Entry e = (Map.Entry) obj;
163
			MParameter parm = CommandsFactoryImpl.eINSTANCE.createParameter();
123
			MParameter parm = CommandsFactoryImpl.eINSTANCE.createParameter();
Lines 165-381 Link Here
165
			parm.setValue(e.getValue().toString());
125
			parm.setValue(e.getValue().toString());
166
			item.getParameters().add(parm);
126
			item.getParameters().add(parm);
167
		}
127
		}
168
		item.setIconURI(getIconUrl(commandAddition, IWorkbenchRegistryConstants.ATT_ICON));
128
		item.setIconURI(MenuHelper
169
		item.setLabel(getLabel(commandAddition));
129
				.getIconUrl(commandAddition, IWorkbenchRegistryConstants.ATT_ICON));
170
		item.setMnemonics(getMnemonic(commandAddition));
130
		item.setLabel(MenuHelper.getLabel(commandAddition));
171
		item.setTooltip(getTooltip(commandAddition));
131
		item.setMnemonics(MenuHelper.getMnemonic(commandAddition));
172
		item.setType(getStyle(commandAddition));
132
		item.setTooltip(MenuHelper.getTooltip(commandAddition));
173
		item.setVisibleWhen(getVisibleWhen(commandAddition));
133
		item.setType(MenuHelper.getStyle(commandAddition));
134
		item.setVisibleWhen(MenuHelper.getVisibleWhen(commandAddition));
174
		return item;
135
		return item;
175
	}
136
	}
176
137
177
	/**
178
	 * @param commandAddition
179
	 * @return
180
	 */
181
	private MExpression getVisibleWhen(IConfigurationElement commandAddition) {
182
		try {
183
			IConfigurationElement[] visibleConfig = configElement
184
					.getChildren(IWorkbenchRegistryConstants.TAG_VISIBLE_WHEN);
185
			if (visibleConfig.length > 0 && visibleConfig.length < 2) {
186
				IConfigurationElement[] visibleChild = visibleConfig[0].getChildren();
187
				if (visibleChild.length > 0) {
188
					Expression visWhen = ExpressionConverter.getDefault().perform(visibleChild[0]);
189
					MCoreExpression exp = UiFactoryImpl.eINSTANCE.createCoreExpression();
190
					exp.setCoreExpressionId("programmatic.value"); //$NON-NLS-1$
191
					exp.setCoreExpression(visWhen);
192
					return exp;
193
					// visWhenMap.put(configElement, visWhen);
194
				}
195
			}
196
		} catch (InvalidRegistryObjectException e) {
197
			// visWhenMap.put(configElement, null);
198
			// TODO Auto-generated catch block
199
			e.printStackTrace();
200
		} catch (CoreException e) {
201
			// visWhenMap.put(configElement, null);
202
			// TODO Auto-generated catch block
203
			e.printStackTrace();
204
		}
205
		return null;
206
	}
207
208
	/**
209
	 * @param element
210
	 *            the configuration element
211
	 * @return <code>true</code> if the checkEnabled is <code>true</code>.
212
	 */
213
	static boolean getVisibleEnabled(IConfigurationElement element) {
214
		IConfigurationElement[] children = element
215
				.getChildren(IWorkbenchRegistryConstants.TAG_VISIBLE_WHEN);
216
		String checkEnabled = null;
217
218
		if (children.length > 0) {
219
			checkEnabled = children[0].getAttribute(IWorkbenchRegistryConstants.ATT_CHECK_ENABLED);
220
		}
221
222
		return checkEnabled != null && checkEnabled.equalsIgnoreCase("true"); //$NON-NLS-1$
223
	}
224
225
	/*
226
	 * Support Utilities
227
	 */
228
	public static String getId(IConfigurationElement element) {
229
		String id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
230
231
		// For sub-menu management -all- items must be id'd so enforce this
232
		// here (we could optimize by checking the 'name' of the config
233
		// element == "menu"
234
		if (id == null || id.length() == 0) {
235
			id = getCommandId(element);
236
		}
237
		if (id == null || id.length() == 0) {
238
			id = element.toString();
239
		}
240
241
		return id;
242
	}
243
244
	static String getName(IConfigurationElement element) {
245
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
246
	}
247
248
	static int getMode(IConfigurationElement element) {
249
		if ("FORCE_TEXT".equals(element.getAttribute(IWorkbenchRegistryConstants.ATT_MODE))) { //$NON-NLS-1$
250
			return CommandContributionItem.MODE_FORCE_TEXT;
251
		}
252
		return 0;
253
	}
254
255
	static String getLabel(IConfigurationElement element) {
256
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL);
257
	}
258
259
	static String getMnemonic(IConfigurationElement element) {
260
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_MNEMONIC);
261
	}
262
263
	static String getTooltip(IConfigurationElement element) {
264
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_TOOLTIP);
265
	}
266
267
	static String getIconPath(IConfigurationElement element) {
268
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
269
	}
270
271
	static String getDisabledIconPath(IConfigurationElement element) {
272
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_DISABLEDICON);
273
	}
274
275
	static String getHoverIconPath(IConfigurationElement element) {
276
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_HOVERICON);
277
	}
278
279
	static String getIconUrl(IConfigurationElement element, String attr) {
280
		String extendingPluginId = element.getDeclaringExtension().getContributor().getName();
281
282
		String iconPath = element.getAttribute(attr);
283
		if (iconPath == null) {
284
			return null;
285
		}
286
		if (!iconPath.startsWith("platform:")) { //$NON-NLS-1$
287
			iconPath = "platform:/plugin/" + extendingPluginId + "/" + iconPath; //$NON-NLS-1$//$NON-NLS-2$
288
		}
289
		URL url = null;
290
		try {
291
			url = FileLocator.find(new URL(iconPath));
292
		} catch (MalformedURLException e) {
293
			// TODO Auto-generated catch block
294
			e.printStackTrace();
295
		}
296
		return url == null ? null : url.toString();
297
	}
298
299
	static ImageDescriptor getDisabledIconDescriptor(IConfigurationElement element) {
300
		String extendingPluginId = element.getDeclaringExtension().getContributor().getName();
301
302
		String iconPath = getDisabledIconPath(element);
303
		if (iconPath != null) {
304
			return AbstractUIPlugin.imageDescriptorFromPlugin(extendingPluginId, iconPath);
305
		}
306
		return null;
307
	}
308
309
	static ImageDescriptor getHoverIconDescriptor(IConfigurationElement element) {
310
		String extendingPluginId = element.getDeclaringExtension().getContributor().getName();
311
312
		String iconPath = getHoverIconPath(element);
313
		if (iconPath != null) {
314
			return AbstractUIPlugin.imageDescriptorFromPlugin(extendingPluginId, iconPath);
315
		}
316
		return null;
317
	}
318
319
	static String getHelpContextId(IConfigurationElement element) {
320
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_HELP_CONTEXT_ID);
321
	}
322
323
	public static boolean isSeparatorVisible(IConfigurationElement element) {
324
		String val = element.getAttribute(IWorkbenchRegistryConstants.ATT_VISIBLE);
325
		return Boolean.valueOf(val).booleanValue();
326
	}
327
328
	public static String getClassSpec(IConfigurationElement element) {
329
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_CLASS);
330
	}
331
332
	public static String getCommandId(IConfigurationElement element) {
333
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_COMMAND_ID);
334
	}
335
336
	private ItemType getStyle(IConfigurationElement element) {
337
		String style = element.getAttribute(IWorkbenchRegistryConstants.ATT_STYLE);
338
		if (style == null || style.length() == 0) {
339
			return ItemType.PUSH;
340
		}
341
		if (IWorkbenchRegistryConstants.STYLE_TOGGLE.equals(style)) {
342
			return ItemType.CHECK;
343
		}
344
		if (IWorkbenchRegistryConstants.STYLE_RADIO.equals(style)) {
345
			return ItemType.RADIO;
346
		}
347
		if (IWorkbenchRegistryConstants.STYLE_PULLDOWN.equals(style)) {
348
			E4Util.unsupported("Failed to get style for " + IWorkbenchRegistryConstants.STYLE_PULLDOWN); //$NON-NLS-1$
349
			// return CommandContributionItem.STYLE_PULLDOWN;
350
		}
351
		return ItemType.PUSH;
352
	}
353
354
	/**
355
	 * @param element
356
	 * @return A map of parameters names to parameter values. All Strings. The
357
	 *         map may be empty.
358
	 */
359
	public static Map getParameters(IConfigurationElement element) {
360
		HashMap map = new HashMap();
361
		IConfigurationElement[] parameters = element
362
				.getChildren(IWorkbenchRegistryConstants.TAG_PARAMETER);
363
		for (int i = 0; i < parameters.length; i++) {
364
			String name = parameters[i].getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
365
			String value = parameters[i].getAttribute(IWorkbenchRegistryConstants.ATT_VALUE);
366
			if (name != null && value != null) {
367
				map.put(name, value);
368
			}
369
		}
370
		return map;
371
	}
372
138
373
	private void addChildren(final MElementContainer<MMenuElement> container,
139
	private void addChildren(final MElementContainer<MMenuElement> container,
374
			IConfigurationElement parent, String filter) {
140
			IConfigurationElement parent, String filter) {
375
		IConfigurationElement[] items = parent.getChildren();
141
		IConfigurationElement[] items = parent.getChildren();
376
		for (int i = 0; i < items.length; i++) {
142
		for (int i = 0; i < items.length; i++) {
377
			String itemType = items[i].getName();
143
			String itemType = items[i].getName();
378
			String id = getId(items[i]);
144
			String id = MenuHelper.getId(items[i]);
379
145
380
			if (IWorkbenchRegistryConstants.TAG_COMMAND.equals(itemType)) {
146
			if (IWorkbenchRegistryConstants.TAG_COMMAND.equals(itemType)) {
381
				MMenuElement element = createCommandAddition(items[i]);
147
				MMenuElement element = createCommandAddition(items[i]);
Lines 417-423 Link Here
417
			return;
183
			return;
418
		}
184
		}
419
		menuContribution = MenuFactoryImpl.eINSTANCE.createMenuContribution();
185
		menuContribution = MenuFactoryImpl.eINSTANCE.createMenuContribution();
420
		String idContrib = getId(configElement);
186
		String idContrib = MenuHelper.getId(configElement);
421
		if (idContrib != null && idContrib.length() > 0) {
187
		if (idContrib != null && idContrib.length() > 0) {
422
			menuContribution.setElementId(idContrib);
188
			menuContribution.setElementId(idContrib);
423
		}
189
		}
Lines 437-442 Link Here
437
			filter = MenuServiceFilter.MC_POPUP;
203
			filter = MenuServiceFilter.MC_POPUP;
438
		}
204
		}
439
		menuContribution.getTags().add(filter);
205
		menuContribution.getTags().add(filter);
206
		menuContribution.setVisibleWhen(MenuHelper.getVisibleWhen(configElement));
440
		addChildren(menuContribution, configElement, filter);
207
		addChildren(menuContribution, configElement, filter);
441
		application.getMenuContributions().add(menuContribution);
208
		application.getMenuContributions().add(menuContribution);
442
	}
209
	}
(-)Eclipse (+318 lines)
Added Link Here
1
package org.eclipse.ui.internal.menus;
2
3
import java.lang.reflect.Field;
4
import java.net.MalformedURLException;
5
import java.net.URL;
6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9
import org.eclipse.core.expressions.Expression;
10
import org.eclipse.core.expressions.ExpressionConverter;
11
import org.eclipse.core.runtime.CoreException;
12
import org.eclipse.core.runtime.FileLocator;
13
import org.eclipse.core.runtime.IConfigurationElement;
14
import org.eclipse.core.runtime.InvalidRegistryObjectException;
15
import org.eclipse.e4.ui.model.application.MApplication;
16
import org.eclipse.e4.ui.model.application.commands.MCommand;
17
import org.eclipse.e4.ui.model.application.ui.MCoreExpression;
18
import org.eclipse.e4.ui.model.application.ui.MElementContainer;
19
import org.eclipse.e4.ui.model.application.ui.MExpression;
20
import org.eclipse.e4.ui.model.application.ui.impl.UiFactoryImpl;
21
import org.eclipse.e4.ui.model.application.ui.menu.ItemType;
22
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
23
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
24
import org.eclipse.e4.ui.model.application.ui.menu.impl.MenuFactoryImpl;
25
import org.eclipse.jface.resource.ImageDescriptor;
26
import org.eclipse.ui.internal.e4.compatibility.E4Util;
27
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
28
import org.eclipse.ui.internal.util.Util;
29
import org.eclipse.ui.menus.CommandContributionItem;
30
31
public class MenuHelper {
32
33
	public static final String ACTION_SET_CMD_PREFIX = "AS::"; //$NON-NLS-1$
34
	public static final String MAIN_MENU_ID = "org.eclipse.ui.main.menu"; //$NON-NLS-1$
35
	private static Field urlField;
36
37
38
	public static int indexForId(MElementContainer<MMenuElement> parentMenu, String id) {
39
		if (id == null || id.length() == 0) {
40
			return -1;
41
		}
42
		int i = 0;
43
		for (MMenuElement item : parentMenu.getChildren()) {
44
			if (id.equals(item.getElementId())) {
45
				return i;
46
			}
47
			i++;
48
		}
49
		return -1;
50
	}
51
52
	public static String getActionSetCommandId(IConfigurationElement element) {
53
		String id = MenuHelper.getDefinitionId(element);
54
		if (id != null) {
55
			return id;
56
		}
57
		id = MenuHelper.getId(element);
58
		String actionSetId = null;
59
		Object obj = element.getParent();
60
		while (obj instanceof IConfigurationElement && actionSetId == null) {
61
			IConfigurationElement parent = (IConfigurationElement) obj;
62
			if (parent.getName().equals(
63
					IWorkbenchRegistryConstants.TAG_ACTION_SET)) {
64
				actionSetId = MenuHelper.getId(parent);
65
			}
66
			obj = parent.getParent();
67
		}
68
		return ACTION_SET_CMD_PREFIX + actionSetId + '/' + id;
69
	}
70
71
	/**
72
	 * @param imageDescriptor
73
	 * @return
74
	 */
75
	public static String getImageUrl(ImageDescriptor imageDescriptor) {
76
		if (imageDescriptor == null)
77
			return null;
78
		Class idc = imageDescriptor.getClass();
79
		if (idc.getName().endsWith("URLImageDescriptor")) { //$NON-NLS-1$
80
			URL url = getUrl(idc, imageDescriptor);
81
			return url.toExternalForm();
82
		}
83
		return null;
84
	}
85
86
	private static URL getUrl(Class idc, ImageDescriptor imageDescriptor) {
87
		try {
88
			if (urlField == null) {
89
				urlField = idc.getDeclaredField("url"); //$NON-NLS-1$
90
				urlField.setAccessible(true);
91
			}
92
			return (URL) urlField.get(imageDescriptor);
93
		} catch (SecurityException e) {
94
			// TODO Auto-generated catch block
95
			e.printStackTrace();
96
		} catch (NoSuchFieldException e) {
97
			// TODO Auto-generated catch block
98
			e.printStackTrace();
99
		} catch (IllegalArgumentException e) {
100
			// TODO Auto-generated catch block
101
			e.printStackTrace();
102
		} catch (IllegalAccessException e) {
103
			// TODO Auto-generated catch block
104
			e.printStackTrace();
105
		}
106
		return null;
107
	}
108
109
	static MCommand getCommandById(MApplication app, String cmdId) {
110
		final List<MCommand> cmds = app.getCommands();
111
		for (MCommand cmd : cmds) {
112
			if (cmdId.equals(cmd.getElementId())) {
113
				return cmd;
114
			}
115
		}
116
		return null;
117
	}
118
119
	/**
120
	 * @param element
121
	 *            the configuration element
122
	 * @return <code>true</code> if the checkEnabled is <code>true</code>.
123
	 */
124
	static boolean getVisibleEnabled(IConfigurationElement element) {
125
		IConfigurationElement[] children = element
126
				.getChildren(IWorkbenchRegistryConstants.TAG_VISIBLE_WHEN);
127
		String checkEnabled = null;
128
129
		if (children.length > 0) {
130
			checkEnabled = children[0].getAttribute(IWorkbenchRegistryConstants.ATT_CHECK_ENABLED);
131
		}
132
133
		return checkEnabled != null && checkEnabled.equalsIgnoreCase("true"); //$NON-NLS-1$
134
	}
135
136
	static MExpression getVisibleWhen(IConfigurationElement commandAddition) {
137
		try {
138
			IConfigurationElement[] visibleConfig = commandAddition
139
					.getChildren(IWorkbenchRegistryConstants.TAG_VISIBLE_WHEN);
140
			if (visibleConfig.length > 0 && visibleConfig.length < 2) {
141
				IConfigurationElement[] visibleChild = visibleConfig[0].getChildren();
142
				if (visibleChild.length > 0) {
143
					Expression visWhen = ExpressionConverter.getDefault().perform(visibleChild[0]);
144
					MCoreExpression exp = UiFactoryImpl.eINSTANCE.createCoreExpression();
145
					exp.setCoreExpressionId("programmatic.value"); //$NON-NLS-1$
146
					exp.setCoreExpression(visWhen);
147
					return exp;
148
					// visWhenMap.put(configElement, visWhen);
149
				}
150
			}
151
		} catch (InvalidRegistryObjectException e) {
152
			// visWhenMap.put(configElement, null);
153
			// TODO Auto-generated catch block
154
			e.printStackTrace();
155
		} catch (CoreException e) {
156
			// visWhenMap.put(configElement, null);
157
			// TODO Auto-generated catch block
158
			e.printStackTrace();
159
		}
160
		return null;
161
	}
162
163
	/*
164
	 * Support Utilities
165
	 */
166
	public static String getId(IConfigurationElement element) {
167
		String id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
168
169
		// For sub-menu management -all- items must be id'd so enforce this
170
		// here (we could optimize by checking the 'name' of the config
171
		// element == "menu"
172
		if (id == null || id.length() == 0) {
173
			id = getCommandId(element);
174
		}
175
		if (id == null || id.length() == 0) {
176
			id = element.toString();
177
		}
178
179
		return id;
180
	}
181
182
	static String getName(IConfigurationElement element) {
183
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
184
	}
185
186
	static int getMode(IConfigurationElement element) {
187
		if ("FORCE_TEXT".equals(element.getAttribute(IWorkbenchRegistryConstants.ATT_MODE))) { //$NON-NLS-1$
188
			return CommandContributionItem.MODE_FORCE_TEXT;
189
		}
190
		return 0;
191
	}
192
193
	static String getLabel(IConfigurationElement element) {
194
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL);
195
	}
196
197
	static String getPath(IConfigurationElement element) {
198
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_PATH);
199
	}
200
201
	static String getMnemonic(IConfigurationElement element) {
202
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_MNEMONIC);
203
	}
204
205
	static String getTooltip(IConfigurationElement element) {
206
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_TOOLTIP);
207
	}
208
209
	static String getIconPath(IConfigurationElement element) {
210
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
211
	}
212
213
	static String getDisabledIconPath(IConfigurationElement element) {
214
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_DISABLEDICON);
215
	}
216
217
	static String getHoverIconPath(IConfigurationElement element) {
218
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_HOVERICON);
219
	}
220
221
	static String getIconUrl(IConfigurationElement element, String attr) {
222
		String extendingPluginId = element.getDeclaringExtension().getContributor().getName();
223
224
		String iconPath = element.getAttribute(attr);
225
		if (iconPath == null) {
226
			return null;
227
		}
228
		if (!iconPath.startsWith("platform:")) { //$NON-NLS-1$
229
			iconPath = "platform:/plugin/" + extendingPluginId + "/" + iconPath; //$NON-NLS-1$//$NON-NLS-2$
230
		}
231
		URL url = null;
232
		try {
233
			url = FileLocator.find(new URL(iconPath));
234
		} catch (MalformedURLException e) {
235
			// TODO Auto-generated catch block
236
			e.printStackTrace();
237
		}
238
		return url == null ? null : url.toString();
239
	}
240
241
	static String getHelpContextId(IConfigurationElement element) {
242
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_HELP_CONTEXT_ID);
243
	}
244
245
	public static boolean isSeparatorVisible(IConfigurationElement element) {
246
		String val = element.getAttribute(IWorkbenchRegistryConstants.ATT_VISIBLE);
247
		return Boolean.valueOf(val).booleanValue();
248
	}
249
250
	public static String getClassSpec(IConfigurationElement element) {
251
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_CLASS);
252
	}
253
254
	public static String getCommandId(IConfigurationElement element) {
255
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_COMMAND_ID);
256
	}
257
258
	public static ItemType getStyle(IConfigurationElement element) {
259
		String style = element.getAttribute(IWorkbenchRegistryConstants.ATT_STYLE);
260
		if (style == null || style.length() == 0) {
261
			return ItemType.PUSH;
262
		}
263
		if (IWorkbenchRegistryConstants.STYLE_TOGGLE.equals(style)) {
264
			return ItemType.CHECK;
265
		}
266
		if (IWorkbenchRegistryConstants.STYLE_RADIO.equals(style)) {
267
			return ItemType.RADIO;
268
		}
269
		if (IWorkbenchRegistryConstants.STYLE_PULLDOWN.equals(style)) {
270
			E4Util.unsupported("Failed to get style for " + IWorkbenchRegistryConstants.STYLE_PULLDOWN); //$NON-NLS-1$
271
			// return CommandContributionItem.STYLE_PULLDOWN;
272
		}
273
		return ItemType.PUSH;
274
	}
275
276
	public static boolean getRetarget(IConfigurationElement element) {
277
		String r = element.getAttribute(IWorkbenchRegistryConstants.ATT_RETARGET);
278
		return Boolean.valueOf(r);
279
	}
280
281
	public static String getDefinitionId(IConfigurationElement element) {
282
		return element.getAttribute(IWorkbenchRegistryConstants.ATT_DEFINITION_ID);
283
	}
284
285
	public static Map<String, String> getParameters(IConfigurationElement element) {
286
		HashMap<String, String> map = new HashMap<String, String>();
287
		IConfigurationElement[] parameters = element
288
				.getChildren(IWorkbenchRegistryConstants.TAG_PARAMETER);
289
		for (int i = 0; i < parameters.length; i++) {
290
			String name = parameters[i].getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
291
			String value = parameters[i].getAttribute(IWorkbenchRegistryConstants.ATT_VALUE);
292
			if (name != null && value != null) {
293
				map.put(name, value);
294
			}
295
		}
296
		return map;
297
	}
298
299
	public static MMenu createMenuAddition(IConfigurationElement menuAddition) {
300
		MMenu element = MenuFactoryImpl.eINSTANCE.createMenu();
301
		String id = MenuHelper.getId(menuAddition);
302
		element.setElementId(id);
303
		String text = MenuHelper.getLabel(menuAddition);
304
		String mnemonic = MenuHelper.getMnemonic(menuAddition);
305
		if (text != null && mnemonic != null) {
306
			E4Util.unsupported("mnemonic processing in menus: " + id + ": " + text); //$NON-NLS-1$//$NON-NLS-2$
307
			int idx = text.indexOf(mnemonic);
308
			if (idx != -1) {
309
				text = text.substring(0, idx) + '&' + text.substring(idx);
310
			}
311
		}
312
		element.setIconURI(MenuHelper
313
				.getIconUrl(menuAddition, IWorkbenchRegistryConstants.ATT_ICON));
314
		element.setLabel(Util.safeString(text));
315
316
		return element;
317
	}
318
}
(-)Eclipse UI/org/eclipse/ui/internal/menus/MenuPersistence.java (-16 / +40 lines)
Lines 12-17 Link Here
12
package org.eclipse.ui.internal.menus;
12
package org.eclipse.ui.internal.menus;
13
13
14
import java.util.ArrayList;
14
import java.util.ArrayList;
15
import java.util.Arrays;
15
import java.util.Collections;
16
import java.util.Collections;
16
import java.util.Comparator;
17
import java.util.Comparator;
17
import java.util.Iterator;
18
import java.util.Iterator;
Lines 43-48 Link Here
43
	private MApplication application;
44
	private MApplication application;
44
	private IEclipseContext appContext;
45
	private IEclipseContext appContext;
45
	private ArrayList<MenuAdditionCacheEntry> contributions = new ArrayList<MenuAdditionCacheEntry>();
46
	private ArrayList<MenuAdditionCacheEntry> contributions = new ArrayList<MenuAdditionCacheEntry>();
47
	private ArrayList<ActionSet> actionContributions = new ArrayList<ActionSet>();
46
48
47
	/**
49
	/**
48
	 * Constructs a new instance of {@link MenuPersistence}.
50
	 * Constructs a new instance of {@link MenuPersistence}.
Lines 63-68 Link Here
63
			mc.dispose();
65
			mc.dispose();
64
		}
66
		}
65
		contributions.clear();
67
		contributions.clear();
68
		for (ActionSet as : actionContributions) {
69
			as.dispose();
70
		}
71
		actionContributions.clear();
66
		super.dispose();
72
		super.dispose();
67
	}
73
	}
68
74
Lines 101-106 Link Here
101
107
102
		// read the 3.3 menu additions
108
		// read the 3.3 menu additions
103
		readAdditions();
109
		readAdditions();
110
111
		// convert actionSets to MenuContributions
112
		readActionSets();
104
	}
113
	}
105
114
106
	//
115
	//
Lines 167-173 Link Here
167
176
168
	public void readAdditions() {
177
	public void readAdditions() {
169
		final IExtensionRegistry registry = Platform.getExtensionRegistry();
178
		final IExtensionRegistry registry = Platform.getExtensionRegistry();
170
		ArrayList configElements = new ArrayList();
179
		ArrayList<IConfigurationElement> configElements = new ArrayList<IConfigurationElement>();
171
180
172
		final IConfigurationElement[] menusExtensionPoint = registry
181
		final IConfigurationElement[] menusExtensionPoint = registry
173
				.getConfigurationElementsFor(EXTENSION_MENUS);
182
				.getConfigurationElementsFor(EXTENSION_MENUS);
Lines 178-199 Link Here
178
				configElements.add(menusExtensionPoint[i]);
187
				configElements.add(menusExtensionPoint[i]);
179
			}
188
			}
180
		}
189
		}
181
		Comparator comparer = new Comparator() {
190
		Comparator<IConfigurationElement> comparer = new Comparator<IConfigurationElement>() {
182
			public int compare(Object o1, Object o2) {
191
			public int compare(IConfigurationElement c1, IConfigurationElement c2) {
183
				IConfigurationElement c1 = (IConfigurationElement) o1;
192
				return c1.getNamespaceIdentifier().compareToIgnoreCase(c2.getNamespaceIdentifier());
184
				IConfigurationElement c2 = (IConfigurationElement) o2;
185
				return c1.getNamespaceIdentifier().compareToIgnoreCase(
186
						c2.getNamespaceIdentifier());
187
			}
193
			}
188
		};
194
		};
189
		Collections.sort(configElements, comparer);
195
		Collections.sort(configElements, comparer);
190
196
191
		Iterator i = configElements.iterator();
197
		Iterator<IConfigurationElement> i = configElements.iterator();
192
		while (i.hasNext()) {
198
		while (i.hasNext()) {
193
			final IConfigurationElement configElement = (IConfigurationElement) i
199
			final IConfigurationElement configElement = i.next();
194
					.next();
200
195
			
196
			
197
			if (isProgramaticContribution(configElement)) {
201
			if (isProgramaticContribution(configElement)) {
198
				// newFactory = new ProxyMenuAdditionCacheEntry(
202
				// newFactory = new ProxyMenuAdditionCacheEntry(
199
				// configElement
203
				// configElement
Lines 202-211 Link Here
202
				E4Util.unsupported("Programmatic Contribution Factories not supported"); //$NON-NLS-1$
206
				E4Util.unsupported("Programmatic Contribution Factories not supported"); //$NON-NLS-1$
203
207
204
			} else {
208
			} else {
205
				MenuAdditionCacheEntry menuContribution = new MenuAdditionCacheEntry(application, appContext,
209
				MenuAdditionCacheEntry menuContribution = new MenuAdditionCacheEntry(application,
206
						configElement,
210
						appContext, configElement,
207
						configElement
211
						configElement.getAttribute(IWorkbenchRegistryConstants.TAG_LOCATION_URI),
208
								.getAttribute(IWorkbenchRegistryConstants.TAG_LOCATION_URI),
209
						configElement.getNamespaceIdentifier());
212
						configElement.getNamespaceIdentifier());
210
				contributions.add(menuContribution);
213
				contributions.add(menuContribution);
211
				menuContribution.addToModel();
214
				menuContribution.addToModel();
Lines 223-226 Link Here
223
	private boolean isProgramaticContribution(IConfigurationElement menuAddition) {
226
	private boolean isProgramaticContribution(IConfigurationElement menuAddition) {
224
		return menuAddition.getAttribute(IWorkbenchRegistryConstants.ATT_CLASS) != null;
227
		return menuAddition.getAttribute(IWorkbenchRegistryConstants.ATT_CLASS) != null;
225
	}
228
	}
229
230
	private void readActionSets() {
231
		final IExtensionRegistry registry = Platform.getExtensionRegistry();
232
		ArrayList<IConfigurationElement> configElements = new ArrayList<IConfigurationElement>();
233
234
		configElements.addAll(Arrays.asList(registry
235
				.getConfigurationElementsFor(IWorkbenchRegistryConstants.EXTENSION_ACTION_SETS)));
236
237
		Comparator<IConfigurationElement> comparer = new Comparator<IConfigurationElement>() {
238
			public int compare(IConfigurationElement c1, IConfigurationElement c2) {
239
				return c1.getNamespaceIdentifier().compareToIgnoreCase(c2.getNamespaceIdentifier());
240
			}
241
		};
242
		Collections.sort(configElements, comparer);
243
244
		for (IConfigurationElement element : configElements) {
245
			ActionSet actionSet = new ActionSet(application, appContext, element);
246
			actionContributions.add(actionSet);
247
			actionSet.addToModel();
248
		}
249
	}
226
}
250
}

Return to bug 316303