Search in sources :

Example 21 with DBNNode

use of org.jkiss.dbeaver.model.navigator.DBNNode 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 22 with DBNNode

use of org.jkiss.dbeaver.model.navigator.DBNNode 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 23 with DBNNode

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

the class ItemListControl method fillCustomActions.

@Override
protected void fillCustomActions(IContributionManager contributionManager) {
    super.fillCustomActions(contributionManager);
    final DBNNode rootNode = getRootNode();
    if (rootNode instanceof DBNDatabaseFolder && ((DBNDatabaseFolder) rootNode).getItemsMeta() != null) {
        contributionManager.add(new Action("Filter", DBeaverIcons.getImageDescriptor(UIIcon.FILTER)) {

            @Override
            public void run() {
                NavigatorHandlerFilterConfig.configureFilters(getShell(), rootNode);
            }
        });
    }
    IWorkbenchSite workbenchSite = getWorkbenchSite();
    if (workbenchSite != null) {
        contributionManager.add(ActionUtils.makeCommandContribution(workbenchSite, IWorkbenchCommandConstants.FILE_REFRESH));
    }
    if (rootNode instanceof DBNDatabaseNode) {
        contributionManager.add(new Separator());
        contributionManager.add(ActionUtils.makeCommandContribution(workbenchSite, CoreCommands.CMD_OBJECT_OPEN));
        contributionManager.add(ActionUtils.makeCommandContribution(workbenchSite, CoreCommands.CMD_OBJECT_CREATE));
        contributionManager.add(ActionUtils.makeCommandContribution(workbenchSite, CoreCommands.CMD_OBJECT_DELETE));
    }
    if (rootNode instanceof DBNDatabaseNode && rootNode.isPersisted()) {
        boolean hasReorder = false;
        List<Class<?>> childrenTypes = ((DBNDatabaseNode) rootNode).getChildrenTypes(null);
        for (Class<?> chilType : childrenTypes) {
            if (EntityEditorsRegistry.getInstance().getObjectManager(chilType, DBEObjectReorderer.class) != null) {
                hasReorder = true;
                break;
            }
        }
        if (hasReorder) {
            contributionManager.add(new Separator());
            contributionManager.add(ActionUtils.makeCommandContribution(workbenchSite, CoreCommands.CMD_OBJECT_MOVE_UP));
            contributionManager.add(ActionUtils.makeCommandContribution(workbenchSite, CoreCommands.CMD_OBJECT_MOVE_DOWN));
        }
    }
    if (workbenchSite instanceof MultiPageEditorSite) {
        final MultiPageEditorPart editor = ((MultiPageEditorSite) workbenchSite).getMultiPageEditor();
        if (editor instanceof EntityEditor) {
            contributionManager.add(new Separator());
            contributionManager.add(ActionUtils.makeCommandContribution(workbenchSite, IWorkbenchCommandConstants.FILE_SAVE, null, UIIcon.SAVE, null, true));
            contributionManager.add(ActionUtils.makeCommandContribution(workbenchSite, IWorkbenchCommandConstants.FILE_REVERT, null, UIIcon.RESET, null, true));
        }
    }
}
Also used : IWorkbenchSite(org.eclipse.ui.IWorkbenchSite) Action(org.eclipse.jface.action.Action) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) MultiPageEditorPart(org.eclipse.ui.part.MultiPageEditorPart) DBNDatabaseFolder(org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder) DBEObjectReorderer(org.jkiss.dbeaver.model.edit.DBEObjectReorderer) EntityEditor(org.jkiss.dbeaver.ui.editors.entity.EntityEditor) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) MultiPageEditorSite(org.eclipse.ui.part.MultiPageEditorSite) Separator(org.eclipse.jface.action.Separator)

Example 24 with DBNNode

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

the class DatabaseNavigatorContentProvider method getChildren.

@Override
public Object[] getChildren(final Object parent) {
    if (parent instanceof TreeLoadNode) {
        return null;
    }
    if (!(parent instanceof DBNNode)) {
        log.error("Bad parent type: " + parent);
        return null;
    }
    //view.getNavigatorModel().findNode(parent);
    final DBNNode parentNode = (DBNNode) parent;
    /*
        if (parentNode == null) {
            log.error("Can't find parent node '" + ((DBSObject) parent).getName() + "' in model");
            return EMPTY_CHILDREN;
        }
*/
    if (!parentNode.hasChildren(true)) {
        return EMPTY_CHILDREN;
    }
    if (parentNode instanceof DBNDatabaseNode && ((DBNDatabaseNode) parentNode).needsInitialization()) {
        if (navigatorTree.isFiltering()) {
            return EMPTY_CHILDREN;
        }
        return TreeLoadVisualizer.expandChildren(navigatorTree.getViewer(), new TreeLoadService("Loading", ((DBNDatabaseNode) parentNode)));
    } else {
        try {
            // Read children with null monitor cos' it's not a lazy node
            // and no blocking process will occur
            DBNNode[] children = NavigatorUtils.getNodeChildrenFiltered(VoidProgressMonitor.INSTANCE, parentNode, true);
            if (ArrayUtils.isEmpty(children)) {
                return EMPTY_CHILDREN;
            } else {
                return children;
            }
        } catch (Throwable ex) {
            UIUtils.showErrorDialog(navigatorTree.getViewer().getControl().getShell(), "Navigator error", ex.getMessage(), ex);
            // Collapse this item
            DBeaverUI.asyncExec(new Runnable() {

                @Override
                public void run() {
                    navigatorTree.getViewer().collapseToLevel(parent, 1);
                    navigatorTree.getViewer().refresh(parent);
                }
            });
            return EMPTY_CHILDREN;
        }
    }
}
Also used : TreeLoadNode(org.jkiss.dbeaver.ui.navigator.database.load.TreeLoadNode) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) TreeLoadService(org.jkiss.dbeaver.ui.navigator.database.load.TreeLoadService) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 25 with DBNNode

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

the class TreeLoadVisualizer method expandChildren.

public static Object[] expandChildren(AbstractTreeViewer viewer, TreeLoadService service) {
    DBNNode parent = service.getParentNode();
    TreeLoadNode placeHolder = TreeLoadNode.createPlaceHolder(parent);
    if (placeHolder != null && TreeLoadNode.canBeginLoading(parent)) {
        TreeLoadVisualizer visualizer = new TreeLoadVisualizer(viewer, placeHolder, parent);
        LoadingJob.createService(service, visualizer).schedule();
        return new Object[] { placeHolder };
    }
    return EMPTY_ELEMENT_ARRAY;
}
Also used : DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode)

Aggregations

DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)40 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)19 DBException (org.jkiss.dbeaver.DBException)13 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)12 ISelection (org.eclipse.jface.viewers.ISelection)9 ArrayList (java.util.ArrayList)6 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)5 GridData (org.eclipse.swt.layout.GridData)5 DBNDatabaseFolder (org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder)5 DBNResource (org.jkiss.dbeaver.model.navigator.DBNResource)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 IFile (org.eclipse.core.resources.IFile)4 DBNDataSource (org.jkiss.dbeaver.model.navigator.DBNDataSource)4 DBNModel (org.jkiss.dbeaver.model.navigator.DBNModel)4 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)4 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)3 SelectionEvent (org.eclipse.swt.events.SelectionEvent)3 DBNDatabaseItem (org.jkiss.dbeaver.model.navigator.DBNDatabaseItem)3 CSmartCombo (org.jkiss.dbeaver.ui.controls.CSmartCombo)3 IProject (org.eclipse.core.resources.IProject)2