Search in sources :

Example 1 with DBECommandContext

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

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)

Example 2 with DBECommandContext

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

the class EntityEditor method dispose.

@Override
public void dispose() {
    if (breadcrumbsMenu != null) {
        breadcrumbsMenu.dispose();
        breadcrumbsMenu = null;
    }
    for (Map.Entry<IEditorPart, IEditorActionBarContributor> entry : actionContributors.entrySet()) {
        GlobalContributorManager.getInstance().removeContributor(entry.getValue(), entry.getKey());
    }
    actionContributors.clear();
    DBECommandContext commandContext = getCommandContext();
    if (commandListener != null && commandContext != null) {
        commandContext.removeCommandListener(commandListener);
        commandListener = null;
    }
    super.dispose();
    if (getDatabaseObject() != null && commandContext != null) {
        commandContext.resetChanges(true);
    // // Remove all non-persisted objects
    // for (DBPObject object : getCommandContext().getEditedObjects()) {
    // if (object instanceof DBPPersistedObject && !((DBPPersistedObject)object).isPersisted()) {
    // dataSource.getContainer().fireEvent(new DBPEvent(DBPEvent.Action.OBJECT_REMOVE, (DBSObject) object));
    // }
    // }
    }
    this.editorMap.clear();
    this.activeEditor = null;
}
Also used : DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext)

Example 3 with DBECommandContext

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

the class EntityEditor method redoChanges.

public void redoChanges() {
    IUndoManager undoManager = getAdapter(IUndoManager.class);
    if (undoManager != null) {
        undoManager.redo();
    } else {
        DBECommandContext commandContext = getCommandContext();
        if (commandContext != null && commandContext.getRedoCommand() != null) {
            commandContext.redoCommand();
            firePropertyChange(IEditorPart.PROP_DIRTY);
        }
    }
}
Also used : IUndoManager(org.eclipse.jface.text.IUndoManager) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext)

Example 4 with DBECommandContext

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

the class EntityEditor method refreshPart.

@Override
public void refreshPart(final Object source, boolean force) {
    if (getContainer() == null || getContainer().isDisposed() || isSaveInProgress()) {
        return;
    }
    if (force && isDirty()) {
        if (ConfirmationDialog.showConfirmDialog(ResourceBundle.getBundle(UINavigatorMessages.BUNDLE_NAME), null, NavigatorPreferences.CONFIRM_ENTITY_REVERT, ConfirmationDialog.QUESTION, getTitle()) != IDialogConstants.YES_ID) {
            return;
        }
    }
    if (source instanceof DBNEvent && ((DBNEvent) source).getNodeChange() == DBNEvent.NodeChange.REFRESH) {
    // This may happen if editor was refreshed indirectly (it is a child of refreshed node)
    // force = true;
    }
    if (force && getDatabaseObject().isPersisted()) {
        // Lists and commands should be refreshed only if we make real refresh from remote storage
        // Otherwise just update object's properties
        DBECommandContext commandContext = getCommandContext();
        if (commandContext != null && commandContext.isDirty()) {
            // Just clear command context. Do not undo because object state was already refreshed
            commandContext.resetChanges(true);
        }
    }
    DBSObject databaseObject = getEditorInput().getDatabaseObject();
    if (databaseObject != null) {
        // Refresh visual content in parts
        for (IEditorPart editor : editorMap.values()) {
            if (editor instanceof IRefreshablePart) {
                ((IRefreshablePart) editor).refreshPart(source, force);
            }
        }
    }
    setPartName(getEditorInput().getName());
    setTitleImage(getEditorInput().getImageDescriptor());
    if (hasPropertiesEditor) {
        // Update main editor image
        setPageImage(0, DBeaverIcons.getImage(getEditorInput().getNavigatorNode().getNodeIconDefault()));
    }
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext)

Example 5 with DBECommandContext

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

the class EntityEditor method promptToSaveOnClose.

@Override
public int promptToSaveOnClose() {
    List<String> changedSubEditors = new ArrayList<>();
    final DBECommandContext commandContext = getCommandContext();
    if (commandContext != null && commandContext.isDirty()) {
        changedSubEditors.add(UINavigatorMessages.registry_entity_editor_descriptor_name);
    }
    for (IEditorPart editor : editorMap.values()) {
        if (editor.isDirty()) {
            EntityEditorDescriptor editorDescriptor = EntityEditorsRegistry.getInstance().getEntityEditor(editor);
            if (editorDescriptor != null) {
                changedSubEditors.add(editorDescriptor.getName());
            }
        }
    }
    String subEditorsString = changedSubEditors.isEmpty() ? "" : "(" + String.join(", ", changedSubEditors) + ")";
    final int result = ConfirmationDialog.showConfirmDialog(ResourceBundle.getBundle(UINavigatorMessages.BUNDLE_NAME), getSite().getShell(), NavigatorPreferences.CONFIRM_ENTITY_EDIT_CLOSE, ConfirmationDialog.QUESTION_WITH_CANCEL, getEditorInput().getNavigatorNode().getNodeName(), subEditorsString);
    if (result == IDialogConstants.YES_ID) {
        // getWorkbenchPart().getSite().getPage().saveEditor(this, false);
        return ISaveablePart2.YES;
    } else if (result == IDialogConstants.NO_ID) {
        return ISaveablePart2.NO;
    } else {
        return ISaveablePart2.CANCEL;
    }
}
Also used : DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext) Point(org.eclipse.swt.graphics.Point)

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