Example usage for org.eclipse.jface.commands ActionHandler ActionHandler

List of usage examples for org.eclipse.jface.commands ActionHandler ActionHandler

Introduction

In this page you can find the example usage for org.eclipse.jface.commands ActionHandler ActionHandler.

Prototype

public ActionHandler(final IAction action) 

Source Link

Document

Creates a new instance of this class given an instance of IAction.

Usage

From source file:ch.elexis.actions.GlobalActions.java

License:Open Source License

/**
 * Creates an ActionHandler for the given IAction and registers it to the Site's HandlerService,
 * i. e. binds the action to the command so that key bindings get activated. You need to set the
 * action's actionDefinitionId to the command id.
 * // ww w. j av a  2 s .c o m
 * @param action
 *            the action to activate. The action's actionDefinitionId must have been set to the
 *            command's id (using <code>setActionDefinitionId()</code>)
 * @param part
 *            the view this action should be registered for
 */
public static void registerActionHandler(final ViewPart part, final IAction action) {
    String commandId = action.getActionDefinitionId();
    if (!StringTool.isNothing(commandId)) {
        IHandlerService handlerService = (IHandlerService) part.getSite().getService(IHandlerService.class);
        IHandler handler = new ActionHandler(action);
        handlerService.activateHandler(commandId, handler);
    }
}

From source file:ch.elexis.core.ui.actions.GlobalActions.java

License:Open Source License

/**
 * Creates an ActionHandler for the given IAction and registers it to the Site's HandlerService,
 * i. e. binds the action to the command so that key bindings get activated. You need to set the
 * action's actionDefinitionId to the command id.
 * /*w ww . j a v a 2s  . c o  m*/
 * @param action
 *            the action to activate. The action's actionDefinitionId must have been set to the
 *            command's id (using <code>setActionDefinitionId()</code>)
 * @param part
 *            the view this action should be registered for
 */
public static void registerActionHandler(final ViewPart part, final IAction action) {
    String commandId = action.getActionDefinitionId();
    if (!StringTool.isNothing(commandId)) {
        IHandlerService handlerService = part.getSite().getService(IHandlerService.class);
        IHandler handler = new ActionHandler(action);
        handlerService.activateHandler(commandId, handler);
    }
}

From source file:cideplus.ui.astview.ASTView.java

License:Open Source License

private void contributeToActionBars() {
    IActionBars bars = getViewSite().getActionBars();
    fillLocalPullDown(bars.getMenuManager());
    fillLocalToolBar(bars.getToolBarManager());
    bars.setGlobalActionHandler(ActionFactory.COPY.getId(), fCopyAction);
    bars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), fFocusAction);
    bars.setGlobalActionHandler(ActionFactory.DELETE.getId(), fDeleteAction);

    IHandlerService handlerService = (IHandlerService) getViewSite().getService(IHandlerService.class);
    handlerService.activateHandler(IWorkbenchCommandConstants.NAVIGATE_TOGGLE_LINK_WITH_EDITOR,
            new ActionHandler(fLinkWithEditor));
}

From source file:com.amazonaws.eclipse.dynamodb.editor.DynamoDBTableEditor.java

License:Apache License

