Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Open menu of toolItem when toolitem is clicked
Open menu of toolItem when toolitem is clicked [message #1049785] Fri, 26 April 2013 09:10 Go to next message
Tobias Schulte is currently offline Tobias SchulteFriend
Messages: 2
Registered: July 2009
Junior Member
I have created a ToolItem with a menu. This works fine. When I click the arrow button, the menu is shown. But now I want the menu to also be shown, when I click the button. I tried the following Handler

public class DropdownButtonHandler {

	@Execute
	public void execute(MToolItem toolItem,
			IPresentationEngine presentationEngine) {
		Menu menu = (Menu) toolItem.getMenu().getWidget();
		if (menu != null)
			menu.setVisible(true);
	}
}


wich works, but only after I have clicked on the arrow button before. When the arrow button has not been clicked before, toolItem.getMenu().getWidget() is null.

How can I achive the desired behavior?
Re: Open menu of toolItem when toolitem is clicked [message #1051732 is a reply to message #1049785] Mon, 29 April 2013 06:55 Go to previous messageGo to next message
Tobias Schulte is currently offline Tobias SchulteFriend
Messages: 2
Registered: July 2009
Junior Member
The "Create new visual classes" ToolItem of the WindowBuilder plugin does exactly what I want. I had a look at the plugin source, and found the sollution by looking at org.eclipse.wb.internal.core.wizards.actions.NewDesignerTypeDropDownAction#runWithEvent. Following action methods works like a charm:

@Execute
public void execute(EModelService modelService, MApplication application,
        @Named("toolItemId") String toolItemId) {
    MUIElement mToolItem = modelService.find(toolItemId, application);
    if (mToolItem != null && mToolItem instanceof MToolItem) {
        execute((MToolItem) mToolItem);
    }
}

@Execute
public void execute(MToolItem mToolItem) {
    ToolItem toolItem = (ToolItem) mToolItem.getWidget();
    Listener[] listeners = toolItem.getListeners(SWT.Selection);
    if (listeners.length > 0) {
        Listener listener = listeners[0];
        // prepare DropDown click event
        Event e = new Event();
        e.type = SWT.Selection;
        e.widget = toolItem;
        e.detail = SWT.DROP_DOWN;
        // send event to the widget
        listener.handleEvent(e);
    }
}


Where the first method is necessary, since I also want this action to work with a shortcut. In that case the toolItemId is a parameter to the command (and also the MKeyBinding#getParameters())
Re: Open menu of toolItem when toolitem is clicked [message #1403124 is a reply to message #1049785] Mon, 21 July 2014 13:53 Go to previous message
SlowStrider Mising name is currently offline SlowStrider Mising nameFriend
Messages: 115
Registered: July 2009
Senior Member
I found this post while trying to do the same for Eclipse 3.x commands. I found an earlier version of WindowBuilder that still used actions. I took part of that code and created a similar command handler for Eclipse 3.x:

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Widget;

/**
 * Handler for drop down commands contributed to a toolbar which opens the
 * corresponding drop down menu when the command is executed (i.e. toolbar item
 * is pressed).
 * 
 * <p>
 * Like <a href="http://www.eclipse.org/forums/index.php/t/483266/">this forum
 * post</a>, the implementation is (mostly) taken from WindowBuilders
 * "Create new visual classes" drop down command.
 *
 * @author Henno Vermeulen
 */
public class DropDownCommandHandler extends AbstractHandler {

	public Object execute(ExecutionEvent event) throws ExecutionException {
		if (event.getTrigger() != null && (event.getTrigger() instanceof Event)) {
			runWithEvent((Event) event.getTrigger());
		}
		return null;
	}

	public void runWithEvent(Event event) {
		// this action is represented as ToolItem
		Widget widget = event.widget;
		if (widget instanceof ToolItem) {
			ToolItem toolItem = (ToolItem) widget;
			Listener[] listeners = toolItem.getListeners(SWT.Selection);
			if (listeners.length > 0) {
				Listener listener = listeners[0];
				// prepare DropDown click event
				Event e = new Event();
				e.type = SWT.Selection;
				e.widget = toolItem;
				e.detail = SWT.DROP_DOWN;
				e.x = toolItem.getBounds().x;
				e.y = toolItem.getBounds().height;
				// send event to the widget
				listener.handleEvent(e);
			}
		}
	}
}



Previous Topic:Handler status evaluation with invalid selection resolution
Next Topic:Spaces in the toolbar with custom product in eclipse 4.3
Goto Forum:
  


Current Time: Sat Dec 20 10:34:20 GMT 2014

Powered by FUDForum. Page generated in 0.01707 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software