Example usage for org.eclipse.jface.action IMenuCreator getMenu

List of usage examples for org.eclipse.jface.action IMenuCreator getMenu

Introduction

In this page you can find the example usage for org.eclipse.jface.action IMenuCreator getMenu.

Prototype

public Menu getMenu(Menu parent);

Source Link

Document

Returns an SWT menu created as a drop down menu parented by the given menu.

Usage

From source file:ch.hsr.ifs.cdt.metriculator.views.MetriculatorView.java

License:Open Source License

private void createActionExport() {
    actionExport = new Action("Export") {

        @Override// www.j a  v  a 2  s  .co  m
        public void runWithEvent(Event e) {
            IMenuCreator mc = this.getMenuCreator();
            Widget item = e.widget;
            ToolItem ti = (ToolItem) item;

            if (mc != null) {
                Menu m = mc.getMenu(ti.getParent());
                if (m != null) {
                    // position the menu below the drop down item / next to cursor
                    Point point = item.getDisplay().getCursorLocation();
                    m.setLocation(point.x, point.y);
                    m.setVisible(true);
                    return; // we don't fire the action
                }
            }

        }
    };
    actionExport.setImageDescriptor(
            MetriculatorPluginActivator.getDefault().getImageDescriptor(Icon.Size16.EXPORT));
    actionExport.setMenuCreator(new ExportActionMenuCreator(this));
}

From source file:com.architexa.org.eclipse.gef.internal.ui.palette.ToolbarDropdownContributionItem.java

License:Open Source License

/**
 * The <code>ToolbarDropdownContributionItem</code> implementation of this 
 * <code>IContributionItem</code> method creates a SWT MenuItem for the action. 
 * If the action's checked property has been set, a toggle button is created 
 * and primed to the value of the checked property.
 * If the action's menu creator property has been set, a cascading submenu is created.
 *//*from  ww w .  j  av  a2s .co  m*/
public void fill(Menu parent, int index) {
    if (widget == null && parent != null) {
        int flags = SWT.PUSH;
        Menu subMenu = null;

        if (action != null) {
            int style = action.getStyle();
            if (style == IAction.AS_CHECK_BOX)
                flags = SWT.CHECK;
            else if (style == IAction.AS_DROP_DOWN_MENU) {
                IMenuCreator mc = action.getMenuCreator();
                subMenu = mc.getMenu(parent);
                flags = SWT.CASCADE;
            }
        }

        MenuItem mi = null;
        if (index >= 0)
            mi = new MenuItem(parent, flags, index);
        else
            mi = new MenuItem(parent, flags);
        widget = mi;

        mi.setData(this);
        mi.addListener(SWT.Arm, listener);
        mi.addListener(SWT.Dispose, listener);
        mi.addListener(SWT.Selection, listener);
        if (action.getHelpListener() != null)
            mi.addHelpListener(action.getHelpListener());

        if (subMenu != null)
            mi.setMenu(subMenu);

        update(null);

        action.addPropertyChangeListener(listener);
    }
}

From source file:com.architexa.org.eclipse.gef.internal.ui.palette.ToolbarDropdownContributionItem.java

License:Open Source License

/**
 * Handles a widget selection event./*from  ww w .jav a2  s.c o  m*/
 */
private void handleWidgetSelection(Event e) {
    Widget item = e.widget;
    if (item != null) {

        int style = item.getStyle();

        if ((style & (SWT.TOGGLE | SWT.CHECK)) != 0) {
            if (action.getStyle() == IAction.AS_CHECK_BOX) {
                action.setChecked(!action.isChecked());
            }

        } else if ((style & SWT.DROP_DOWN) != 0) {
            /*
             * Added by Pratik Shah
             * Do this regardless of whether the down arrow button on the side was
             * clicked, or the main button itself
             */
            if (action.getStyle() == IAction.AS_DROP_DOWN_MENU) {
                IMenuCreator mc = action.getMenuCreator();
                ToolItem ti = (ToolItem) item;
                // we create the menu as a sub-menu of "dummy" so that we can use
                // it in a cascading menu too.
                // If created on a SWT control we would get an SWT error...
                //Menu dummy= new Menu(ti.getParent());
                //Menu m= mc.getMenu(dummy);
                //dummy.dispose();

                Menu m = mc.getMenu(ti.getParent());
                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);
                    return; // we don't fire the action
                }
            }
        }

        // Ensure action is enabled first.
        // See 1GAN3M6: ITPUI:WINNT - Any IAction in the workbench can be executed 
        // while disabled.
        if (action.isEnabled()) {
            action.runWithEvent(e);
        }
    }
}

