Search in sources :

Example 36 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.

the class NavigatorHandlerLinkEditor method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final IWorkbenchPage activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
    final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    if (activeEditor == null) {
        return null;
    }
    NavigatorViewBase navigatorView = NavigatorUtils.getActiveNavigatorView(event);
    if (navigatorView == null) {
        return null;
    }
    if (navigatorView instanceof ProjectExplorerView) {
        if (activeEditor instanceof SQLEditor) {
            IFile file = EditorUtils.getFileFromInput(activeEditor.getEditorInput());
            if (file != null) {
                showResourceInNavigator(navigatorView, file);
            }
        } else if (activeEditor.getEditorInput() instanceof ProjectFileEditorInput) {
            IFile editorFile = ((ProjectFileEditorInput) activeEditor.getEditorInput()).getFile();
            showResourceInNavigator(navigatorView, editorFile);
        }
    } else if (activeEditor.getEditorInput() instanceof IDatabaseEditorInput) {
        IDatabaseEditorInput editorInput = (IDatabaseEditorInput) activeEditor.getEditorInput();
        DBNNode dbnNode = editorInput.getNavigatorNode();
        if (dbnNode != null) {
            navigatorView.showNode(dbnNode);
        }
    } else if (activeEditor instanceof IDataSourceContainerProvider) {
        DBPDataSourceContainer dsContainer = ((IDataSourceContainerProvider) activeEditor).getDataSourceContainer();
        @NotNull final DBSObject activeObject;
        if (dsContainer != null) {
            DBPDataSource dataSource = dsContainer.getDataSource();
            if (dataSource != null) {
                activeObject = DBUtils.getDefaultOrActiveObject(dataSource);
            } else {
                activeObject = dsContainer;
            }
            final NavigatorViewBase view = navigatorView;
            DBeaverUI.runInUI(activePage.getWorkbenchWindow(), new DBRRunnableWithProgress() {

                @Override
                public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    DBSObject showObject = activeObject;
                    if (showObject instanceof DBPDataSource) {
                        showObject = ((DBPDataSource) showObject).getContainer();
                    }
                    DBNDatabaseNode objectNode = view.getModel().getNodeByObject(monitor, showObject, true);
                    if (objectNode != null) {
                        view.showNode(objectNode);
                    }
                }
            });
        }
    }
    activePage.activate(navigatorView);
    return null;
}
Also used : SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) IDataSourceContainerProvider(org.jkiss.dbeaver.model.IDataSourceContainerProvider) ProjectExplorerView(org.jkiss.dbeaver.ui.navigator.project.ProjectExplorerView) IFile(org.eclipse.core.resources.IFile) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ProjectFileEditorInput(org.jkiss.dbeaver.ui.editors.ProjectFileEditorInput) IEditorPart(org.eclipse.ui.IEditorPart) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) NotNull(org.jkiss.code.NotNull) InvocationTargetException(java.lang.reflect.InvocationTargetException) IDatabaseEditorInput(org.jkiss.dbeaver.ui.editors.IDatabaseEditorInput) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) NavigatorViewBase(org.jkiss.dbeaver.ui.navigator.database.NavigatorViewBase) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 37 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.

the class NavigatorHandlerObjectBase method getCommandTarget.

