01: /*******************************************************************************
02: * Copyright (c) 2004, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.ide.actions;
11:
12: import org.eclipse.jface.action.Action;
13: import org.eclipse.jface.action.IMenuManager;
14: import org.eclipse.jface.bindings.TriggerSequence;
15: import org.eclipse.ui.IWorkbench;
16: import org.eclipse.ui.PlatformUI;
17: import org.eclipse.ui.actions.QuickMenuCreator;
18: import org.eclipse.ui.keys.IBindingService;
19:
20: /**
21: * A quick menu actions provides support to assign short cuts
22: * to sub menus.
23: *
24: * @since 3.0
25: */
26: public abstract class QuickMenuAction extends Action {
27:
28: private QuickMenuCreator creator = new QuickMenuCreator() {
29: protected void fillMenu(IMenuManager menu) {
30: QuickMenuAction.this .fillMenu(menu);
31: }
32: };
33:
34: /**
35: * Creates a new quick menu action with the given command id.
36: *
37: * @param commandId the command id of the short cut used to open
38: * the sub menu
39: */
40: public QuickMenuAction(String commandId) {
41: setId(commandId);
42: setActionDefinitionId(commandId);
43: }
44:
45: /**
46: * {@inheritDoc}
47: */
48: public void run() {
49: creator.createMenu();
50: }
51:
52: /**
53: * Dispose of this menu action.
54: */
55: public void dispose() {
56: if (creator != null) {
57: creator.dispose();
58: creator = null;
59: }
60: }
61:
62: /**
63: * Hook to fill a menu manager with the items of the sub menu.
64: *
65: * @param menu the sub menu to fill
66: */
67: protected abstract void fillMenu(IMenuManager menu);
68:
69: /**
70: * Returns the short cut assigned to the sub menu or <code>null</code> if
71: * no short cut is assigned.
72: *
73: * @return the short cut as a human readable string or <code>null</code>
74: */
75: public String getShortCutString() {
76: final IWorkbench workbench = PlatformUI.getWorkbench();
77: final IBindingService bindingService = (IBindingService) workbench
78: .getAdapter(IBindingService.class);
79: final TriggerSequence[] activeBindings = bindingService
80: .getActiveBindingsFor(getActionDefinitionId());
81: if (activeBindings.length > 0) {
82: return activeBindings[0].format();
83: }
84:
85: return null;
86: }
87: }
|