From source file:com.github.haixing_hu.swt.action.ActionContributionItemEx.java

License:Open Source License

/**
 * Handles a widget selection event./*from w  ww  .  j ava 2  s.com*/
 */
private void handleWidgetSelection(Event e, boolean selection) {

    final Widget item = e.widget;
    if (item != null) {
        final int style = item.getStyle();

        if ((style & (SWT.TOGGLE | SWT.CHECK)) != 0) {
            if (action.getStyle() == IAction.AS_CHECK_BOX) {
                action.setChecked(selection);
            }
        } 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) {
                    final IMenuCreator mc = action.getMenuCreator();
                    menuCreatorCalled = true;
                    final ToolItem ti = (ToolItem) item;
                    // we create the menu as a sub-menu of "dummy" so that
                    // we can use
                    // it in a cascading menu too.
                    // If created on a SWT control we would get an SWT
                    // error...
                    // Menu dummy= new Menu(ti.getParent());
                    // Menu m= mc.getMenu(dummy);
                    // dummy.dispose();
                    if (mc != null) {
                        final Menu m = mc.getMenu(ti.getParent());
                        if (m != null) {
                            // position the menu below the drop down item
                            final Point point = ti.getParent().toDisplay(new Point(e.x, e.y));
                            m.setLocation(point.x, point.y); // waiting
                            // for SWT
                            // 0.42
                            m.setVisible(true);
                            return; // we don't fire the action
                        }
                    }
                }
            }
        }

        ExternalActionManager.IExecuteCallback callback = null;
        final String actionDefinitionId = action.getActionDefinitionId();
        if (actionDefinitionId != null) {
            final Object obj = ExternalActionManager.getInstance().getCallback();
            if (obj instanceof ExternalActionManager.IExecuteCallback) {
                callback = (ExternalActionManager.IExecuteCallback) obj;
            }
        }

        // Ensure action is enabled first.
        // See 1GAN3M6: ITPUI:WINNT - Any IAction in the workbench can be
        // executed while disabled.
        if (action.isEnabled()) {
            final boolean trace = Policy.TRACE_ACTIONS;

            long ms = 0L;
            if (trace) {
                ms = System.currentTimeMillis();
                System.out.println("Running action: " + action.getText()); //$NON-NLS-1$
            }

            IPropertyChangeListener resultListener = null;
            if (callback != null) {
                resultListener = new IPropertyChangeListener() {
                    @Override
                    public void propertyChange(PropertyChangeEvent event) {
                        // Check on result
                        if (event.getProperty().equals(IAction.RESULT)) {
                            if (event.getNewValue() instanceof Boolean) {
                                result = (Boolean) event.getNewValue();
                            }
                        }
                    }
                };
                action.addPropertyChangeListener(resultListener);
                callback.preExecute(action, e);
            }

            action.runWithEvent(e);

            if (callback != null) {
                if ((result == null) || result.equals(Boolean.TRUE)) {
                    callback.postExecuteSuccess(action, Boolean.TRUE);
                } else {
                    callback.postExecuteFailure(action,
                            new ExecutionException(action.getText() + " returned failure.")); //$NON-NLS-1$
                }
            }

            if (resultListener != null) {
                result = null;
                action.removePropertyChangeListener(resultListener);
            }
            if (trace) {
                System.out
                        .println((System.currentTimeMillis() - ms) + " ms to run action: " + action.getText()); //$NON-NLS-1$
            }
        } else {
            if (callback != null) {
                callback.notEnabled(action, new NotEnabledException(action.getText() + " is not enabled.")); //$NON-NLS-1$
            }
        }
    }
}

From source file:com.github.haixing_hu.swt.action.ActionContributionItemEx.java

License:Open Source License

/**
 * The proxy menu is being shown, we better get the real menu.
 *
 * @param proxy/*ww  w.  ja  va 2s  .c o  m*/
 *          the proxy menu
 * @since 3.4
 */
private void handleShowProxy(Menu proxy) {
    proxy.removeListener(SWT.Show, getMenuCreatorListener());
    final IMenuCreator mc = action.getMenuCreator();
    menuCreatorCalled = true;
    if (mc == null) {
        return;
    }
    holdMenu = mc.getMenu(proxy.getParentMenu());
    if (holdMenu == null) {
        return;
    }
    copyMenu(holdMenu, proxy);
}

From source file:com.github.haixing_hu.swt.action.DropDownAction.java

License:Open Source License

