Search in sources :

Example 31 with DBECommandContext

use of org.jkiss.dbeaver.model.edit.DBECommandContext in project dbeaver by dbeaver.

the class EntityEditor method createPages.

@Override
protected void createPages() {
    super.createPages();
    final IDatabaseEditorInput editorInput = getEditorInput();
    if (editorInput instanceof DatabaseLazyEditorInput) {
        try {
            addPage(new ProgressEditorPart(this), editorInput);
            setPageText(0, "Initializing ...");
            Image tabImage = DBeaverIcons.getImage(UIIcon.REFRESH);
            setPageImage(0, tabImage);
            setActivePage(0);
            ((CTabFolder) getContainer()).setTabHeight(tabImage.getBounds().height + 2);
        } catch (PartInitException e) {
            log.error(e);
        }
        return;
    } else if (editorInput instanceof ErrorEditorInput) {
        ErrorEditorInput errorInput = (ErrorEditorInput) editorInput;
        try {
            addPage(new ErrorEditorPartEx(errorInput.getError()), errorInput);
            setPageImage(0, UIUtils.getShardImage(ISharedImages.IMG_OBJS_ERROR_TSK));
            setPageText(0, "Error");
            setActivePage(0);
        } catch (PartInitException e) {
            log.error(e);
        }
        return;
    }
    // Command listener
    commandListener = new DBECommandAdapter() {

        @Override
        public void onCommandChange(DBECommand<?> command) {
            UIUtils.syncExec(() -> firePropertyChange(IEditorPart.PROP_DIRTY));
        }
    };
    DBECommandContext commandContext = getCommandContext();
    if (commandContext != null) {
        commandContext.addCommandListener(commandListener);
    }
    // Property listener
    addPropertyListener((source, propId) -> {
        if (propId == IEditorPart.PROP_DIRTY) {
            EntityEditorPropertyTester.firePropertyChange(EntityEditorPropertyTester.PROP_DIRTY);
            EntityEditorPropertyTester.firePropertyChange(EntityEditorPropertyTester.PROP_CAN_UNDO);
            EntityEditorPropertyTester.firePropertyChange(EntityEditorPropertyTester.PROP_CAN_REDO);
        }
    });
    DBSObject databaseObject = editorInput.getDatabaseObject();
    EditorDefaults editorDefaults = null;
    if (databaseObject == null) {
        // Weird
        log.debug("Null database object in EntityEditor");
    } else {
        synchronized (defaultPageMap) {
            editorDefaults = defaultPageMap.get(databaseObject.getClass().getName());
        }
        EntityEditorsRegistry editorsRegistry = EntityEditorsRegistry.getInstance();
        // Add object editor page
        EntityEditorDescriptor defaultEditor = editorsRegistry.getMainEntityEditor(databaseObject, this);
        hasPropertiesEditor = false;
        if (defaultEditor != null) {
            hasPropertiesEditor = addEditorTab(defaultEditor);
        }
        if (hasPropertiesEditor) {
            DBNNode node = editorInput.getNavigatorNode();
            int propEditorIndex = getPageCount() - 1;
            setPageText(propEditorIndex, UINavigatorMessages.editors_entity_properties_text);
            setPageToolTip(propEditorIndex, node.getNodeType() + UINavigatorMessages.editors_entity_properties_tooltip_suffix);
            setPageImage(propEditorIndex, DBeaverIcons.getImage(node.getNodeIconDefault()));
        }
    }
    // Add contributed pages
    addContributions(EntityEditorDescriptor.POSITION_PROPS);
    addContributions(EntityEditorDescriptor.POSITION_START);
    addContributions(EntityEditorDescriptor.POSITION_MIDDLE);
    // Add contributed pages
    addContributions(EntityEditorDescriptor.POSITION_END);
    String defPageId = editorInput.getDefaultPageId();
    String defFolderId = editorInput.getDefaultFolderId();
    if (defPageId == null && editorDefaults != null) {
        defPageId = editorDefaults.pageId;
    }
    if (defPageId != null) {
        IEditorPart defEditorPage = editorMap.get(defPageId);
        if (defEditorPage != null) {
            setActiveEditor(defEditorPage);
        }
    } else {
        setActiveEditor(getEditor(0));
    }
    this.activeEditor = getActiveEditor();
    if (activeEditor instanceof ITabbedFolderContainer) {
        if (defFolderId == null && editorDefaults != null) {
            defFolderId = editorDefaults.folderId;
        }
        if (defFolderId != null) {
            String folderId = defFolderId;
            UIUtils.asyncExec(() -> {
                ((ITabbedFolderContainer) activeEditor).switchFolder(folderId);
            });
        }
    }
    UIUtils.setHelp(getContainer(), IHelpContextIds.CTX_ENTITY_EDITOR);
}
Also used : CTabFolder(org.eclipse.swt.custom.CTabFolder) Image(org.eclipse.swt.graphics.Image) Point(org.eclipse.swt.graphics.Point) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) ITabbedFolderContainer(org.jkiss.dbeaver.ui.controls.folders.ITabbedFolderContainer) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext) DBECommandAdapter(org.jkiss.dbeaver.model.impl.edit.DBECommandAdapter)

