KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > actions > QuickMenuAction


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.ui.internal.ide.actions;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.action.IMenuManager;
15 import org.eclipse.jface.bindings.TriggerSequence;
16 import org.eclipse.ui.IWorkbench;
17 import org.eclipse.ui.PlatformUI;
18 import org.eclipse.ui.actions.QuickMenuCreator;
19 import org.eclipse.ui.keys.IBindingService;
20
21 /**
22  * A quick menu actions provides support to assign short cuts
23  * to sub menus.
24  *
25  * @since 3.0
26  */

27 public abstract class QuickMenuAction extends Action {
28
29     private QuickMenuCreator creator = new QuickMenuCreator() {
30         protected void fillMenu(IMenuManager menu) {
31             QuickMenuAction.this.fillMenu(menu);
32         }
33     };
34
35     /**
36      * Creates a new quick menu action with the given command id.
37      *
38      * @param commandId the command id of the short cut used to open
39      * the sub menu
40      */

41     public QuickMenuAction(String JavaDoc commandId) {
42         setId(commandId);
43         setActionDefinitionId(commandId);
44     }
45
46     /**
47      * {@inheritDoc}
48      */

49     public void run() {
50         creator.createMenu();
51     }
52     
53     /**
54      * Dispose of this menu action.
55      */

56     public void dispose() {
57         if (creator != null) {
58             creator.dispose();
59             creator = null;
60         }
61     }
62
63     /**
64      * Hook to fill a menu manager with the items of the sub menu.
65      *
66      * @param menu the sub menu to fill
67      */

68     protected abstract void fillMenu(IMenuManager menu);
69     
70     /**
71      * Returns the short cut assigned to the sub menu or <code>null</code> if
72      * no short cut is assigned.
73      *
74      * @return the short cut as a human readable string or <code>null</code>
75      */

76     public String JavaDoc getShortCutString() {
77         final IWorkbench workbench = PlatformUI.getWorkbench();
78         final IBindingService bindingService = (IBindingService) workbench
79                 .getAdapter(IBindingService.class);
80         final TriggerSequence[] activeBindings = bindingService
81                 .getActiveBindingsFor(getActionDefinitionId());
82         if (activeBindings.length > 0) {
83             return activeBindings[0].format();
84         }
85
86         return null;
87     }
88 }
89
Popular Tags