private void createActions() {
    runScanAction = new Action() {
        @Override/*  w w  w  . j av  a 2s.  c om*/
        public ImageDescriptor getImageDescriptor() {
            return AwsToolkitCore.getDefault().getImageRegistry().getDescriptor(AwsToolkitCore.IMAGE_START);
        }

        @Override
        public String getText() {
            return "Run scan";
        }

        @Override
        public String getToolTipText() {
            return getText();
        }

        @Override
        public void run() {
            runScan();
        }

        @Override
        public String getActionDefinitionId() {
            return "com.amazonaws.eclipse.dynamodb.editor.runScan";
        }
    };

    IHandler handler = new ActionHandler(runScanAction);
    IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
    handlerService.activateHandler(runScanAction.getActionDefinitionId(), handler);

    saveAction = new Action() {

        @Override
        public ImageDescriptor getImageDescriptor() {
            return PlatformUI.getWorkbench().getSharedImages()
                    .getImageDescriptor(ISharedImages.IMG_ETOOL_SAVE_EDIT);
        }

        @Override
        public String getText() {
            return "Save changes to Dynamo";
        }

        @Override
        public void run() {
            getEditorSite().getPage().saveEditor(DynamoDBTableEditor.this, false);
        }

        @Override
        public String getActionDefinitionId() {
            return "org.eclipse.ui.file.save";
        }
    };

    handlerService.activateHandler(saveAction.getActionDefinitionId(), new ActionHandler(saveAction));

    nextPageResultsAction = new Action() {

        @Override
        public ImageDescriptor getImageDescriptor() {
            return DynamoDBPlugin.getDefault().getImageRegistry()
                    .getDescriptor(DynamoDBPlugin.IMAGE_NEXT_RESULTS);
        }

        @Override
        public String getText() {
            return "Next page of results";
        }

        @Override
        public void run() {
            getNextPageResults();
        }

    };

    exportAsCSVAction = new Action() {

        @Override
        public ImageDescriptor getImageDescriptor() {
            return AwsToolkitCore.getDefault().getImageRegistry().getDescriptor(AwsToolkitCore.IMAGE_EXPORT);
        }

        @Override
        public String getText() {
            return "Export as CSV";
        }

        @Override
        public void run() {
            FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
            dialog.setOverwrite(true);
            dialog.setFilterExtensions(exportExtensions);
            String csvFile = dialog.open();
            if (csvFile != null) {
                writeCsvFile(csvFile);
            }
        }

        private void writeCsvFile(final String csvFile) {
            try {
                // truncate file before writing
                RandomAccessFile raf = new RandomAccessFile(new File(csvFile), "rw");
                raf.setLength(0L);
                raf.close();

                List<Map<String, AttributeValue>> items = new LinkedList<Map<String, AttributeValue>>();

                for (TableItem tableItem : viewer.getTable().getItems()) {
                    @SuppressWarnings("unchecked")
                    Map<String, AttributeValue> e = (Map<String, AttributeValue>) tableItem.getData();
                    items.add(e);
                }

                BufferedWriter out = new BufferedWriter(new FileWriter(csvFile));
                boolean seenOne = false;
                for (String col : contentProvider.getColumns()) {
                    if (seenOne) {
                        out.write(",");
                    } else {
                        seenOne = true;
                    }
                    out.write(col);
                }
                out.write("\n");

                for (Map<String, AttributeValue> item : items) {
                    seenOne = false;
                    for (String col : contentProvider.getColumns()) {
                        if (seenOne) {
                            out.write(",");
                        } else {
                            seenOne = true;
                        }
                        AttributeValue values = item.get(col);
                        if (values != null) {
                            String value = format(values);
                            // For csv files, we need to quote all values and escape all quotes
                            value = value.replaceAll("\"", "\"\"");
                            value = "\"" + value + "\"";
                            out.write(value);
                        }
                    }
                    out.write("\n");
                }

                out.close();

            } catch (Exception e) {
                AwsToolkitCore.getDefault().logException("Couldn't save CSV file", e);
            }
        }

    };

    addNewAttributeAction = new Action() {

        @Override
        public ImageDescriptor getImageDescriptor() {
            return AwsToolkitCore.getDefault().getImageRegistry().getDescriptor(AwsToolkitCore.IMAGE_ADD);
        }

        @Override
        public String getText() {
            return "Add attribute column";
        }

        @Override
        public void run() {
            NewAttributeDialog dialog = new NewAttributeDialog();
            if (dialog.open() == 0) {
                contentProvider.columns.add(dialog.getNewAttributeName());
                contentProvider.createColumn(viewer.getTable(),
                        (TableColumnLayout) viewer.getTable().getParent().getLayout(),
                        dialog.getNewAttributeName());
                viewer.getTable().getParent().layout();
            }
        }

        final class NewAttributeDialog extends AbstractAddNewAttributeDialog {

            @Override
            public void validate() {
                if (getButton(0) == null)
                    return;
                if (getNewAttributeName().length() == 0
                        || contentProvider.columns.contains(getNewAttributeName())) {
                    getButton(0).setEnabled(false);
                    return;
                }

                getButton(0).setEnabled(true);
                return;
            }

        }
    };

    runScanAction.setEnabled(false);
    saveAction.setEnabled(false);
    nextPageResultsAction.setEnabled(false);
    exportAsCSVAction.setEnabled(false);
    addNewAttributeAction.setEnabled(false);

    toolBarManager.add(runScanAction);
    toolBarManager.add(nextPageResultsAction);
    toolBarManager.add(saveAction);
    toolBarManager.add(exportAsCSVAction);
    toolBarManager.add(addNewAttributeAction);
    toolBarManager.update(true);
}

