Search in sources :

Example 1 with ExecutionEvent

use of org.eclipse.core.commands.ExecutionEvent in project dbeaver by serge-rider.

the class ResultSetCommandHandler method execute.

@Nullable
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final ResultSetViewer rsv = (ResultSetViewer) getActiveResultSet(HandlerUtil.getActivePart(event));
    if (rsv == null) {
        return null;
    }
    boolean shiftPressed = event.getTrigger() instanceof Event && ((((Event) event.getTrigger()).stateMask & SWT.SHIFT) == SWT.SHIFT);
    String actionId = event.getCommand().getId();
    IResultSetPresentation presentation = rsv.getActivePresentation();
    switch(actionId) {
        case IWorkbenchCommandConstants.FILE_REFRESH:
            rsv.refreshData(null);
            break;
        case CMD_TOGGLE_MODE:
            rsv.toggleMode();
            break;
        case CMD_TOGGLE_PANELS:
            rsv.showPanels(!rsv.isPanelsVisible());
            break;
        case CMD_SWITCH_PRESENTATION:
            rsv.switchPresentation();
            break;
        case CMD_ROW_PREVIOUS:
        case ITextEditorActionDefinitionIds.WORD_PREVIOUS:
            presentation.scrollToRow(IResultSetPresentation.RowPosition.PREVIOUS);
            break;
        case CMD_ROW_NEXT:
        case ITextEditorActionDefinitionIds.WORD_NEXT:
            presentation.scrollToRow(IResultSetPresentation.RowPosition.NEXT);
            break;
        case CMD_ROW_FIRST:
        case ITextEditorActionDefinitionIds.SELECT_WORD_PREVIOUS:
            presentation.scrollToRow(IResultSetPresentation.RowPosition.FIRST);
            break;
        case CMD_ROW_LAST:
        case ITextEditorActionDefinitionIds.SELECT_WORD_NEXT:
            presentation.scrollToRow(IResultSetPresentation.RowPosition.LAST);
            break;
        case CMD_FETCH_PAGE:
            rsv.readNextSegment();
            break;
        case CMD_FETCH_ALL:
            rsv.readAllData();
            break;
        case CMD_ROW_EDIT:
            if (presentation instanceof IResultSetEditor) {
                ((IResultSetEditor) presentation).openValueEditor(false);
            }
            break;
        case CMD_ROW_EDIT_INLINE:
            if (presentation instanceof IResultSetEditor) {
                ((IResultSetEditor) presentation).openValueEditor(true);
            }
            break;
        case CMD_ROW_ADD:
            rsv.addNewRow(false, shiftPressed);
            break;
        case CMD_ROW_COPY:
            rsv.addNewRow(true, shiftPressed);
            break;
        case CMD_ROW_DELETE:
        case IWorkbenchCommandConstants.EDIT_DELETE:
            rsv.deleteSelectedRows();
            break;
        case CMD_CELL_SET_NULL:
        case CMD_CELL_RESET:
            {
                IResultSetSelection selection = rsv.getSelection();
                for (Object cell : selection.toArray()) {
                    DBDAttributeBinding attr = selection.getElementAttribute(cell);
                    ResultSetRow row = selection.getElementRow(cell);
                    if (row != null && attr != null) {
                        ResultSetValueController valueController = new ResultSetValueController(rsv, attr, row, IValueController.EditType.NONE, null);
                        if (actionId.equals(CMD_CELL_SET_NULL)) {
                            valueController.updateValue(BaseValueManager.makeNullValue(valueController), false);
                        } else {
                            rsv.getModel().resetCellValue(attr, row);
                        }
                    }
                }
                rsv.redrawData(false, false);
                rsv.updatePanelsContent(false);
                break;
            }
        case CMD_APPLY_CHANGES:
            rsv.applyChanges(null);
            break;
        case CMD_REJECT_CHANGES:
            rsv.rejectChanges();
            break;
        case CMD_GENERATE_SCRIPT:
            {
                try {
                    final List<DBEPersistAction> sqlScript = new ArrayList<>();
                    try {
                        DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {

                            @Override
                            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                                List<DBEPersistAction> script = rsv.generateChangesScript(monitor);
                                if (script != null) {
                                    sqlScript.addAll(script);
                                }
                            }
                        });
                    } catch (InterruptedException e) {
                    // ignore
                    }
                    if (!sqlScript.isEmpty()) {
                        String scriptText = SQLUtils.generateScript(rsv.getDataContainer() == null ? null : rsv.getDataContainer().getDataSource(), sqlScript.toArray(new DBEPersistAction[sqlScript.size()]), false);
                        scriptText = SQLUtils.generateCommentLine(rsv.getExecutionContext() == null ? null : rsv.getExecutionContext().getDataSource(), "Actual parameter values may differ, what you see is a default string representation of values") + scriptText;
                        ViewSQLDialog dialog = new ViewSQLDialog(HandlerUtil.getActivePart(event).getSite(), rsv.getExecutionContext(), CoreMessages.editors_entity_dialog_preview_title, UIIcon.SQL_PREVIEW, scriptText);
                        dialog.open();
                    }
                } catch (InvocationTargetException e) {
                    UIUtils.showErrorDialog(HandlerUtil.getActiveShell(event), "Script generation", "Can't generate changes script", e.getTargetException());
                }
                break;
            }
        case CMD_COPY_COLUMN_NAMES:
            {
                StringBuilder buffer = new StringBuilder();
                IResultSetSelection selection = rsv.getSelection();
                Collection<DBDAttributeBinding> attrs = selection.isEmpty() ? rsv.getModel().getVisibleAttributes() : selection.getSelectedAttributes();
                for (DBDAttributeBinding attr : attrs) {
                    if (buffer.length() > 0) {
                        buffer.append("\t");
                    }
                    buffer.append(attr.getName());
                }
                ResultSetUtils.copyToClipboard(buffer.toString());
                break;
            }
        case CMD_COPY_ROW_NAMES:
            {
                StringBuilder buffer = new StringBuilder();
                IResultSetSelection selection = rsv.getSelection();
                for (ResultSetRow row : ((IResultSetSelection) selection).getSelectedRows()) {
                    if (buffer.length() > 0) {
                        buffer.append("\n");
                    }
                    buffer.append(row.getVisualNumber() + 1);
                }
                ResultSetUtils.copyToClipboard(buffer.toString());
                break;
            }
        case IWorkbenchCommandConstants.EDIT_COPY:
            ResultSetUtils.copyToClipboard(presentation.copySelectionToString(new ResultSetCopySettings(false, false, false, true, null, null, DBDDisplayFormat.EDIT)));
            break;
        case IWorkbenchCommandConstants.EDIT_PASTE:
        case CoreCommands.CMD_PASTE_SPECIAL:
            if (presentation instanceof IResultSetEditor) {
                ((IResultSetEditor) presentation).pasteFromClipboard(actionId.equals(CoreCommands.CMD_PASTE_SPECIAL));
            }
            break;
        case IWorkbenchCommandConstants.EDIT_CUT:
            ResultSetUtils.copyToClipboard(presentation.copySelectionToString(new ResultSetCopySettings(false, false, true, true, null, null, DBDDisplayFormat.EDIT)));
            break;
        case IWorkbenchCommandConstants.FILE_PRINT:
            presentation.printResultSet();
            break;
        case ITextEditorActionDefinitionIds.SMART_ENTER:
            if (presentation instanceof IResultSetEditor) {
                ((IResultSetEditor) presentation).openValueEditor(false);
            }
            break;
        case IWorkbenchCommandConstants.EDIT_FIND_AND_REPLACE:
            FindReplaceAction action = new FindReplaceAction(DBeaverActivator.getCoreResourceBundle(), "Editor.FindReplace.", HandlerUtil.getActiveShell(event), rsv.getAdapter(IFindReplaceTarget.class));
            action.run();
            break;
        case CMD_NAVIGATE_LINK:
            final ResultSetRow row = rsv.getCurrentRow();
            final DBDAttributeBinding attr = rsv.getActivePresentation().getCurrentAttribute();
            if (row != null && attr != null) {
                new AbstractJob("Navigate association") {

                    @Override
                    protected IStatus run(DBRProgressMonitor monitor) {
                        try {
                            rsv.navigateAssociation(monitor, attr, row, false);
                        } catch (DBException e) {
                            return GeneralUtils.makeExceptionStatus(e);
                        }
                        return Status.OK_STATUS;
                    }
                }.schedule();
            }
            break;
        case CMD_COUNT:
            rsv.updateRowCount();
            break;
        case IWorkbenchCommandConstants.NAVIGATE_BACKWARD_HISTORY:
            {
                final int hp = rsv.getHistoryPosition();
                if (hp > 0) {
                    rsv.navigateHistory(hp - 1);
                }
                break;
            }
        case IWorkbenchCommandConstants.NAVIGATE_FORWARD_HISTORY:
            {
                final int hp = rsv.getHistoryPosition();
                if (hp < rsv.getHistorySize() - 1) {
                    rsv.navigateHistory(hp + 1);
                }
                break;
            }
        case ITextEditorActionDefinitionIds.LINE_GOTO:
            {
                ResultSetRow currentRow = rsv.getCurrentRow();
                final int rowCount = rsv.getModel().getRowCount();
                if (rowCount <= 0) {
                    break;
                }
                GotoLineDialog d = new GotoLineDialog(HandlerUtil.getActiveShell(event), "Go to Row", "Enter row number (1.." + rowCount + ")", String.valueOf(currentRow == null ? 1 : currentRow.getVisualNumber() + 1), new IInputValidator() {

                    @Override
                    public String isValid(String input) {
                        try {
                            int i = Integer.parseInt(input);
                            if (i <= 0 || rowCount < i) {
                                return "Row number is out of range";
                            }
                        } catch (NumberFormatException x) {
                            return "Not a number";
                        }
                        return null;
                    }
                });
                if (d.open() == Window.OK) {
                    int line = Integer.parseInt(d.getValue());
                    rsv.setCurrentRow(rsv.getModel().getRow(line - 1));
                    rsv.getActivePresentation().scrollToRow(IResultSetPresentation.RowPosition.CURRENT);
                }
                break;
            }
        case CMD_FILTER_MENU:
            {
                rsv.showFiltersMenu();
                break;
            }
    }
    return null;
}
Also used : DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) FindReplaceAction(org.eclipse.ui.texteditor.FindReplaceAction) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) ArrayList(java.util.ArrayList) List(java.util.List) InvocationTargetException(java.lang.reflect.InvocationTargetException) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) ViewSQLDialog(org.jkiss.dbeaver.ui.dialogs.sql.ViewSQLDialog) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) Event(org.eclipse.swt.widgets.Event) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) Collection(java.util.Collection) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IFindReplaceTarget(org.eclipse.jface.text.IFindReplaceTarget) IInputValidator(org.eclipse.jface.dialogs.IInputValidator) Nullable(org.jkiss.code.Nullable)

