Search in sources :

Example 6 with DBNContainer

use of org.jkiss.dbeaver.model.navigator.DBNContainer in project dbeaver by serge-rider.

the class NavigatorHandlerObjectRename method renameDatabaseObject.

public static boolean renameDatabaseObject(IWorkbenchWindow workbenchWindow, DBNDatabaseNode node, String newName) {
    try {
        if (node.getParentNode() instanceof DBNContainer) {
            DBSObject object = node.getObject();
            if (object != null) {
                DBEObjectRenamer objectRenamer = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(object.getClass(), DBEObjectRenamer.class);
                if (objectRenamer != null) {
                    CommandTarget commandTarget = getCommandTarget(workbenchWindow, node.getParentNode(), object.getClass(), false);
                    objectRenamer.renameObject(commandTarget.getContext(), object, newName);
                    if (object.isPersisted() && commandTarget.getEditor() == null) {
                        Map<String, Object> options = DBPScriptObject.EMPTY_OPTIONS;
                        if (!showScript(workbenchWindow, commandTarget.getContext(), options, "Rename script")) {
                            commandTarget.getContext().resetChanges(true);
                            return false;
                        } else {
                            ObjectSaver renamer = new ObjectSaver(commandTarget.getContext(), options);
                            TasksJob.runTask("Rename object '" + object.getName() + "'", renamer);
                        }
                    } else {
                        for (DBECommand command : commandTarget.getContext().getFinalCommands()) {
                        // System.out.println(command);
                        }
                    }
                    return true;
                }
            }
        }
    } catch (Throwable e) {
        DBWorkbench.getPlatformUI().showError("Rename object", "Can't rename object '" + node.getNodeName() + "'", e);
        return false;
    }
    return false;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBECommand(org.jkiss.dbeaver.model.edit.DBECommand) DBNContainer(org.jkiss.dbeaver.model.navigator.DBNContainer) DBEObjectRenamer(org.jkiss.dbeaver.model.edit.DBEObjectRenamer) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPScriptObject(org.jkiss.dbeaver.model.DBPScriptObject)

Example 7 with DBNContainer

use of org.jkiss.dbeaver.model.navigator.DBNContainer in project dbeaver by serge-rider.

the class ModelPropertyTester method test.

@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
    if (!(receiver instanceof DBSObject)) {
        return false;
    }
    Display display = Display.getCurrent();
    if (display == null) {
        return false;
    }
    switch(property) {
        case PROP_CHILD_OF_TYPE:
            {
                {
                    DBSObject object = (DBSObject) receiver;
                    if (object instanceof DBSObjectContainer) {
                        if (expectedValue instanceof String) {
                            try {
                                Class<?> expectedChildClass = Class.forName((String) expectedValue);
                                Class<? extends DBSObject> childType = ((DBSObjectContainer) object).getPrimaryChildType(null);
                                return expectedChildClass.isAssignableFrom(childType);
                            } catch (Exception e) {
                                return false;
                            }
                        }
                    }
                }
                return false;
            }
        case PROP_IS_TABLE_CONTAINER:
            {
                DBSObject object = DBUtils.getPublicObject((DBSObject) receiver);
                if (object instanceof DBNContainer) {
                    Object valueObject = ((DBNContainer) object).getValueObject();
                    if (valueObject instanceof DBSObject) {
                        object = (DBSObject) valueObject;
                    }
                }
                if (object instanceof DBSSchema) {
                    return true;
                }
                if (object instanceof DBSObjectContainer) {
                    try {
                        Class<? extends DBSObject> primaryChildType = ((DBSObjectContainer) object).getPrimaryChildType(null);
                        if (DBSDataContainer.class.isAssignableFrom(primaryChildType)) {
                            return true;
                        }
                    } catch (DBException e) {
                        log.debug(e);
                    }
                }
                return false;
            }
    }
    return false;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBNContainer(org.jkiss.dbeaver.model.navigator.DBNContainer) DBException(org.jkiss.dbeaver.DBException) DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer) Display(org.eclipse.swt.widgets.Display)

Example 8 with DBNContainer

use of org.jkiss.dbeaver.model.navigator.DBNContainer in project dbeaver by serge-rider.

the class NavigatorHandlerObjectCreateBase method createNewObject.

