Search in sources :

Example 36 with DBNDatabaseNode

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

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

the class NavigatorHandlerObjectCreateCopy method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final ISelection selection = HandlerUtil.getCurrentSelection(event);
    DBNNode curNode = NavigatorUtils.getSelectedNode(selection);
    if (curNode != null) {
        Clipboard clipboard = new Clipboard(Display.getDefault());
        try {
            @SuppressWarnings("unchecked") Collection<DBNNode> cbNodes = (Collection<DBNNode>) clipboard.getContents(TreeNodeTransfer.getInstance());
            if (cbNodes != null) {
                for (DBNNode nodeObject : cbNodes) {
                    if (nodeObject instanceof DBNDatabaseNode) {
                        createNewObject(HandlerUtil.getActiveWorkbenchWindow(event), curNode, ((DBNDatabaseNode) nodeObject));
                    } else if (nodeObject instanceof DBNResource && curNode instanceof DBNResource) {
                        pasteResource((DBNResource) nodeObject, (DBNResource) curNode);
                    }
                }
            } else if (curNode instanceof DBNResource) {
                String[] files = (String[]) clipboard.getContents(FileTransfer.getInstance());
                if (files != null) {
                    for (String fileName : files) {
                        final File file = new File(fileName);
                        if (file.exists()) {
                            pasteResource(file, (DBNResource) curNode);
                        }
                    }
                } else {
                    DBUserInterface.getInstance().showError("Paste error", "Unsupported clipboard format. File or folder were expected.");
                }
            } else {
                DBUserInterface.getInstance().showError("Paste error", "Clipboard contains data in unsupported format");
            }
        } finally {
            clipboard.dispose();
        }
    }
    return null;
}
Also used : DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ISelection(org.eclipse.jface.viewers.ISelection) Collection(java.util.Collection) Clipboard(org.eclipse.swt.dnd.Clipboard) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) IFile(org.eclipse.core.resources.IFile)

Example 38 with DBNDatabaseNode

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

the class NavigatorHandlerObjectGoto method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    DBCExecutionContext context = null;
    DBSObject container = null;
    IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    if (activePart instanceof DBPContextProvider) {
        context = ((DBPContextProvider) activePart).getExecutionContext();
    } else if (activePart instanceof INavigatorModelView) {
        final ISelection selection = HandlerUtil.getCurrentSelection(event);
        if (selection instanceof IStructuredSelection) {
            Object element = ((IStructuredSelection) selection).getFirstElement();
            if (element instanceof DBSWrapper) {
                DBSObject object = ((DBSWrapper) element).getObject();
                if (object != null) {
                    container = object;
                    while (container instanceof DBSFolder) {
                        container = container.getParentObject();
                    }
                    DBPDataSource dataSource = object.getDataSource();
                    if (dataSource != null) {
                        context = dataSource.getDefaultContext(true);
                    }
                }
            }
        }
    }
    if (context == null) {
        DBUserInterface.getInstance().showError("Go to object", "No active datasource");
        return null;
    }
    IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
    GotoObjectDialog dialog = new GotoObjectDialog(HandlerUtil.getActiveShell(event), context, container);
    dialog.open();
    Object[] objectsToOpen = dialog.getResult();
    if (!ArrayUtils.isEmpty(objectsToOpen)) {
        Collection<DBNDatabaseNode> nodes = NavigatorHandlerObjectBase.getNodesByObjects(Arrays.asList(objectsToOpen));
        for (DBNDatabaseNode node : nodes) {
            NavigatorUtils.openNavigatorNode(node, workbenchWindow);
        }
    }
    return null;
}
Also used : DBSFolder(org.jkiss.dbeaver.model.struct.DBSFolder) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DBPContextProvider(org.jkiss.dbeaver.model.DBPContextProvider) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) GotoObjectDialog(org.jkiss.dbeaver.ui.dialogs.GotoObjectDialog) INavigatorModelView(org.jkiss.dbeaver.ui.navigator.INavigatorModelView) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 39 with DBNDatabaseNode

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

the class NavigatorHandlerRefresh method refreshNavigator.