@Override
public void runWithEvent(Event event) {
    final IMenuCreator mc = getMenuCreator();
    if (mc == null) {
        logger.error("No menu creator for action: {}", getId());
        return;//  www . j  a  va  2 s .com
    }
    if (event.widget instanceof ToolItem) {
        final ToolItem ti = (ToolItem) event.widget;
        final Menu menu = mc.getMenu(ti.getParent());
        //  calculate the position where to display the drop-down menu
        Point point = new Point(event.x, event.y);
        final Rectangle rect = ti.getBounds();
        point.x += rect.x;
        point.y += rect.y + rect.height;
        point = ti.getParent().toDisplay(point);
        // position the menu below the drop down item
        menu.setLocation(point.x, point.y);
        menu.setVisible(true);
    } else {
        logger.error("Cannot create pop-menu for action: {}", getId());
    }
}

From source file:net.tourbook.ui.tourChart.action.ActionChartOptions.java

License:Open Source License

@Override
public void runWithEvent(final Event event) {

    // open and position drop down menu below the action button
    final Widget item = event.widget;
    if (item instanceof ToolItem) {

        final ToolItem toolItem = (ToolItem) item;

        final IMenuCreator mc = getMenuCreator();
        if (mc != null) {

            final ToolBar toolBar = toolItem.getParent();

            final Menu menu = mc.getMenu(toolBar);
            if (menu != null) {

                final Rectangle toolItemBounds = toolItem.getBounds();
                Point topLeft = new Point(toolItemBounds.x, toolItemBounds.y + toolItemBounds.height);
                topLeft = toolBar.toDisplay(topLeft);

                menu.setLocation(topLeft.x, topLeft.y);
                menu.setVisible(true);/*from   w  w  w.j a  v a  2 s .c  o  m*/
            }
        }
    }

}

From source file:net.yatomiya.e4.ui.workbench.renderers.swt.AbstractContributionItem.java

License:Open Source License

protected Menu getMenu(final MMenu mmenu, ToolItem toolItem) {
    Object obj = mmenu.getWidget();
    if (obj instanceof Menu && !((Menu) obj).isDisposed()) {
        return (Menu) obj;
    }//  w ww .  j ava 2 s .c  om
    // this is a temporary passthrough of the IMenuCreator
    if (RenderedElementUtil.isRenderedMenu(mmenu)) {
        obj = RenderedElementUtil.getContributionManager(mmenu);
        if (obj instanceof IContextFunction) {
            final IEclipseContext lclContext = getContext(mmenu);
            obj = ((IContextFunction) obj).compute(lclContext, null);
            RenderedElementUtil.setContributionManager(mmenu, obj);
        }
        if (obj instanceof IMenuCreator) {
            final IMenuCreator creator = (IMenuCreator) obj;
            final Menu menu = creator.getMenu(toolItem.getParent().getShell());
            if (menu != null) {
                toolItem.addDisposeListener(new DisposeListener() {
                    @Override
                    public void widgetDisposed(DisposeEvent e) {
                        if (menu != null && !menu.isDisposed()) {
                            creator.dispose();
                            mmenu.setWidget(null);
                        }
                    }
                });
                menu.setData(AbstractPartRenderer.OWNING_ME, menu);
                return menu;
            }
        }
    } else {
        final IEclipseContext lclContext = getContext(getModel());
        IPresentationEngine engine = lclContext.get(IPresentationEngine.class);
        obj = engine.createGui(mmenu, toolItem.getParent(), lclContext);
        if (obj instanceof Menu) {
            return (Menu) obj;
        }
        if (logger != null) {
            logger.debug("Rendering returned " + obj); //$NON-NLS-1$
        }
    }
    return null;
}

From source file:org.carrot2.workbench.core.helpers.DropDownMenuAction.java

License:Open Source License

/**
 * Show a menu for the given action.// w  w  w .j a  v a2  s.  c  o m
 */
public static void showMenu(IAction action, Event e) {
    final IMenuCreator mc = action.getMenuCreator();

    if (mc != null && e != null) {
        final ToolItem ti = (ToolItem) e.widget;
        if (ti != null && ti instanceof ToolItem) {
            final Menu m = mc.getMenu((ti).getParent());
            if (m != null) {
                final Rectangle b = ti.getBounds();
                final Point p = ti.getParent().toDisplay(new Point(b.x, b.y + b.height));
                m.setLocation(p.x, p.y);
                m.setVisible(true);
            }
        }
    }
}

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);
        }//  ww  w .  ja v  a 2 s .com
    } 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);
}