Search in sources :

Example 11 with SQLEditor

use of org.jkiss.dbeaver.ui.editors.sql.SQLEditor in project dbeaver by serge-rider.

the class DBeaverStackRenderer method populateFileMenu.

private void populateFileMenu(@NotNull final Menu menu, @NotNull final IWorkbenchPart workbenchPart, @Nullable final IFile inputFile, @NotNull final File file) {
    IWorkbenchPage activePage = workbenchPart.getSite().getWorkbenchWindow().getActivePage();
    if (activePage.getActiveEditor() != workbenchPart) {
        activePage.activate(workbenchPart);
    }
    new MenuItem(menu, SWT.SEPARATOR);
    {
        MenuItem menuItemOpenFolder = new MenuItem(menu, SWT.NONE);
        menuItemOpenFolder.setText(CoreMessages.editor_file_open_in_explorer);
        menuItemOpenFolder.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                if (file.getParentFile().isDirectory()) {
                    UIUtils.launchProgram(file.getParentFile().getAbsolutePath());
                }
            }
        });
    }
    {
        MenuItem menuItemOthers = new MenuItem(menu, SWT.NONE);
        menuItemOthers.setText(CoreMessages.editor_file_copy_path);
        menuItemOthers.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                String filePath = file.getAbsolutePath();
                UIUtils.setClipboardContents(Display.getCurrent(), TextTransfer.getInstance(), filePath);
            }
        });
    }
    {
        {
            String deleteText = ActionUtils.findCommandName(SQLEditorCommands.CMD_SQL_DELETE_THIS_SCRIPT);
            // $NON-NLS-1$
            String shortcut = ActionUtils.findCommandDescription(SQLEditorCommands.CMD_SQL_DELETE_THIS_SCRIPT, workbenchPart.getSite(), true);
            if (shortcut != null) {
                deleteText += "\t" + shortcut;
            }
            MenuItem menuItemDelete = new MenuItem(menu, SWT.NONE);
            menuItemDelete.setText(deleteText);
            menuItemDelete.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    ActionUtils.runCommand(SQLEditorCommands.CMD_SQL_DELETE_THIS_SCRIPT, workbenchPart.getSite());
                }
            });
        }
        if (inputFile != null) {
            MenuItem menuItemOthers = new MenuItem(menu, SWT.NONE);
            String renameText = CoreMessages.editor_file_rename;
            if (workbenchPart instanceof SQLEditor) {
                // $NON-NLS-1$
                renameText += "\t" + ActionUtils.findCommandDescription(SQLEditorCommands.CMD_SQL_RENAME, workbenchPart.getSite(), true);
            }
            menuItemOthers.setText(renameText);
            menuItemOthers.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    // $NON-NLS-1$
                    SQLEditorHandlerRenameFile.renameFile(workbenchPart, inputFile, "file");
                }
            });
        }
    }
}
Also used : SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) MenuItem(org.eclipse.swt.widgets.MenuItem)

Example 12 with SQLEditor

use of org.jkiss.dbeaver.ui.editors.sql.SQLEditor in project dbeaver by serge-rider.

the class SQLEditorHandlerMaximizeResultsPanel method updateElement.

@Override
public void updateElement(UIElement element, Map parameters) {
    IWorkbenchWindow workbenchWindow = element.getServiceLocator().getService(IWorkbenchWindow.class);
    if (workbenchWindow == null || workbenchWindow.getActivePage() == null) {
        return;
    }
    IEditorPart activeEditor = workbenchWindow.getActivePage().getActiveEditor();
    if (activeEditor == null) {
        return;
    }
    SQLEditor editor = RuntimeUtils.getObjectAdapter(activeEditor, SQLEditor.class);
    if (editor != null) {
        if (editor.hasMaximizedControl()) {
            element.setText(SQLEditorMessages.action_menu_sqleditor_restoreResultsPanel);
        } else {
            element.setText(SQLEditorMessages.action_menu_sqleditor_maximizeResultsPanel);
        }
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) IEditorPart(org.eclipse.ui.IEditorPart)

Example 13 with SQLEditor

use of org.jkiss.dbeaver.ui.editors.sql.SQLEditor in project dbeaver by serge-rider.

the class OpenLinkInWindowHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    SQLEditor editor = RuntimeUtils.getObjectAdapter(HandlerUtil.getActiveEditor(event), SQLEditor.class);
    if (editor == null) {
        DBWorkbench.getPlatformUI().showError(TITLE, "No suitable editor was found for SQL");
        return null;
    }
    ISelection selection = editor.getSelectionProvider().getSelection();
    if (isSelectedTextNullOrEmpty(selection)) {
        DBWorkbench.getPlatformUI().showError(TITLE, "No text was selected");
        return null;
    }
    TextSelection textSelection = (TextSelection) selection;
    String googleLink = SEARCH_WEB_ADDRESS_PREFIX + textSelection.getText().replaceAll(" ", "%20").trim();
    if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
        UIUtils.launchProgram(googleLink);
    } else {
        DBWorkbench.getPlatformUI().showError(TITLE, "Desktop is not supported.");
    }
    return null;
}
Also used : SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection)

Example 14 with SQLEditor

use of org.jkiss.dbeaver.ui.editors.sql.SQLEditor in project dbeaver by serge-rider.

the class SQLCommandInclude method handleCommand.

