Search in sources :

Example 1 with CommandException

use of org.eclipse.core.commands.common.CommandException in project org.csstudio.display.builder by kasemir.

the class SearchView method configureActions.

/**
 * React to selections, button presses, ...
 */
private void configureActions() {
    // Start a channel search
    search.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(final SelectionEvent e) {
            searchForChannels();
        }
    });
    // Channel Table: Allow dragging of PVs with archive
    final Table table = channel_table.getTable();
    new ControlSystemDragSource(table) {

        @Override
        public Object getSelection() {
            final IStructuredSelection selection = (IStructuredSelection) channel_table.getSelection();
            // To allow transfer of array, the data must be
            // of the actual array type, not Object[]
            final Object[] objs = selection.toArray();
            final ChannelInfo[] channels = Arrays.copyOf(objs, objs.length, ChannelInfo[].class);
            return channels;
        }
    };
    // Double-clicks should have the same effect as context menu -> Data Browser
    getSite().setSelectionProvider(channel_table);
    channel_table.addDoubleClickListener(new IDoubleClickListener() {

        @Override
        public void doubleClick(DoubleClickEvent event) {
            // casting is needed for RAP
            IHandlerService handlerService = getSite().getService(IHandlerService.class);
            try {
                handlerService.executeCommand("org.csstudio.trends.databrowser3.OpenDataBrowserPopup", null);
            } catch (CommandException ex) {
                // $NON-NLS-1$
                Activator.getLogger().log(Level.WARNING, "Failed to open data browser", ex);
            }
        }
    });
    // Add context menu for object contributions
    final MenuManager menu = new MenuManager();
    menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
    table.setMenu(menu.createContextMenu(table));
    getSite().registerContextMenu(menu, channel_table);
}
Also used : Table(org.eclipse.swt.widgets.Table) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ChannelInfo(org.csstudio.trends.databrowser3.model.ChannelInfo) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) CommandException(org.eclipse.core.commands.common.CommandException) ControlSystemDragSource(org.csstudio.ui.util.dnd.ControlSystemDragSource) IHandlerService(org.eclipse.ui.handlers.IHandlerService) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) SelectionEvent(org.eclipse.swt.events.SelectionEvent) MenuManager(org.eclipse.jface.action.MenuManager) Separator(org.eclipse.jface.action.Separator)

Example 2 with CommandException

use of org.eclipse.core.commands.common.CommandException in project egit by eclipse.

the class CommonUtils method runCommand.

/**
 * Programatically run command based on it id and given selection
 *
 * @param commandId
 *            id of command that should be run
 * @param selection
 *            given selection
 * @return {@code true} when command was successfully executed,
 *         {@code false} otherwise
 */
public static boolean runCommand(String commandId, IStructuredSelection selection) {
    ICommandService commandService = CommonUtils.getService(PlatformUI.getWorkbench(), ICommandService.class);
    Command cmd = commandService.getCommand(commandId);
    if (!cmd.isDefined())
        return false;
    IHandlerService handlerService = CommonUtils.getService(PlatformUI.getWorkbench(), IHandlerService.class);
    EvaluationContext c = null;
    if (selection != null) {
        c = new EvaluationContext(handlerService.createContextSnapshot(false), selection.toList());
        c.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
        c.removeVariable(ISources.ACTIVE_MENU_SELECTION_NAME);
    }
    try {
        if (c != null)
            handlerService.executeCommandInContext(new ParameterizedCommand(cmd, null), null, c);
        else
            handlerService.executeCommand(commandId, null);
        return true;
    } catch (CommandException ignored) {
    // Ignored
    }
    return false;
}
Also used : IHandlerService(org.eclipse.ui.handlers.IHandlerService) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) EvaluationContext(org.eclipse.core.expressions.EvaluationContext) CommandException(org.eclipse.core.commands.common.CommandException) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) ICommandService(org.eclipse.ui.commands.ICommandService)

Aggregations

CommandException (org.eclipse.core.commands.common.CommandException)2 IHandlerService (org.eclipse.ui.handlers.IHandlerService)2 ChannelInfo (org.csstudio.trends.databrowser3.model.ChannelInfo)1 ControlSystemDragSource (org.csstudio.ui.util.dnd.ControlSystemDragSource)1 Command (org.eclipse.core.commands.Command)1 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)1 EvaluationContext (org.eclipse.core.expressions.EvaluationContext)1 MenuManager (org.eclipse.jface.action.MenuManager)1 Separator (org.eclipse.jface.action.Separator)1 DoubleClickEvent (org.eclipse.jface.viewers.DoubleClickEvent)1 IDoubleClickListener (org.eclipse.jface.viewers.IDoubleClickListener)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 Table (org.eclipse.swt.widgets.Table)1 ICommandService (org.eclipse.ui.commands.ICommandService)1