From source file:com.archimatetool.editor.diagram.AbstractDiagramEditor.java

License:Open Source License

/**
 * Add some extra Actions - *after* the graphical viewer has been created
 *///  ww  w.j  a va 2  s.  c  o  m
@SuppressWarnings("unchecked")
protected void createActions(GraphicalViewer viewer) {
    ActionRegistry registry = getActionRegistry();
    IAction action;

    // Zoom Manager tweaking
    ZoomManager zoomManager = (ZoomManager) getAdapter(ZoomManager.class);
    double[] zoomLevels = { .25, .5, .75, 1, 1.5, 2, 3, 4, 6, 8 };
    zoomManager.setZoomLevels(zoomLevels);
    List<String> zoomContributionLevels = new ArrayList<String>();
    zoomContributionLevels.add(ZoomManager.FIT_ALL);
    zoomContributionLevels.add(ZoomManager.FIT_WIDTH);
    zoomContributionLevels.add(ZoomManager.FIT_HEIGHT);
    zoomManager.setZoomLevelContributions(zoomContributionLevels);

    // Zoom Actions
    IAction zoomIn = new ZoomInAction(zoomManager);
    IAction zoomOut = new ZoomOutAction(zoomManager);
    registry.registerAction(zoomIn);
    registry.registerAction(zoomOut);

    // Add these zoom actions to the key binding service
    IHandlerService service = (IHandlerService) getEditorSite().getService(IHandlerService.class);
    service.activateHandler(zoomIn.getActionDefinitionId(), new ActionHandler(zoomIn));
    service.activateHandler(zoomOut.getActionDefinitionId(), new ActionHandler(zoomOut));

    // Add our own Select All Action so we can select connections as well
    action = new SelectAllAction(this);
    registry.registerAction(action);

    // Add our own Print Action
    action = new PrintDiagramAction(this);
    registry.registerAction(action);

    // Direct Edit Rename
    action = new DirectEditAction(this);
    action.setId(ActionFactory.RENAME.getId()); // Set this for Global Handler
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Change the Delete Action label
    action = registry.getAction(ActionFactory.DELETE.getId());
    action.setText(Messages.AbstractDiagramEditor_2);
    action.setToolTipText(action.getText());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Paste
    PasteAction pasteAction = new PasteAction(this, viewer);
    registry.registerAction(pasteAction);
    getSelectionActions().add(pasteAction.getId());

    // Cut
    action = new CutAction(this, pasteAction);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Copy
    action = new CopyAction(this, pasteAction);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Use Grid Action
    action = new ToggleGridEnabledAction();
    registry.registerAction(action);

    // Show Grid Action
    action = new ToggleGridVisibleAction();
    registry.registerAction(action);

    // Snap to Alignment Guides
    action = new ToggleSnapToAlignmentGuidesAction();
    registry.registerAction(action);

    // Ruler
    //IAction showRulers = new ToggleRulerVisibilityAction(getGraphicalViewer());
    //registry.registerAction(showRulers);

    action = new MatchWidthAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());

    action = new MatchHeightAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());

    action = new AlignmentAction((IWorkbenchPart) this, PositionConstants.LEFT);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());

    action = new AlignmentAction((IWorkbenchPart) this, PositionConstants.RIGHT);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());

    action = new AlignmentAction((IWorkbenchPart) this, PositionConstants.TOP);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());

    action = new AlignmentAction((IWorkbenchPart) this, PositionConstants.BOTTOM);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());

    action = new AlignmentAction((IWorkbenchPart) this, PositionConstants.CENTER);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());

    action = new AlignmentAction((IWorkbenchPart) this, PositionConstants.MIDDLE);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());

    // Default Size
    action = new DefaultEditPartSizeAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Reset Aspect Ratio
    action = new ResetAspectRatioAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Properties
    action = new PropertiesAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());

    // Fill Colour
    action = new FillColorAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Connection Line Width
    action = new ConnectionLineWidthAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Connection Line Color
    action = new LineColorAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Font
    action = new FontAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Font Colour
    action = new FontColorAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Export As Image
    action = new ExportAsImageAction(viewer);
    registry.registerAction(action);

    // Export As Image to Clipboard
    action = new ExportAsImageToClipboardAction(viewer);
    registry.registerAction(action);

    // Connection Router types
    action = new ConnectionRouterAction.BendPointConnectionRouterAction(this);
    registry.registerAction(action);
    action = new ConnectionRouterAction.ShortestPathConnectionRouterAction(this);
    registry.registerAction(action);
    action = new ConnectionRouterAction.ManhattanConnectionRouterAction(this);
    registry.registerAction(action);

    // Send Backward
    action = new SendBackwardAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Bring Forward
    action = new BringForwardAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Send to Back
    action = new SendToBackAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Bring To Front
    action = new BringToFrontAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Text Alignment Actions
    for (TextAlignmentAction a : TextAlignmentAction.createActions(this)) {
        registry.registerAction(a);
        getSelectionActions().add(a.getId());
        getUpdateCommandStackActions().add(a);
    }

    // Text Position Actions
    for (TextPositionAction a : TextPositionAction.createActions(this)) {
        registry.registerAction(a);
        getSelectionActions().add(a.getId());
        getUpdateCommandStackActions().add(a);
    }

    // Lock Object
    action = new LockObjectAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Border Color
    action = new BorderColorAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    getUpdateCommandStackActions().add((UpdateAction) action);

    // Full Screen
    if (!PlatformUtils.supportsMacFullScreen()) {
        action = new FullScreenAction(this);
        registry.registerAction(action);
    }

    // Select Element in Tree
    action = new SelectElementInTreeAction(this);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
}