@Override
public boolean handleCommand(SQLControlCommand command, final SQLScriptContext scriptContext) throws DBException {
    String fileName = command.getParameter();
    if (CommonUtils.isEmpty(fileName)) {
        throw new DBException("Empty input file");
    }
    fileName = GeneralUtils.replaceVariables(fileName, new ScriptVariablesResolver(scriptContext)).trim();
    fileName = DBUtils.getUnQuotedIdentifier(scriptContext.getExecutionContext().getDataSource(), fileName);
    File curFile = scriptContext.getSourceFile();
    File incFile = curFile == null ? new File(fileName) : new File(curFile.getParent(), fileName);
    if (!incFile.exists()) {
        incFile = new File(fileName);
    }
    if (!incFile.exists()) {
        throw new DBException("File '" + fileName + "' not found");
    }
    final String fileContents;
    try (InputStream is = new FileInputStream(incFile)) {
        Reader reader = new InputStreamReader(is, getResourceEncoding());
        fileContents = IOUtils.readToString(reader);
    } catch (IOException e) {
        throw new DBException("IO error reading file '" + fileName + "'", e);
    }
    final File finalIncFile = incFile;
    final boolean[] statusFlag = new boolean[1];
    UIUtils.syncExec(() -> {
        try {
            final IWorkbenchWindow workbenchWindow = UIUtils.getActiveWorkbenchWindow();
            final IncludeEditorInput input = new IncludeEditorInput(finalIncFile, fileContents);
            SQLEditor sqlEditor = SQLEditorHandlerOpenEditor.openSQLConsole(workbenchWindow, new SQLNavigatorContext(scriptContext.getExecutionContext()), input);
            final IncludeScriptListener scriptListener = new IncludeScriptListener(workbenchWindow, sqlEditor, statusFlag);
            boolean execResult = sqlEditor.processSQL(false, true, null, scriptListener);
            if (!execResult) {
                statusFlag[0] = true;
            }
        } catch (Throwable e) {
            log.error(e);
            statusFlag[0] = true;
        }
    });
    // Wait until script finished
    while (!statusFlag[0]) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            break;
        }
    }
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) SQLNavigatorContext(org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext) ScriptVariablesResolver(org.jkiss.dbeaver.model.sql.eval.ScriptVariablesResolver)

Example 15 with SQLEditor

use of org.jkiss.dbeaver.ui.editors.sql.SQLEditor in project dbeaver by serge-rider.

the class WorkbenchContextListener method activatePartContexts.

void activatePartContexts(IWorkbenchPart part) {
    IContextService contextService = PlatformUI.getWorkbench().getService(IContextService.class);
    if (contextService == null) {
        return;
    }
    try {
        contextService.deferUpdates(true);
        if (part instanceof INavigatorModelView) {
        // We check for instanceof (do not use adapter) because otherwise it become active
        // for all entity editor and clashes with SQL editor and other complex stuff.
        // if (activationNavigator != null) {
        // //log.debug("Double activation of navigator context");
        // contextService.deactivateContext(activationNavigator);
        // }
        // activationNavigator = contextService.activateContext(INavigatorModelView.NAVIGATOR_CONTEXT_ID);
        }
        if (part instanceof SQLEditorBase || part.getAdapter(SQLEditorBase.class) != null) {
            if (activationSQL != null) {
                // log.debug("Double activation of SQL context");
                contextService.deactivateContext(activationSQL);
            }
            activationSQL = contextService.activateContext(SQLEditorContributions.SQL_EDITOR_CONTEXT);
        }
        if (part.getAdapter(ResultSetViewer.class) != null || (part instanceof SQLEditor) || (part instanceof EntityEditor && ((EntityEditor) part).getDatabaseObject() instanceof DBSDataContainer)) {
            if (activationResults != null) {
                contextService.deactivateContext(activationResults);
            }
            activationResults = contextService.activateContext(RESULTS_CONTEXT_ID);
        }
        // Refresh auto-commit element state (#3315)
        // Refresh OpenSeparateConnection
        ActionUtils.fireCommandRefresh(ConnectionCommands.CMD_TOGGLE_AUTOCOMMIT, SQLEditorCommands.CMD_TOGGLE_SEPARATE_CONNECTION);
    } finally {
        contextService.deferUpdates(false);
    }
}
Also used : SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) SQLEditorBase(org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase) IContextService(org.eclipse.ui.contexts.IContextService) INavigatorModelView(org.jkiss.dbeaver.ui.navigator.INavigatorModelView) EntityEditor(org.jkiss.dbeaver.ui.editors.entity.EntityEditor) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer) ResultSetViewer(org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer)

Aggregations

SQLEditor (org.jkiss.dbeaver.ui.editors.sql.SQLEditor)23 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)5 IFile (org.eclipse.core.resources.IFile)4 IEditorPart (org.eclipse.ui.IEditorPart)4 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)4 TextSelection (org.eclipse.jface.text.TextSelection)3 ISelection (org.eclipse.jface.viewers.ISelection)3 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)3 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)3 SQLQueryTransformerCount (org.jkiss.dbeaver.model.impl.sql.SQLQueryTransformerCount)3 SQLQueryTransformerExpression (org.jkiss.dbeaver.model.impl.sql.SQLQueryTransformerExpression)3 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)2 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 MenuItem (org.eclipse.swt.widgets.MenuItem)2 NotNull (org.jkiss.code.NotNull)2 DBException (org.jkiss.dbeaver.DBException)2