KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > actions > CheatSheetMenuAction


1 /*******************************************************************************
2  * Copyright (c) 2002, 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.cheatsheets.actions;
12
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.swt.events.*;
16 import org.eclipse.swt.widgets.*;
17 import org.eclipse.ui.*;
18 import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
19
20 /**
21  * This is the action used to contribute the CheatSheets menu to the workbench's
22  * help menu.
23  */

24 public class CheatSheetMenuAction implements IWorkbenchWindowPulldownDelegate2, IPropertyListener {
25     /**
26      * The menu created by this action
27      */

28     private Menu fMenu;
29
30     /**
31      * Indicates whether the cheat sheet history has changed and
32      * the sub menu needs to be recreated.
33      */

34     protected boolean fRecreateMenu = false;
35
36     /**
37      * The constructor.
38      */

39     public CheatSheetMenuAction() {
40         CheatSheetPlugin.getPlugin().getCheatSheetHistory().addListener(this);
41     }
42
43     /* (non-Javadoc)
44      * @see IWorkbenchWindowActionDelegate#dispose
45      */

46     public void dispose() {
47         setMenu(null);
48         CheatSheetPlugin.getPlugin().getCheatSheetHistory().removeListener(this);
49     }
50
51     /**
52      * Fills the drop-down menu with cheat sheets history
53      *
54      * @param menu the menu to fill
55      */

56     protected void fillMenu(Menu menu) {
57         CheatSheetMenu cheatsheetMenuMenuItem = new CheatSheetMenu();
58         cheatsheetMenuMenuItem.fill(menu, 0);
59     }
60
61     /* (non-Javadoc)
62      * @see org.eclipse.ui.IWorkbenchWindowPulldownDelegate#getMenu(org.eclipse.swt.widgets.Control)
63      */

64     public Menu getMenu(Control parent) {
65         return null;
66     }
67
68     /* (non-Javadoc)
69      * @see org.eclipse.ui.IWorkbenchWindowPulldownDelegate2#getMenu(org.eclipse.swt.widgets.Menu)
70      */

71     public Menu getMenu(Menu parent) {
72         setMenu(new Menu(parent));
73         fillMenu(fMenu);
74         initMenu();
75         return fMenu;
76     }
77
78     /* (non-Javadoc)
79      * @see IWorkbenchWindowActionDelegate#init
80      */

81     public void init(IWorkbenchWindow window) {
82     }
83
84     /**
85      * Creates the menu for the action
86      */

87     private void initMenu() {
88         // Add listener to repopulate the menu each time
89
// it is shown because of dynamic history list
90
fMenu.addMenuListener(new MenuAdapter() {
91             public void menuShown(MenuEvent e) {
92                 if (fRecreateMenu) {
93                     Menu m = (Menu)e.widget;
94                     MenuItem[] items = m.getItems();
95                     for (int i=0; i < items.length; i++) {
96                         items[i].dispose();
97                     }
98                     fillMenu(m);
99                     fRecreateMenu= false;
100                 }
101             }
102         });
103     }
104     
105     /* (non-Javadoc)
106      * @see org.eclipse.ui.IPropertyListener#propertyChanged(java.lang.Object, int)
107      */

108     public void propertyChanged(Object JavaDoc source, int propId) {
109         fRecreateMenu = true;
110     }
111
112     /* (non-Javadoc)
113      * @see IWorkbenchWindowActionDelegate#run
114      */

115     public void run(IAction action) {
116     }
117
118     /* (non-Javadoc)
119      * @see IWorkbenchWindowActionDelegate#selectionChanged
120      */

121     public void selectionChanged(IAction action, ISelection selection) {
122     }
123
124     /**
125      * Sets this action's drop-down menu, disposing the previous menu.
126      *
127      * @param menu the new menu
128      */

129     private void setMenu(Menu menu) {
130         if (fMenu != null) {
131             fMenu.dispose();
132         }
133         fMenu = menu;
134     }
135 }
136
Popular Tags