Bug 58489 - [Workbench] Need to be able to create and contribute drop down menu and actions to the action bar.
Summary: [Workbench] Need to be able to create and contribute drop down menu and actio...
Status: RESOLVED INVALID
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Platform-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: needinfo
: 58490 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-04-14 11:01 EDT by anne_wang CLA
Modified: 2009-08-30 02:09 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description anne_wang CLA 2004-04-14 11:01:25 EDT
PROBLEM: Enable to create and contribute drop down menu and actions to the 
action bar.  The drop down menu should support either a default action (i.e. 
when user clicks the top level menu action, a default action will be executed), 
or without a default action (i.e. User has to select one of the actions in the 
drop down menu).

SOLUTION:  I'm includeing 3 files here:  
1. DropDownAction.java
2. DropDownMenu.java
3. DropDownUsage.java - snippet of usage:

---------------------------   DropDownAction.java---------------------------
/**
 *  */
public class DropDownAction extends Action implements IMenuCreator, 
IWorkbenchWindowPulldownDelegate2 {
	private IWorkbench 	_workbench;
	private Action 		_defaultAction;
	private ContributionItem _dropDownMenu;
	private ToolBar 	_toolBar;
	private int 		_toolItemIndex;
	private MenuManager dropDownMenuMgr;
	/**
	 *	Create a new instance of this class
	 */
	//  constructor
	public DropDownAction(IWorkbench workbench, Action defaultAction, 
ContributionItem dropDownMenu) {
		super(defaultAction.getText()); 
		this._workbench = workbench;
		this._defaultAction  = defaultAction;
		this._dropDownMenu = dropDownMenu;
		setToolTipText(_defaultAction.getToolTipText());
		setImageDescriptor(_defaultAction.getImageDescriptor());
		setMenuCreator(this);
	}
	/**
	 *	constructor for drop down action without a default action.
	 */
	public DropDownAction(IWorkbench workbench, Action defaultAction, 
ContributionItem dropDownMenu, ToolBar toolBar, int toolItemIndex) {
		super(defaultAction == null ? "" : defaultAction.getText()); 
		this._workbench 		= workbench;
		this._defaultAction  	= defaultAction;
		this._dropDownMenu 		= dropDownMenu;
		this._toolBar 			= toolBar;
		this._toolItemIndex 	= toolItemIndex;

		if (_defaultAction != null) {
			setToolTipText(_defaultAction.getToolTipText());
			setImageDescriptor(_defaultAction.getImageDescriptor());
		}
		
		setMenuCreator(this);
	}
	/**
	 * create the menu manager for the drop down menu.
	 */
	protected void createDropDownMenuMgr() {
		if (dropDownMenuMgr == null) {
			dropDownMenuMgr = new MenuManager();
			dropDownMenuMgr.add(_dropDownMenu);
		}
	}
	
	/**
	 * dispose method comment.
	 */
	public void dispose() {
		if (dropDownMenuMgr != null) {
			dropDownMenuMgr.dispose();
			dropDownMenuMgr = null;
		}
	}
	/**
	 * getMenu method comment.
	 */
	public Menu getMenu(Control parent) {
		createDropDownMenuMgr();
		return dropDownMenuMgr.createContextMenu(parent);
	}
	/**
	 * Create the drop down menu as a submenu of parent.  Necessary
	 * for CoolBar support.
	 */
	public Menu getMenu(Menu parent) {
		createDropDownMenuMgr();
		Menu menu= new Menu(parent);
		IContributionItem[] items = dropDownMenuMgr.getItems();
		for (int i=0; i<items.length; i++) {
			IContributionItem item = items[i];
			IContributionItem newItem = item;
			if (item instanceof ActionContributionItem) {
				newItem = new ActionContributionItem
(((ActionContributionItem)item).getAction());
			}
			newItem.fill(menu, -1);
		}
		return menu;
	}
	/**
	 * @see IWorkbenchWindowActionDelegate#init(IWorkbenchWindow)
	 */
	public void init(IWorkbenchWindow window){
	}
	
    // this is the execution of the default action 
    public void run() {

        if (_defaultAction != null && _defaultAction.isEnabled())
            _defaultAction.run();
        else {
            ToolItem ti = _toolBar.getItem(_toolItemIndex);
            if (_dropDownMenu != null) {
                Menu m = getMenu(_toolBar);
                if (m != null) {
                    // position the menu below the drop down item
                    Rectangle b = ti.getBounds();
                    Point p = ti.getParent().toDisplay(new Point(b.x, b.y + 
b.height));
                    m.setLocation(p.x, p.y); // waiting for SWT 0.42
                    m.setVisible(true);
                }
            }
        }
    }
    
	/**
	 * @see runWithEvent(IAction, Event)
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
	}
	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection){
	}
}


---------------------------   DropDownMenu.java---------------------------
public class DropDownMenu extends ContributionItem {
	private Map actions = new HashMap(21);
	private Action[] _submenuActions;
	private ContributionItem[] _submenuContributionItems;
	private boolean enabled = true;
	private IWorkbenchWindow window;

	private boolean dirty = true;
	private IMenuListener menuListener = new IMenuListener() {
		public void menuAboutToShow(IMenuManager manager) {
			manager.markDirty();
			dirty = true;
		}
	};	
		
	//  constructor
	public DropDownMenu(IMenuManager innerMgr, IWorkbenchWindow window, 
Action[] submenuActions) {
		this(window, submenuActions);
		fillMenu(innerMgr); // Must be done after constructor to ensure 
field initialization.
	}
	//	 constructor
	public DropDownMenu(IWorkbenchWindow window, Action[] submenuActions) {
		super();
		this.window = window;
		this._submenuActions = submenuActions;
		this._submenuContributionItems = null;
	}
	//	 constructor
	public DropDownMenu(IWorkbenchWindow window, ContributionItem[] 
submenuContributionItems) {
		super();
		this.window = window;
		this._submenuActions = null;
		this._submenuContributionItems = submenuContributionItems;
	}
	//	constructor
	public DropDownMenu(IWorkbenchWindow window, Action[] submenuActions, 
ContributionItem[] submenuContributionItems) {
		super();
		this.window = window;
		this._submenuActions = submenuActions;
		this._submenuContributionItems = submenuContributionItems;
	}
	/* (non-Javadoc)
	 * Fills the menu with New Wizards.
	 */
	private void fillMenu(IContributionManager innerMgr) {
		// Remove all.
		innerMgr.removeAll();

		if (this.enabled) {
			
			// ===== Add submenu here ... ======
			if (_submenuActions != null) {
				for (int i=0;i<_submenuActions.length;i++) {
					if (_submenuActions[i]==null) // a null 
action indicates a separator
						innerMgr.add(new Separator());
					else
						innerMgr.add(_submenuActions
[i]);
				}
			}
		
			if (_submenuContributionItems != null) {
				innerMgr.add(new Separator());
				for (int 
i=0;i<_submenuContributionItems.length;i++) {
					if (_submenuContributionItems[i]
==null)  // null contribution item indicates a separator
						innerMgr.add(new Separator());
					else
						innerMgr.add
(_submenuContributionItems[i]);
				}
			}
		}
	}
	/* (non-Javadoc)
	 * Returns the action for the given wizard id, or null if not found.
	 */
	private IAction getAction(String id) {
		// Keep a cache, rather than creating a new action each time,
		// so that image caching in ActionContributionItem works.
		IAction action = (IAction) actions.get(id);
		if (action == null) {
			}
		return action;
	}
	/* (non-Javadoc)
	 * Method declared on IContributionItem.
	 */
	public boolean isEnabled() {
		return enabled;
	}
	/* (non-Javadoc)
	 * Method declared on IContributionItem.
	 */
	public boolean isDynamic() {
		return true;
	}
	/* (non-Javadoc)
	 * Method declared on IContributionItem.
	 */
	public boolean isDirty() {
		return dirty;
	}
	/**
	 * Sets the enabled state of the receiver.
	 * 
	 * @param enabledValue if <code>true</code> the menu is enabled; else
	 * 		it is disabled
	 */
	public void setEnabled(boolean enabledValue) {
		this.enabled = enabledValue;
	}
	/**
	 * Removes all listeners from the containing workbench window.
	 * <p>
	 * This method should only be called if the shortcut menu is created
	 * with <code>register = true</code>.
	 * </p>
	 * 
	 * @deprecated
	 */
	public void deregisterListeners() {}
	/* (non-Javadoc)
	 * Method declared on IContributionItem.
	 */
	public void fill(Menu menu, int index) {
		if(getParent() instanceof MenuManager) {
			((MenuManager)getParent()).addMenuListener
(menuListener);
		}
			
		// commented out the following dirty checking, so the submenu 
will always be re-build.
		// This might have some performance hit ???	
//		if(!dirty)
//			return;
	
		MenuManager manager = new MenuManager();
		fillMenu(manager);
		IContributionItem items[] = manager.getItems();
		for (int i = 0; i < items.length; i++) {
			items[i].fill(menu,index++);
		}
		dirty = false;
	}

}


---------------------------   DropDownUsage.java (snippet code)-----------------

// create actions to be executed
CommandAction new1Action = new CommandAction("new1Action");
CommandAction new2Action = new CommandAction("new2Action");
CommandAction new3Action = new CommandAction("new3Action");

CommandAction[] newSubmenuActions={new1Action ,new2Action ,new3Action };

// create drop down menu
DropDownMenu newDropDownMenu = new DropDownMenu
(workbenchWindow,newSubmenuActions);

// create drop down action - with default action new1Action
DropDownAction newDropDownAction = new DropDownAction
(workbenchWindow.getWorkbench(),new1Action , newDropDownMenu );

// contribute the drop down action
actionBarManager.add(newDropDownAction );
Comment 1 Wassim Melhem CLA 2004-04-14 11:07:45 EDT
*** Bug 58490 has been marked as a duplicate of this bug. ***
Comment 2 Tod Creasey CLA 2006-04-13 21:14:29 EDT
Is this still an issue?
Comment 3 Denis Roy CLA 2009-08-30 02:09:37 EDT
As of now 'LATER' and 'REMIND' resolutions are no longer supported.
Please reopen this bug if it is still valid for you.