From source file:com.bdaum.zoom.ui.internal.views.BasicView.java

License:Open Source License

protected void registerCommand(IAction action, String commandId) {
    if (action != null) {
        IHandlerService service = getSite().getService(IHandlerService.class);
        if (service != null) {
            action.setActionDefinitionId(commandId);
            activations.add(service.activateHandler(commandId, new ActionHandler(action)));
        }/* w  ww  .ja v  a 2  s .co m*/
    }
}

From source file:com.bdaum.zoom.ui.internal.views.TagCloudView.java

License:Open Source License

protected void registerCommand(IAction action, String commandId) {
    IHandlerService service = getSite().getService(IHandlerService.class);
    if (service != null) {
        action.setActionDefinitionId(commandId);
        service.activateHandler(commandId, new ActionHandler(action));
    }/* w w  w .j a  v a 2 s.  c  o  m*/
}

From source file:com.google.dart.tools.ui.actions.GenerateActionGroup.java

License:Open Source License

private void installQuickAccessAction() {
    fHandlerService = (IHandlerService) fSite.getService(IHandlerService.class);
    if (fHandlerService != null) {
        fQuickAccessAction = new SourceQuickAccessAction(fEditor);
        fQuickAccessHandlerActivation = fHandlerService.activateHandler(
                fQuickAccessAction.getActionDefinitionId(), new ActionHandler(fQuickAccessAction));
    }/*from ww  w.  j a  v  a2s.  com*/
}

From source file:com.google.dart.tools.ui.internal.libraryview.LibraryExplorerActionGroup.java

License:Open Source License

private void setGlobalActionHandlers(IActionBars actionBars) {

    // These are not necessary because there is no change in behavior for these actions
    // for each different view.
    //actionBars.setGlobalActionHandler(RunInBrowserAction.ACTION_ID, runInBrowserAction);
    //actionBars.setGlobalActionHandler(CloseLibraryAction.ID, closeLibraryAction);

    IHandlerService handlerService = (IHandlerService) part.getViewSite().getService(IHandlerService.class);
    handlerService.activateHandler(IWorkbenchCommandConstants.NAVIGATE_TOGGLE_LINK_WITH_EDITOR,
            new ActionHandler(toggleLinkingAction));
    handlerService.activateHandler(CollapseAllHandler.COMMAND_ID, new ActionHandler(collapseAllAction));
}

From source file:com.idega.app.eplatform.BrowserView.java

License:Open Source License

