Example usage for org.eclipse.jface.action IAction runWithEvent

List of usage examples for org.eclipse.jface.action IAction runWithEvent

Introduction

In this page you can find the example usage for org.eclipse.jface.action IAction runWithEvent.

Prototype

void runWithEvent(Event event);

Source Link

Document

Runs this action, passing the triggering SWT event.

Usage

From source file:com.diffplug.common.swt.jface.Actions.java

License:Apache License

private Actions(IAction action) {
    this.text = action.getText();
    this.style = Style.of(action);
    if (action instanceof ActionImp) {
        callback = ((ActionImp) action).callback;
    } else {/*from  w w w .j a v  a 2  s.  co m*/
        callback = (a, e) -> {
            if (e == null) {
                action.run();
            } else {
                action.runWithEvent(e);
            }
        };
    }
    this.img = action.getImageDescriptor();
    this.accelerator = action.getAccelerator();
    this.tooltip = action.getToolTipText();

    if (accelerator != SWT.NONE) {
        // the toolTip might have had an accelerator added,
        // which we'll want to strip so it doesn't get doubled
        String hint = getAcceleratorHint(accelerator);
        if (tooltip.endsWith(hint)) {
            tooltip = tooltip.substring(0, tooltip.length() - hint.length());
        }
    }
}

From source file:com.google.gdt.eclipse.designer.gxt.model.widgets.UntypedEventsTest.java

License:Open Source License

public void test_openNewListener() throws Exception {
    parseJavaInfo("public class Test implements EntryPoint {", "  public void onModuleLoad() {",
            "    RootPanel rootPanel = RootPanel.get();", "    {", "      Button button = new Button();",
            "      rootPanel.add(button);", "    }", "  }", "}");
    ComponentInfo button = getJavaInfoByName("button");
    // prepare action
    IAction action;
    {//from   w  ww.j av  a  2  s.  c om
        IMenuManager contextMenu = getContextMenu(button);
        IMenuManager eventManager = findChildMenuManager(contextMenu, "ButtonEvent");
        assertNotNull(eventManager);
        action = findChildAction(eventManager, "Select");
        assertNotNull(action);
    }
    // set mock for DesignPageSite
    IDesignPageSite pageSite;
    Capture<Integer> openSourcePosition = new Capture<Integer>();
    {
        pageSite = EasyMock.createStrictMock(IDesignPageSite.class);
        pageSite.openSourcePosition(capture(openSourcePosition));
        EasyMock.replay(pageSite);
        // do set
        DesignPageSite.Helper.setSite(button, pageSite);
    }
    // run action
    action.runWithEvent(new Event());
    waitEventLoop(0);
    // verify
    EasyMock.verify(pageSite);
    assertEditor("public class Test implements EntryPoint {", "  public void onModuleLoad() {",
            "    RootPanel rootPanel = RootPanel.get();", "    {", "      Button button = new Button();",
            "      button.addListener(Events.Select, new Listener<ButtonEvent>() {",
            "        public void handleEvent(ButtonEvent e) {", "        }", "      });",
            "      rootPanel.add(button);", "    }", "  }", "}");
    // check captured position
    {
        assertTrue(openSourcePosition.hasCaptured());
        int position = openSourcePosition.getValue().intValue();
        assertEquals(getNode("button.addListener").getStartPosition(), position);
    }
}

From source file:com.google.gdt.eclipse.designer.gxt.model.widgets.UntypedEventsTest.java

License:Open Source License