public static void refreshNavigator(final Collection<? extends DBNNode> refreshObjects) {
    Job refreshJob = new AbstractJob("Refresh navigator object(s)") {

        public Throwable error;

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            addJobChangeListener(new JobChangeAdapter() {

                @Override
                public void done(IJobChangeEvent event) {
                    if (error != null) {
                        DBUserInterface.getInstance().showError("Refresh", "Error refreshing node", error);
                    }
                }
            });
            Set<DBNNode> refreshedSet = new HashSet<>();
            for (DBNNode node : refreshObjects) {
                if (node.isDisposed() || node.isLocked()) {
                    // Skip locked nodes
                    continue;
                }
                // Check this node was already refreshed
                if (!refreshedSet.isEmpty()) {
                    boolean skip = false;
                    for (DBNNode refreshed : refreshedSet) {
                        if (node == refreshed || node.isChildOf(refreshed)) {
                            skip = true;
                            break;
                        }
                    }
                    if (skip) {
                        continue;
                    }
                }
                // Check for dirty editor (some local changes) and ask for confirmation
                if (node instanceof DBNDatabaseFolder && !(node.getParentNode() instanceof DBNDatabaseFolder) && node.getParentNode() instanceof DBNDatabaseNode) {
                    // USe parent if this node is a folder
                    node = node.getParentNode();
                }
                IEditorPart nodeEditor = NavigatorHandlerObjectOpen.findEntityEditor(DBeaverUI.getActiveWorkbenchWindow(), node);
                if (nodeEditor != null && nodeEditor.isDirty()) {
                    if (!new UIConfirmation() {

                        @Override
                        protected Boolean runTask() {
                            return ConfirmationDialog.showConfirmDialog(null, DBeaverPreferences.CONFIRM_ENTITY_REVERT, ConfirmationDialog.QUESTION, nodeEditor.getTitle()) == IDialogConstants.YES_ID;
                        }
                    }.execute()) {
                        continue;
                    }
                }
                setName("Refresh '" + node.getNodeName() + "'...");
                try {
                    DBNNode refreshed = node.refreshNode(monitor, DBNEvent.FORCE_REFRESH);
                    if (refreshed != null) {
                        refreshedSet.add(refreshed);
                    }
                } catch (Throwable ex) {
                    error = ex;
                }
            }
            return Status.OK_STATUS;
        }
    };
    refreshJob.setUser(true);
    refreshJob.schedule();
}
Also used : UIConfirmation(org.jkiss.dbeaver.ui.UIConfirmation) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) IEditorPart(org.eclipse.ui.IEditorPart) DBNDatabaseFolder(org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) Job(org.eclipse.core.runtime.jobs.Job) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 40 with DBNDatabaseNode

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

the class CursorViewDialog method openNewContainer.

@Override
public void openNewContainer(DBRProgressMonitor monitor, DBSDataContainer dataContainer, DBDDataFilter newFilter) {
    final DBNDatabaseNode targetNode = getExecutionContext().getDataSource().getContainer().getPlatform().getNavigatorModel().getNodeByObject(monitor, dataContainer, false);
    if (targetNode == null) {
        UIUtils.showMessageBox(null, "Open link", "Can't navigate to '" + DBUtils.getObjectFullName(dataContainer, DBPEvaluationContext.UI) + "' - navigator node not found", SWT.ICON_ERROR);
        return;
    }
    AbstractDataEditor.openNewDataEditor(targetNode, newFilter);
}
Also used : DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Aggregations

DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)165 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)64 DBException (org.jkiss.dbeaver.DBException)60 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)60 InvocationTargetException (java.lang.reflect.InvocationTargetException)38 ArrayList (java.util.ArrayList)29 GridData (org.eclipse.swt.layout.GridData)27 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)21 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)21 DBNModel (org.jkiss.dbeaver.model.navigator.DBNModel)20 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)20 ISelection (org.eclipse.jface.viewers.ISelection)16 DBNDatabaseFolder (org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder)15 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)13 SelectionEvent (org.eclipse.swt.events.SelectionEvent)13 DBXTreeNode (org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode)13 Collection (java.util.Collection)12 Composite (org.eclipse.swt.widgets.Composite)12 DBNDataSource (org.jkiss.dbeaver.model.navigator.DBNDataSource)12 IEditorPart (org.eclipse.ui.IEditorPart)11