Example 2 with ExecutionEvent

use of org.eclipse.core.commands.ExecutionEvent in project translationstudio8 by heartsome.

the class ConcordanceSearchHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    if (!isEnabled()) {
        return null;
    }
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    if (editor instanceof IXliffEditor) {
        IXliffEditor xliffEditor = (IXliffEditor) editor;
        String XLIFF_EDITOR_ID = "net.heartsome.cat.ts.ui.xliffeditor.nattable.editor";
        IEditorPart editorRefer = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
        if (editorRefer.getSite().getId().equals(XLIFF_EDITOR_ID)) {
            // IProject project = ((FileEditorInput) editorRefer.getEditorInput()).getFile().getProject();
            IFile file = ((FileEditorInput) editorRefer.getEditorInput()).getFile();
            ProjectConfiger projectConfig = ProjectConfigerFactory.getProjectConfiger(file.getProject());
            List<DatabaseModelBean> lstDatabase = projectConfig.getAllTmDbs();
            if (lstDatabase.size() == 0) {
                MessageDialog.openInformation(HandlerUtil.getActiveShell(event), Messages.getString("handler.ConcordanceSearchHandler.msgTitle"), Messages.getString("handler.ConcordanceSearchHandler.msg"));
                return null;
            }
            String selectText = xliffEditor.getSelectPureText();
            if ((selectText == null || selectText.equals("")) && xliffEditor.getSelectedRowIds().size() == 1) {
                selectText = xliffEditor.getXLFHandler().getSrcPureText(xliffEditor.getSelectedRowIds().get(0));
            } else if (selectText == null) {
                selectText = "";
            }
            String srcLang = xliffEditor.getSrcColumnName();
            String tgtLang = xliffEditor.getTgtColumnName();
            ConcordanceSearchDialog dialog = new ConcordanceSearchDialog(editorRefer.getSite().getShell(), file, srcLang, tgtLang, selectText.trim());
            Language srcLangL = LocaleService.getLanguageConfiger().getLanguageByCode(srcLang);
            Language tgtLangL = LocaleService.getLanguageConfiger().getLanguageByCode(tgtLang);
            dialog.open();
            if (srcLangL.isBidi() || tgtLangL.isBidi()) {
                dialog.getShell().setOrientation(SWT.RIGHT_TO_LEFT);
            }
            if (selectText != null && !selectText.trim().equals("")) {
                dialog.initGroupIdAndSearch();
                IWorkbenchPartSite site = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getSite();
                ICommandService commandService = (ICommandService) site.getService(ICommandService.class);
                Command command = commandService.getCommand(ActionFactory.COPY.getCommandId());
                IEvaluationService evalService = (IEvaluationService) site.getService(IEvaluationService.class);
                IEvaluationContext currentState = evalService.getCurrentState();
                ExecutionEvent executionEvent = new ExecutionEvent(command, Collections.EMPTY_MAP, this, currentState);
                try {
                    command.executeWithChecks(executionEvent);
                } catch (Exception e1) {
                }
            }
        }
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) DatabaseModelBean(net.heartsome.cat.common.bean.DatabaseModelBean) ConcordanceSearchDialog(net.heartsome.cat.database.ui.tm.dialog.ConcordanceSearchDialog) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) IEditorPart(org.eclipse.ui.IEditorPart) IXliffEditor(net.heartsome.cat.ts.ui.editors.IXliffEditor) ICommandService(org.eclipse.ui.commands.ICommandService) ExecutionException(org.eclipse.core.commands.ExecutionException) IWorkbenchPartSite(org.eclipse.ui.IWorkbenchPartSite) Language(net.heartsome.cat.common.locale.Language) ProjectConfiger(net.heartsome.cat.ts.core.file.ProjectConfiger) Command(org.eclipse.core.commands.Command) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IEvaluationService(org.eclipse.ui.services.IEvaluationService)