public void test_openExistingListener() throws Exception {
    parseJavaInfo("public class Test implements EntryPoint {", "  public void onModuleLoad() {",
            "    RootPanel rootPanel = RootPanel.get();", "    {", "      Button button = new Button();",
            "      rootPanel.add(button);",
            "      button.addListener(Events.Select, new Listener<ButtonEvent>() {",
            "        public void handleEvent(ButtonEvent e) {", "        }", "      });", "    }", "  }", "}");
    ComponentInfo button = getJavaInfoByName("button");
    // prepare action
    IAction action;
    {/*from   w  w w.j a v a 2s  .co  m*/
        IMenuManager contextMenu = getContextMenu(button);
        IMenuManager eventManager = findChildMenuManager(contextMenu, "ButtonEvent");
        assertNotNull(eventManager);
        action = findChildAction(eventManager, "Select");
        assertNotNull(action);
    }
    // set mock for DesignPageSite
    IDesignPageSite pageSite;
    Capture<Integer> openSourcePosition = new Capture<Integer>();
    {
        pageSite = EasyMock.createStrictMock(IDesignPageSite.class);
        pageSite.openSourcePosition(capture(openSourcePosition));
        EasyMock.replay(pageSite);
        // do set
        DesignPageSite.Helper.setSite(button, pageSite);
    }
    // run action
    action.runWithEvent(new Event());
    waitEventLoop(0);
    // verify
    EasyMock.verify(pageSite);
    assertEditor("public class Test implements EntryPoint {", "  public void onModuleLoad() {",
            "    RootPanel rootPanel = RootPanel.get();", "    {", "      Button button = new Button();",
            "      rootPanel.add(button);",
            "      button.addListener(Events.Select, new Listener<ButtonEvent>() {",
            "        public void handleEvent(ButtonEvent e) {", "        }", "      });", "    }", "  }", "}");
    // check captured position
    {
        assertTrue(openSourcePosition.hasCaptured());
        int position = openSourcePosition.getValue().intValue();
        assertEquals(getNode("button.addListener").getStartPosition(), position);
    }
}

From source file:com.google.gdt.eclipse.designer.gxt.model.widgets.UntypedEventsTest.java

License:Open Source License

public void test_removeListener() throws Exception {
    parseJavaInfo("public class Test implements EntryPoint {", "  public void onModuleLoad() {",
            "    RootPanel rootPanel = RootPanel.get();", "    {", "      Button button = new Button();",
            "      rootPanel.add(button);",
            "      button.addListener(Events.Select, new Listener<ButtonEvent>() {",
            "        public void handleEvent(ButtonEvent e) {", "        }", "      });", "    }", "  }", "}");
    ComponentInfo button = getJavaInfoByName("button");
    // prepare action
    IAction action;
    {/*from w  w w.ja  v a2  s  .c  o m*/
        IMenuManager contextMenu = getContextMenu(button);
        IMenuManager eventManager = findChildMenuManager(contextMenu, "ButtonEvent");
        assertNotNull(eventManager);
        action = findChildAction(eventManager, "Select");
        assertNotNull(action);
    }
    // run action
    {
        Event event = new Event();
        event.stateMask = SWT.CTRL;
        action.runWithEvent(event);
    }
    assertEditor("public class Test implements EntryPoint {", "  public void onModuleLoad() {",
            "    RootPanel rootPanel = RootPanel.get();", "    {", "      Button button = new Button();",
            "      rootPanel.add(button);", "    }", "  }", "}");
}

From source file:com.microsoft.tfs.client.common.ui.framework.action.ToolbarPulldownAction.java

License:Open Source License

@Override
public void runWithEvent(final Event event) {
    if (showMenuForDefaultAction) {
        final ToolItem item = (ToolItem) event.widget;
        final Rectangle itemRectangle = item.getBounds();
        final Point point = item.getParent().toDisplay(new Point(itemRectangle.x, itemRectangle.y));

        final Menu menu = getSubActionMenu(item.getParent());
        menu.setLocation(point.x, point.y + itemRectangle.height);
        menu.setVisible(true);//from w ww. java2 s.c  om
    } else {
        final IAction defaultSubAction = getDefaultSubAction();
        if (defaultSubAction != null && defaultSubAction.isEnabled()) {
            defaultSubAction.runWithEvent(event);
        }
    }
}

From source file:es.axios.udig.spatialoperations.internal.ui.dialogs.CreateLayerComposite.java

License:Open Source License

private void createButton(Composite composite, final IAction action) {
    final Button button = new Button(composite, SWT.PUSH);
    button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    button.setToolTipText(action.getToolTipText());

    button.addPaintListener(new PaintListener() {
        ImageRegistry images = UiPlugin.getDefault().getImageRegistry();

        public void paintControl(PaintEvent e) {
            Image image = images.get(action.getId());
            if (image == null || image.isDisposed()) {
                images.put(action.getId(), action.getImageDescriptor());
                image = images.get(action.getId());
            }/*  w  w w .  j  a  va2  s . co m*/

            Point size = button.getSize();
            Rectangle imageBounds = image.getBounds();
            e.gc.drawImage(image, 0, 0, imageBounds.width, imageBounds.height, 2, 2, size.x - 4, size.y - 4);
        }
    });

    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            action.runWithEvent(event);
        }
    });
}