private Browser createBrowser(Composite parent, final IActionBars actionBars) {

    if (BrowserApp.LOCATIONBAR_ENABLED) {
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;/*from www  .j av  a 2s .  co  m*/
        parent.setLayout(gridLayout);
    }
    GridData data = null;

    if (BrowserApp.LOCATIONBAR_ENABLED) {

        data = new GridData();
        data = new GridData();
        Label labelAddress = new Label(parent, SWT.NONE);
        labelAddress.setText("A&ddress");

        location = new Text(parent, SWT.BORDER);
        data.horizontalAlignment = GridData.FILL;
        data.grabExcessHorizontalSpace = true;
        location.setLayoutData(data);
    }

    browser = new Browser(parent, SWT.NONE);
    data = new GridData();
    data.horizontalAlignment = GridData.FILL;
    data.verticalAlignment = GridData.FILL;
    data.horizontalSpan = 2;
    data.grabExcessHorizontalSpace = true;
    data.grabExcessVerticalSpace = true;
    browser.setLayoutData(data);

    browser.addProgressListener(new ProgressAdapter() {
        IProgressMonitor monitor = actionBars.getStatusLineManager().getProgressMonitor();
        boolean working = false;
        int workedSoFar;

        public void changed(ProgressEvent event) {
            if (DEBUG) {
                System.out.println("changed: " + event.current + "/" + event.total);
            }
            if (event.total == 0)
                return;
            if (!working) {
                if (event.current == event.total)
                    return;
                monitor.beginTask("", event.total); //$NON-NLS-1$
                workedSoFar = 0;
                working = true;
            }
            monitor.worked(event.current - workedSoFar);
            workedSoFar = event.current;
        }

        public void completed(ProgressEvent event) {
            if (DEBUG) {
                System.out.println("completed: " + event.current + "/" + event.total);
            }
            monitor.done();
            working = false;
        }
    });
    browser.addStatusTextListener(new StatusTextListener() {
        IStatusLineManager status = actionBars.getStatusLineManager();

        public void changed(StatusTextEvent event) {
            if (DEBUG) {
                System.out.println("status: " + event.text);
            }
            status.setMessage(event.text);
        }
    });
    if (BrowserApp.LOCATIONBAR_ENABLED) {
        browser.addLocationListener(new LocationAdapter() {
            public void changed(LocationEvent event) {
                if (event.top)
                    location.setText(event.location);
            }
        });
    }
    browser.addTitleListener(new TitleListener() {
        public void changed(TitleEvent event) {
            setPartName(event.title);
        }
    });
    browser.addOpenWindowListener(new OpenWindowListener() {
        public void open(WindowEvent event) {
            BrowserView.this.openWindow(event);
        }
    });
    // TODO: should handle VisibilityWindowListener.show and .hide events
    browser.addCloseWindowListener(new CloseWindowListener() {
        public void close(WindowEvent event) {
            BrowserView.this.close();
        }
    });
    if (BrowserApp.LOCATIONBAR_ENABLED) {
        location.addSelectionListener(new SelectionAdapter() {
            public void widgetDefaultSelected(SelectionEvent e) {
                browser.setUrl(location.getText());
            }
        });
    }

    // Hook the navigation actons as handlers for the retargetable actions
    // defined in BrowserActionBuilder.
    actionBars.setGlobalActionHandler("back", backAction); //$NON-NLS-1$
    actionBars.setGlobalActionHandler("forward", forwardAction); //$NON-NLS-1$
    actionBars.setGlobalActionHandler("stop", stopAction); //$NON-NLS-1$
    actionBars.setGlobalActionHandler("refresh", refreshAction); //$NON-NLS-1$
    actionBars.setGlobalActionHandler("openinbrowser", openInBrowserAction); //$NON-NLS-1$

    // Register the easter egg action with the key binding service,
    // allowing it to be invoked directly via keypress, 
    // without requiring the action to be visible in the UI.
    // Note that the address field needs to have focus for this to work, 
    // or any control other than the browser widget, due to 
    // <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=69919">bug 69919</a>.
    IHandlerService hs = (IHandlerService) getSite().getService(IHandlerService.class);
    IHandler easterHandler = new ActionHandler(easterEggAction);
    hs.activateHandler(easterEggAction.getActionDefinitionId(), easterHandler);
    return browser;
}