Example 3 with ExecutionEvent

use of org.eclipse.core.commands.ExecutionEvent in project translationstudio8 by heartsome.

the class ConverterCommandTrigger method openDialog.

private static void openDialog(IWorkbenchWindow window, IFile file, String commandId) {
    IWorkbench workbench = window.getWorkbench();
    ICommandService commandService = (ICommandService) workbench.getService(ICommandService.class);
    Command command = commandService.getCommand(commandId);
    try {
        EvaluationContext context = new EvaluationContext(null, IEvaluationContext.UNDEFINED_VARIABLE);
        context.addVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME, window);
        if (file != null) {
            StructuredSelection selection = new StructuredSelection(file);
            context.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
        }
        command.executeWithChecks(new ExecutionEvent(command, Collections.EMPTY_MAP, null, context));
    } catch (ExecutionException e) {
        LOGGER.error("", e);
    } catch (NotDefinedException e) {
        LOGGER.error("", e);
    } catch (NotEnabledException e) {
        LOGGER.error("", e);
    } catch (NotHandledException e) {
        LOGGER.error("", e);
    }
}
Also used : IWorkbench(org.eclipse.ui.IWorkbench) NotHandledException(org.eclipse.core.commands.NotHandledException) Command(org.eclipse.core.commands.Command) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) EvaluationContext(org.eclipse.core.expressions.EvaluationContext) ExecutionException(org.eclipse.core.commands.ExecutionException) NotEnabledException(org.eclipse.core.commands.NotEnabledException) ICommandService(org.eclipse.ui.commands.ICommandService)