From source file:gov.nasa.arc.spife.core.plan.editor.timeline.commands.TimelineKeyHandler.java

License:Open Source License

private boolean performStroke(KeyStroke key, KeyEvent keyEvent) {
    if (actions == null)
        return false;
    IAction action = actions.get(key);
    if (action == null)
        return false;

    boolean enabled = action.isEnabled();
    if (enabled) {
        Event event = new Event();
        event.character = keyEvent.character;
        event.data = keyEvent.data;/*from  w w  w.  j a v  a  2  s  .  c  om*/
        event.display = keyEvent.display;
        event.doit = keyEvent.doit;
        event.keyCode = keyEvent.keyCode;
        event.keyLocation = keyEvent.keyLocation;
        event.stateMask = keyEvent.stateMask;
        event.time = keyEvent.time;
        event.widget = keyEvent.widget;
        action.runWithEvent(event);
    }
    return enabled; // CHANGED from "true" in the original eclipse code
}

From source file:net.refractions.udig.project.ui.internal.tool.display.ToolManager.java

License:Open Source License

/**
 * Creates a action that acts as a proxy for the tool in the editor toolbar.
 * <p>//from  ww w.  ja  va2 s  . co m
 * The client code must set the name image descriptor etc... of the Action
 * </p>
 * 
 * @param toolID the id of the tool
 * @param categoryID the category the tool is part of
 * @return a proxy action that can be put in other toolbars
 */
public IAction createToolAction(final String toolID, final String categoryID) {
    final IAction toolAction = new Action() {
        @Override
        public void runWithEvent(Event event) {
            IAction action = getTool(toolID, categoryID);
            if (action != null && action.isEnabled()) {
                action.runWithEvent(event);
            }
        }
    };
    toolAction.addPropertyChangeListener(new IPropertyChangeListener() {

        public void propertyChange(PropertyChangeEvent event) {
            if (event.getProperty().equals(IAction.ENABLED)) {
                toolAction.setEnabled((Boolean) event.getNewValue());
            }
        }

    });
    toolAction.setEnabled(getTool(toolID, categoryID).isEnabled());
    addToolAction(toolAction);
    return toolAction;
}

From source file:net.refractions.udig.ui.FeatureTypeEditorDialog.java

License:Open Source License

private void createButton(Composite composite, final IAction action) {
    final Button button = new Button(composite, SWT.PUSH | SWT.FLAT);
    GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
    button.setLayoutData(data);//ww  w.j  a va2  s .c o m
    button.setToolTipText(action.getToolTipText());

    ImageRegistry images = UiPlugin.getDefault().getImageRegistry();
    Image image = images.get(action.getId());
    if (image == null || image.isDisposed()) {
        images.put(action.getId(), action.getImageDescriptor());
        image = images.get(action.getId());
    }
    button.setImage(image);

    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            action.runWithEvent(event);
        }
    });
}

From source file:org.eclipse.birt.report.designer.internal.ui.swt.custom.TabbedPropertyTitle.java

License:Open Source License

private void handleWidgetSelection(Event e, ToolItem item) {

    boolean selection = item.getSelection();

    int style = item.getStyle();
    IAction action = (IAction) actionMap.get(item);

    if ((style & (SWT.TOGGLE | SWT.CHECK)) != 0) {
        if (action.getStyle() == IAction.AS_CHECK_BOX) {
            action.setChecked(selection);
        }/* w  w  w .j a  va  2s . c om*/
    } else if ((style & SWT.RADIO) != 0) {
        if (action.getStyle() == IAction.AS_RADIO_BUTTON) {
            action.setChecked(selection);
        }
    } else if ((style & SWT.DROP_DOWN) != 0) {
        if (e.detail == 4) { // on drop-down button
            if (action.getStyle() == IAction.AS_DROP_DOWN_MENU) {
                IMenuCreator mc = action.getMenuCreator();
                ToolItem ti = (ToolItem) item;
                if (mc != null) {
                    Menu m = mc.getMenu(ti.getParent());
                    if (m != null) {
                        Point point = ti.getParent().toDisplay(new Point(e.x, e.y));
                        m.setLocation(point.x, point.y); // waiting
                        m.setVisible(true);
                        return; // we don't fire the action
                    }
                }
            }
        }
    }

    action.runWithEvent(e);
}