001: /*******************************************************************************
002: * Copyright (c) 2002, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets.actions;
011:
012: import org.eclipse.jface.action.IAction;
013: import org.eclipse.jface.viewers.ISelection;
014: import org.eclipse.swt.events.*;
015: import org.eclipse.swt.widgets.*;
016: import org.eclipse.ui.*;
017: import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
018:
019: /**
020: * This is the action used to contribute the CheatSheets menu to the workbench's
021: * help menu.
022: */
023: public class CheatSheetMenuAction implements
024: IWorkbenchWindowPulldownDelegate2, IPropertyListener {
025: /**
026: * The menu created by this action
027: */
028: private Menu fMenu;
029:
030: /**
031: * Indicates whether the cheat sheet history has changed and
032: * the sub menu needs to be recreated.
033: */
034: protected boolean fRecreateMenu = false;
035:
036: /**
037: * The constructor.
038: */
039: public CheatSheetMenuAction() {
040: CheatSheetPlugin.getPlugin().getCheatSheetHistory()
041: .addListener(this );
042: }
043:
044: /* (non-Javadoc)
045: * @see IWorkbenchWindowActionDelegate#dispose
046: */
047: public void dispose() {
048: setMenu(null);
049: CheatSheetPlugin.getPlugin().getCheatSheetHistory()
050: .removeListener(this );
051: }
052:
053: /**
054: * Fills the drop-down menu with cheat sheets history
055: *
056: * @param menu the menu to fill
057: */
058: protected void fillMenu(Menu menu) {
059: CheatSheetMenu cheatsheetMenuMenuItem = new CheatSheetMenu();
060: cheatsheetMenuMenuItem.fill(menu, 0);
061: }
062:
063: /* (non-Javadoc)
064: * @see org.eclipse.ui.IWorkbenchWindowPulldownDelegate#getMenu(org.eclipse.swt.widgets.Control)
065: */
066: public Menu getMenu(Control parent) {
067: return null;
068: }
069:
070: /* (non-Javadoc)
071: * @see org.eclipse.ui.IWorkbenchWindowPulldownDelegate2#getMenu(org.eclipse.swt.widgets.Menu)
072: */
073: public Menu getMenu(Menu parent) {
074: setMenu(new Menu(parent));
075: fillMenu(fMenu);
076: initMenu();
077: return fMenu;
078: }
079:
080: /* (non-Javadoc)
081: * @see IWorkbenchWindowActionDelegate#init
082: */
083: public void init(IWorkbenchWindow window) {
084: }
085:
086: /**
087: * Creates the menu for the action
088: */
089: private void initMenu() {
090: // Add listener to repopulate the menu each time
091: // it is shown because of dynamic history list
092: fMenu.addMenuListener(new MenuAdapter() {
093: public void menuShown(MenuEvent e) {
094: if (fRecreateMenu) {
095: Menu m = (Menu) e.widget;
096: MenuItem[] items = m.getItems();
097: for (int i = 0; i < items.length; i++) {
098: items[i].dispose();
099: }
100: fillMenu(m);
101: fRecreateMenu = false;
102: }
103: }
104: });
105: }
106:
107: /* (non-Javadoc)
108: * @see org.eclipse.ui.IPropertyListener#propertyChanged(java.lang.Object, int)
109: */
110: public void propertyChanged(Object source, int propId) {
111: fRecreateMenu = true;
112: }
113:
114: /* (non-Javadoc)
115: * @see IWorkbenchWindowActionDelegate#run
116: */
117: public void run(IAction action) {
118: }
119:
120: /* (non-Javadoc)
121: * @see IWorkbenchWindowActionDelegate#selectionChanged
122: */
123: public void selectionChanged(IAction action, ISelection selection) {
124: }
125:
126: /**
127: * Sets this action's drop-down menu, disposing the previous menu.
128: *
129: * @param menu the new menu
130: */
131: private void setMenu(Menu menu) {
132: if (fMenu != null) {
133: fMenu.dispose();
134: }
135: fMenu = menu;
136: }
137: }
|