Example 4 with ExecutionEvent

use of org.eclipse.core.commands.ExecutionEvent in project translationstudio8 by heartsome.

the class NewHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    String parameter = event.getParameter("wizardParameter");
    Command command = null;
    ICommandService commandService = (ICommandService) HandlerUtil.getActiveSite(event).getService(ICommandService.class);
    if (parameter == null || parameter.equalsIgnoreCase("project")) {
        command = commandService.getCommand("net.heartsome.cat.ts.ui.command.newProject");
    } else if (parameter.equalsIgnoreCase("folder")) {
        command = commandService.getCommand("net.heartsome.cat.ts.ui.command.newFolder");
    } else if (parameter.equalsIgnoreCase("tm")) {
        command = commandService.getCommand("net.heartsome.cat.database.ui.tm.command.newTM");
    } else if (parameter.equalsIgnoreCase("tb")) {
        command = commandService.getCommand("net.heartsome.cat.database.ui.tb.command.newTB");
    }
    if (command == null) {
        return null;
    }
    IEvaluationService evalService = (IEvaluationService) HandlerUtil.getActiveSite(event).getService(IEvaluationService.class);
    IEvaluationContext currentState = evalService.getCurrentState();
    ExecutionEvent executionEvent = new ExecutionEvent(command, Collections.EMPTY_MAP, this, currentState);
    try {
        command.executeWithChecks(executionEvent);
    } catch (NotDefinedException e) {
        LOGGER.error("", e);
    } catch (NotEnabledException e) {
        LOGGER.error("", e);
    } catch (NotHandledException e) {
        LOGGER.error("", e);
    }
    return null;
}
Also used : NotHandledException(org.eclipse.core.commands.NotHandledException) Command(org.eclipse.core.commands.Command) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) IEvaluationContext(org.eclipse.core.expressions.IEvaluationContext) NotDefinedException(org.eclipse.core.commands.common.NotDefinedException) NotEnabledException(org.eclipse.core.commands.NotEnabledException) ICommandService(org.eclipse.ui.commands.ICommandService) IEvaluationService(org.eclipse.ui.services.IEvaluationService)