protected static CommandTarget getCommandTarget(IWorkbenchWindow workbenchWindow, DBNContainer container, Class<?> childType, boolean openEditor) throws DBException {
    final Object parentObject = container.getValueObject();
    DBSObject objectToSeek = null;
    if (parentObject instanceof DBSObject) {
        final DBEStructEditor parentStructEditor = EntityEditorsRegistry.getInstance().getObjectManager(parentObject.getClass(), DBEStructEditor.class);
        if (parentStructEditor != null && RuntimeUtils.isTypeSupported(childType, parentStructEditor.getChildTypes())) {
            objectToSeek = (DBSObject) parentObject;
        }
    }
    if (objectToSeek != null) {
        for (final IEditorReference editorRef : workbenchWindow.getActivePage().getEditorReferences()) {
            final IEditorPart editor = editorRef.getEditor(false);
            if (editor instanceof IDatabaseEditor) {
                final IDatabaseEditorInput editorInput = ((IDatabaseEditor) editor).getEditorInput();
                if (editorInput.getDatabaseObject() == objectToSeek) {
                    workbenchWindow.getActivePage().activate(editor);
                    switchEditorFolder(container, editor);
                    return new CommandTarget((IDatabaseEditor) editor);
                }
            }
        }
        if (openEditor && container instanceof DBNDatabaseNode) {
            final IDatabaseEditor editor = (IDatabaseEditor) NavigatorHandlerObjectOpen.openEntityEditor((DBNDatabaseNode) container, null, workbenchWindow);
            if (editor != null) {
                switchEditorFolder(container, editor);
                return new CommandTarget(editor);
            }
        }
    }
    if (container instanceof DBNDatabaseNode) {
        // No editor found - create new command context
        DBPDataSource dataSource = ((DBNDatabaseNode) container).getObject().getDataSource();
        if (dataSource != null) {
            return new CommandTarget(new SimpleCommandContext(dataSource.getDefaultContext(true), !openEditor));
        }
    }
    return new CommandTarget();
}
Also used : IDatabaseEditorInput(org.jkiss.dbeaver.ui.editors.IDatabaseEditorInput) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IEditorReference(org.eclipse.ui.IEditorReference) SimpleCommandContext(org.jkiss.dbeaver.ui.SimpleCommandContext) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBEStructEditor(org.jkiss.dbeaver.model.edit.DBEStructEditor) IEditorPart(org.eclipse.ui.IEditorPart) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) IDatabaseEditor(org.jkiss.dbeaver.ui.editors.IDatabaseEditor)

Example 38 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.

the class NavigatorHandlerObjectBase method getNodesByObjects.