protected boolean createNewObject(final IWorkbenchWindow workbenchWindow, DBNNode element, DBNDatabaseNode copyFrom) {
    try {
        DBNContainer container = null;
        if (element instanceof DBNContainer) {
            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;
        final boolean openEditor = (objectMaker.getMakerOptions() & DBEObjectMaker.FEATURE_EDITOR_ON_CREATE) != 0;
        CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, childType, openEditor);
        final Object parentObject = container.getValueObject();
        createDatabaseObject(commandTarget, objectMaker, parentObject, sourceObject);
    } catch (Throwable e) {
        UIUtils.showErrorDialog(workbenchWindow.getShell(), "Create object", null, e);
        return false;
    }
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNContainer(org.jkiss.dbeaver.model.navigator.DBNContainer) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPRefreshableObject(org.jkiss.dbeaver.model.DBPRefreshableObject) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker) EntityEditorsRegistry(org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry)

Example 9 with DBNContainer

use of org.jkiss.dbeaver.model.navigator.DBNContainer in project dbeaver by serge-rider.

the class NavigatorHandlerObjectMove method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final ISelection selection = HandlerUtil.getCurrentSelection(event);
    DBNNode node = NavigatorUtils.getSelectedNode(selection);
    if (node == null || !(node.getParentNode() instanceof DBNContainer)) {
        return null;
    }
    DBNContainer containerNode = (DBNContainer) node.getParentNode();
    DBSObject object = ((DBNDatabaseNode) node).getObject();
    if (!(object instanceof DBPOrderedObject)) {
        return null;
    }
    @SuppressWarnings("unchecked") DBEObjectReorderer<DBSObject> objectReorderer = EntityEditorsRegistry.getInstance().getObjectManager(object.getClass(), DBEObjectReorderer.class);
    if (objectReorderer == null) {
        return null;
    }
    DBPOrderedObject orderedObject = (DBPOrderedObject) object;
    try {
        // Sibling objects - they are involved in reordering process
        List<DBSObject> siblingObjects = new ArrayList<>();
        for (DBNNode siblingNode : node.getParentNode().getChildren(VoidProgressMonitor.INSTANCE)) {
            if (siblingNode instanceof DBNDatabaseNode) {
                DBSObject siblingObject = ((DBNDatabaseNode) siblingNode).getObject();
                if (siblingObject.getClass() != object.getClass()) {
                    log.warn("Sibling object class " + siblingObject.getClass() + " differs from moving object class " + object.getClass().getName());
                } else {
                    siblingObjects.add(siblingObject);
                }
            } else {
                log.warn("Wrong sibling node type: " + siblingNode);
            }
        }
        CommandTarget commandTarget = getCommandTarget(HandlerUtil.getActiveWorkbenchWindow(event), containerNode, object.getClass(), false);
        String actionId = event.getCommand().getId();
        switch(actionId) {
            case CoreCommands.CMD_OBJECT_MOVE_UP:
                objectReorderer.setObjectOrdinalPosition(commandTarget.getContext(), object, siblingObjects, orderedObject.getOrdinalPosition() - 1);
                break;
            case CoreCommands.CMD_OBJECT_MOVE_DOWN:
                objectReorderer.setObjectOrdinalPosition(commandTarget.getContext(), object, siblingObjects, orderedObject.getOrdinalPosition() + 1);
                break;
        }
        if (object.isPersisted() && commandTarget.getEditor() == null) {
            if (!showScript(HandlerUtil.getActiveWorkbenchWindow(event), commandTarget.getContext(), "Reorder script")) {
                commandTarget.getContext().resetChanges();
                return false;
            } else {
                ObjectSaver orderer = new ObjectSaver(commandTarget.getContext());
                TasksJob.runTask("Change object '" + object.getName() + "' position", orderer);
            }
        }
    } catch (DBException e) {
        UIUtils.showErrorDialog(HandlerUtil.getActiveShell(event), "Object move", "Error during object reposition", e);
    }
    return null;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPOrderedObject(org.jkiss.dbeaver.model.DBPOrderedObject) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNContainer(org.jkiss.dbeaver.model.navigator.DBNContainer) ArrayList(java.util.ArrayList) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) ISelection(org.eclipse.jface.viewers.ISelection) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 10 with DBNContainer

use of org.jkiss.dbeaver.model.navigator.DBNContainer in project dbeaver by dbeaver.