Example 32 with DBECommandContext

use of org.jkiss.dbeaver.model.edit.DBECommandContext in project dbeaver by dbeaver.

the class UndoChangesHandler method updateElement.

@Override
public void updateElement(UIElement element, Map parameters) {
    IWorkbenchWindow workbenchWindow = element.getServiceLocator().getService(IWorkbenchWindow.class);
    if (workbenchWindow == null || workbenchWindow.getActivePage() == null) {
        return;
    }
    final IEditorPart activeEditor = workbenchWindow.getActivePage().getActiveEditor();
    if (activeEditor instanceof EntityEditor) {
        final DBECommandContext commandContext = ((EntityEditor) activeEditor).getCommandContext();
        String text = WorkbenchMessages.Workbench_undo;
        if (commandContext != null && commandContext.getUndoCommand() != null) {
            text += " " + commandContext.getUndoCommand().getTitle();
        }
        element.setText(text);
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext) EntityEditor(org.jkiss.dbeaver.ui.editors.entity.EntityEditor) IEditorPart(org.eclipse.ui.IEditorPart)

Example 33 with DBECommandContext

use of org.jkiss.dbeaver.model.edit.DBECommandContext in project dbeaver by dbeaver.

the class NavigatorObjectsDeleter method appendScript.

private void appendScript(final DBRProgressMonitor monitor, final StringBuilder sql, final DBNDatabaseNode node) throws InvocationTargetException {
    if (!(node.getParentNode() instanceof DBNContainer)) {
        return;
    }
    final DBSObject object = node.getObject();
    if (object == null) {
        return;
    }
    final DBEObjectMaker objectMaker = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(object.getClass(), DBEObjectMaker.class);
    if (objectMaker == null) {
        return;
    }
    final boolean supportsCascade = (objectMaker.getMakerOptions(object.getDataSource()) & DBEObjectMaker.FEATURE_DELETE_CASCADE) != 0;
    final NavigatorHandlerObjectBase.CommandTarget commandTarget;
    try {
        commandTarget = NavigatorHandlerObjectBase.getCommandTarget(window, node.getParentNode(), object.getClass(), false);
    } catch (DBException e) {
        log.warn(e);
        return;
    }
    if (commandContext == null) {
        commandContext = commandTarget.getContext();
    }
    if (!object.isPersisted() || commandTarget.getEditor() != null) {
        return;
    }
    final Map<String, Object> deleteOptions;
    if (supportsCascade && deleteCascade) {
        deleteOptions = OPTIONS_CASCADE;
    } else {
        deleteOptions = Collections.emptyMap();
    }
    try {
        objectMaker.deleteObject(commandTarget.getContext(), node.getObject(), deleteOptions);
    } catch (DBException e) {
        log.warn(e);
        return;
    }
    final StringBuilder script = new StringBuilder();
    final DBECommandContext commandContext = commandTarget.getContext();
    Collection<? extends DBECommand> commands = commandContext.getFinalCommands();
    try {
        for (DBECommand command : commands) {
            final DBEPersistAction[] persistActions = command.getPersistActions(monitor, commandContext.getExecutionContext(), deleteOptions);
            script.append(SQLUtils.generateScript(commandContext.getExecutionContext().getDataSource(), persistActions, false));
            if (script.length() == 0) {
                script.append(SQLUtils.generateComments(commandContext.getExecutionContext().getDataSource(), persistActions, false));
            }
        }
    } catch (DBException e) {
        throw new InvocationTargetException(e);
    }
    commandTarget.getContext().resetChanges(true);
    if (sql.length() != 0) {
        sql.append("\n");
    }
    sql.append(script);
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBECommand(org.jkiss.dbeaver.model.edit.DBECommand) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Aggregations

DBECommandContext (org.jkiss.dbeaver.model.edit.DBECommandContext)33 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 DBException (org.jkiss.dbeaver.DBException)7 IUndoManager (org.eclipse.jface.text.IUndoManager)6 DBECommand (org.jkiss.dbeaver.model.edit.DBECommand)6 DBEPersistAction (org.jkiss.dbeaver.model.edit.DBEPersistAction)6 Point (org.eclipse.swt.graphics.Point)5 IEditorPart (org.eclipse.ui.IEditorPart)4 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)4 DBECommandAdapter (org.jkiss.dbeaver.model.impl.edit.DBECommandAdapter)4 EntityEditor (org.jkiss.dbeaver.ui.editors.entity.EntityEditor)4 DBCException (org.jkiss.dbeaver.model.exec.DBCException)3 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)3 CTabFolder (org.eclipse.swt.custom.CTabFolder)2 Image (org.eclipse.swt.graphics.Image)2 PostgreForeignTableManager (org.jkiss.dbeaver.ext.postgresql.edit.PostgreForeignTableManager)2 PostgreTableColumnManager (org.jkiss.dbeaver.ext.postgresql.edit.PostgreTableColumnManager)2 DBPNamedObject (org.jkiss.dbeaver.model.DBPNamedObject)2 DBEObjectMaker (org.jkiss.dbeaver.model.edit.DBEObjectMaker)2