public static Collection<DBNDatabaseNode> getNodesByObjects(Collection<Object> objects) {
    DBNModel model = DBeaverCore.getInstance().getNavigatorModel();
    List<DBNDatabaseNode> result = new ArrayList<>();
    List<Object> missingObjects = new ArrayList<>();
    for (Object object : objects) {
        if (object instanceof DBSObject) {
            DBNDatabaseNode node = model.findNode((DBSObject) object);
            if (node != null) {
                result.add(node);
                continue;
            }
        }
        missingObjects.add(object);
    }
    if (!missingObjects.isEmpty()) {
        NodeLoader nodeLoader = new NodeLoader(model, missingObjects);
        try {
            DBeaverUI.runInProgressService(nodeLoader);
        } catch (InvocationTargetException e) {
            log.warn("Can't load node for objects " + missingObjects.size(), e.getTargetException());
        } catch (InterruptedException e) {
        // do nothing
        }
        result.addAll(nodeLoader.nodes);
    }
    return result;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBNModel(org.jkiss.dbeaver.model.navigator.DBNModel)

Example 39 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.

the class NavigatorHandlerObjectCreateBase method createNewObject.

protected boolean createNewObject(final IWorkbenchWindow workbenchWindow, DBNNode element, DBNDatabaseNode copyFrom) {
    try {
        DBNContainer container = null;
        if (element instanceof DBNContainer && !(element instanceof DBNDataSource)) {
            container = (DBNContainer) element;
        } else {
            DBNNode parentNode = element.getParentNode();
            if (parentNode instanceof DBNContainer) {
                container = (DBNContainer) parentNode;
            }
        }
        if (container == null) {
            throw new DBException("Can't detect container for '" + element.getNodeName() + "'");
        }
        Class<?> childType = container.getChildrenClass();
        if (childType == null) {
            throw new DBException("Can't determine child element type for container '" + container + "'");
        }
        if (childType == IProject.class) {
            return NavigatorHandlerProjectCreate.createNewProject(workbenchWindow);
        }
        DBSObject sourceObject = copyFrom == null ? null : copyFrom.getObject();
        // Do not check for type - manager must do it. Potentially we can copy anything into anything.
        // if (sourceObject != null && !childType.isAssignableFrom(sourceObject.getClass())) {
        // throw new DBException("Can't create '" + childType.getName() + "' from '" + sourceObject.getClass().getName() + "'");
        // }
        final EntityEditorsRegistry editorsRegistry = EntityEditorsRegistry.getInstance();
        DBEObjectManager<?> objectManager = editorsRegistry.getObjectManager(childType);
        if (objectManager == null) {
            throw new DBException("Object manager not found for type '" + childType.getName() + "'");
        }
        DBEObjectMaker objectMaker = (DBEObjectMaker) objectManager;
        DBPDataSource dataSource = container instanceof DBNDatabaseNode ? ((DBNDatabaseNode) container).getDataSource() : null;
        final boolean openEditor = dataSource != null && (objectMaker.getMakerOptions(dataSource) & DBEObjectMaker.FEATURE_EDITOR_ON_CREATE) != 0;
        CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, childType, openEditor);
        // Parent is model object - not node
        final Object parentObject = container.getValueObject();
        if (parentObject instanceof DBPObject) {
            createDatabaseObject(commandTarget, objectMaker, (DBPObject) parentObject, sourceObject);
        } else {
            throw new DBException("Parent object type is not supported: " + parentObject);
        }
    } catch (Throwable e) {
        DBUserInterface.getInstance().showError("Create object", null, e);
        return false;
    }
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) DBNContainer(org.jkiss.dbeaver.model.navigator.DBNContainer) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) EntityEditorsRegistry(org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPScriptObject(org.jkiss.dbeaver.model.DBPScriptObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPRefreshableObject(org.jkiss.dbeaver.model.DBPRefreshableObject) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 40 with DBSObject

use of org.jkiss.dbeaver.model.struct.DBSObject in project dbeaver by dbeaver.

the class NavigatorHandlerObjectDelete method deleteObject.

private boolean deleteObject(IWorkbenchWindow workbenchWindow, DBNDatabaseNode node) {
    try {
        if (!(node.getParentNode() instanceof DBNContainer)) {
            throw new DBException("Node '" + node + "' doesn't have a container");
        }
        final DBNContainer container = (DBNContainer) node.getParentNode();
        // Try to delete object using object manager
        DBSObject object = node.getObject();
        if (object == null) {
            throw new DBException("Can't delete node with null object");
        }
        DBEObjectMaker objectMaker = EntityEditorsRegistry.getInstance().getObjectManager(object.getClass(), DBEObjectMaker.class);
        if (objectMaker == null) {
            // $NON-NLS-2$
            throw new DBException("Object maker not found for type '" + object.getClass().getName() + "'");
        }
        boolean supportsCascade = (objectMaker.getMakerOptions(object.getDataSource()) & DBEObjectMaker.FEATURE_DELETE_CASCADE) != 0;
        CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, object.getClass(), false);
        if (deleteAll == null || !deleteAll) {
            this.deleteOptions.clear();
        }
        ConfirmResult confirmResult = ConfirmResult.YES;
        if (!object.isPersisted() || commandTarget.getEditor() != null) {
            // There should be command context somewhere
            if (deleteNewObject(workbenchWindow, node)) {
                return true;
            }
        // No direct editor host found for this object -
        // try to find corresponding command context
        // and execute command within it
        } else {
            // Persisted object - confirm delete
            // Show "View script" only if we are not in some editor (because it have its own "View script" button)
            confirmResult = confirmObjectDelete(workbenchWindow, node, supportsCascade, deleteOptions, commandTarget.getContext() != null && commandTarget.getEditor() == null);
            if (confirmResult == ConfirmResult.NO) {
                return false;
            }
        }
        objectMaker.deleteObject(commandTarget.getContext(), node.getObject(), deleteOptions);
        if (confirmResult == ConfirmResult.DETAILS) {
            if (!showScript(workbenchWindow, commandTarget.getContext(), deleteOptions, CoreMessages.actions_navigator_delete_script)) {
                commandTarget.getContext().resetChanges();
                // Show confirmation again
                return deleteObject(workbenchWindow, node);
            }
        }
        if (commandTarget.getEditor() == null && commandTarget.getContext() != null) {
            // Persist object deletion - only if there is no host editor and we have a command context
            ObjectSaver deleter = new ObjectSaver(commandTarget.getContext(), deleteOptions);
            // DBeaverUI.runInProgressDialog(deleter);
            tasksToExecute.add(deleter);
        }
    } catch (Throwable e) {
        DBUserInterface.getInstance().showError(CoreMessages.actions_navigator_error_dialog_delete_object_title, "Can't delete object '" + node.getNodeName() + "'", e);
        return false;
    }
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker)

Aggregations

DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)374 DBException (org.jkiss.dbeaver.DBException)129 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)66 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)66 ArrayList (java.util.ArrayList)54 InvocationTargetException (java.lang.reflect.InvocationTargetException)52 ISelection (org.eclipse.jface.viewers.ISelection)46 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)46 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)44 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)38 GridData (org.eclipse.swt.layout.GridData)38 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)38 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)30 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)29 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)29 DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)28 Composite (org.eclipse.swt.widgets.Composite)27 IEditorPart (org.eclipse.ui.IEditorPart)26 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)24 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)22