the class NavigatorHandlerObjectMove method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final ISelection selection = HandlerUtil.getCurrentSelection(event);
    DBNNode node = NavigatorUtils.getSelectedNode(selection);
    if (node == null || !(node.getParentNode() instanceof DBNContainer)) {
        return null;
    }
    DBNContainer containerNode = (DBNContainer) node.getParentNode();
    DBSObject object = ((DBNDatabaseNode) node).getObject();
    if (!(object instanceof DBPOrderedObject)) {
        return null;
    }
    @SuppressWarnings("unchecked") DBEObjectReorderer<DBSObject> objectReorderer = EntityEditorsRegistry.getInstance().getObjectManager(object.getClass(), DBEObjectReorderer.class);
    if (objectReorderer == null) {
        return null;
    }
    DBPOrderedObject orderedObject = (DBPOrderedObject) object;
    try {
        // Sibling objects - they are involved in reordering process
        List<DBSObject> siblingObjects = new ArrayList<>();
        for (DBNNode siblingNode : node.getParentNode().getChildren(new VoidProgressMonitor())) {
            if (siblingNode instanceof DBNDatabaseNode) {
                DBSObject siblingObject = ((DBNDatabaseNode) siblingNode).getObject();
                if (siblingObject.getClass() != object.getClass()) {
                    log.warn("Sibling object class " + siblingObject.getClass() + " differs from moving object class " + object.getClass().getName());
                } else {
                    siblingObjects.add(siblingObject);
                }
            } else {
                log.warn("Wrong sibling node type: " + siblingNode);
            }
        }
        CommandTarget commandTarget = getCommandTarget(HandlerUtil.getActiveWorkbenchWindow(event), containerNode, object.getClass(), false);
        String actionId = event.getCommand().getId();
        switch(actionId) {
            case CoreCommands.CMD_OBJECT_MOVE_UP:
                objectReorderer.setObjectOrdinalPosition(commandTarget.getContext(), object, siblingObjects, orderedObject.getOrdinalPosition() - 1);
                break;
            case CoreCommands.CMD_OBJECT_MOVE_DOWN:
                objectReorderer.setObjectOrdinalPosition(commandTarget.getContext(), object, siblingObjects, orderedObject.getOrdinalPosition() + 1);
                break;
        }
        if (object.isPersisted() && commandTarget.getEditor() == null) {
            Map<String, Object> options = DBPScriptObject.EMPTY_OPTIONS;
            if (!showScript(HandlerUtil.getActiveWorkbenchWindow(event), commandTarget.getContext(), options, "Reorder script")) {
                commandTarget.getContext().resetChanges();
                return false;
            } else {
                ObjectSaver orderer = new ObjectSaver(commandTarget.getContext(), options);
                TasksJob.runTask("Change object '" + object.getName() + "' position", orderer);
            }
        }
    } catch (DBException e) {
        DBUserInterface.getInstance().showError("Object move", "Error during object reposition", e);
    }
    return null;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPOrderedObject(org.jkiss.dbeaver.model.DBPOrderedObject) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNContainer(org.jkiss.dbeaver.model.navigator.DBNContainer) ArrayList(java.util.ArrayList) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) ISelection(org.eclipse.jface.viewers.ISelection) DBPOrderedObject(org.jkiss.dbeaver.model.DBPOrderedObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPScriptObject(org.jkiss.dbeaver.model.DBPScriptObject) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Aggregations

DBNContainer (org.jkiss.dbeaver.model.navigator.DBNContainer)12 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)12 DBException (org.jkiss.dbeaver.DBException)8 DBPScriptObject (org.jkiss.dbeaver.model.DBPScriptObject)7 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)6 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)5 ArrayList (java.util.ArrayList)4 ISelection (org.eclipse.jface.viewers.ISelection)4 DBPOrderedObject (org.jkiss.dbeaver.model.DBPOrderedObject)4 DBECommand (org.jkiss.dbeaver.model.edit.DBECommand)4 DBEObjectRenamer (org.jkiss.dbeaver.model.edit.DBEObjectRenamer)4 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)3 Display (org.eclipse.swt.widgets.Display)2 DBPRefreshableObject (org.jkiss.dbeaver.model.DBPRefreshableObject)2 DBEObjectMaker (org.jkiss.dbeaver.model.edit.DBEObjectMaker)2 DBSDataContainer (org.jkiss.dbeaver.model.struct.DBSDataContainer)2 DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)2 DBSSchema (org.jkiss.dbeaver.model.struct.rdb.DBSSchema)2 EntityEditorsRegistry (org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry)2 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)1