Example 5 with ExecutionEvent

use of org.eclipse.core.commands.ExecutionEvent in project dbeaver by serge-rider.

the class CheckForUpdateAction method deactivateStandardHandler.

public static void deactivateStandardHandler(IWorkbenchWindow window) {
    if (p2UpdateHandlerActivation != null) {
        return;
    }
    IHandlerService srv = window.getService(IHandlerService.class);
    p2UpdateHandlerActivation = srv.activateHandler(CheckForUpdateAction.P2_UPDATE_COMMAND, new AbstractHandler() {

        @Override
        public Object execute(ExecutionEvent event) throws ExecutionException {
            new CheckForUpdateAction().run();
            return null;
        }
    });
}
Also used : IHandlerService(org.eclipse.ui.handlers.IHandlerService) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) AbstractHandler(org.eclipse.core.commands.AbstractHandler)

Aggregations

ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)5 Command (org.eclipse.core.commands.Command)3 IEvaluationContext (org.eclipse.core.expressions.IEvaluationContext)3 ICommandService (org.eclipse.ui.commands.ICommandService)3 ExecutionException (org.eclipse.core.commands.ExecutionException)2 NotEnabledException (org.eclipse.core.commands.NotEnabledException)2 NotHandledException (org.eclipse.core.commands.NotHandledException)2 NotDefinedException (org.eclipse.core.commands.common.NotDefinedException)2 IEvaluationService (org.eclipse.ui.services.IEvaluationService)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 DatabaseModelBean (net.heartsome.cat.common.bean.DatabaseModelBean)1 Language (net.heartsome.cat.common.locale.Language)1 ConcordanceSearchDialog (net.heartsome.cat.database.ui.tm.dialog.ConcordanceSearchDialog)1 ProjectConfiger (net.heartsome.cat.ts.core.file.ProjectConfiger)1 IXliffEditor (net.heartsome.cat.ts.ui.editors.IXliffEditor)1 AbstractHandler (org.eclipse.core.commands.AbstractHandler)1 EvaluationContext (org.eclipse.core.expressions.EvaluationContext)1