Search in sources :

Example 1 with DBSObjectSelector

use of org.jkiss.dbeaver.model.struct.DBSObjectSelector in project dbeaver by serge-rider.

the class NavigatorActionSetActiveObject method run.

@Override
public void run(IAction action) {
    if (selection instanceof IStructuredSelection) {
        DBNNode selectedNode = NavigatorUtils.getSelectedNode(selection);
        if (selectedNode instanceof DBNDatabaseNode) {
            final DBNDatabaseNode databaseNode = (DBNDatabaseNode) selectedNode;
            final DBSObjectSelector activeContainer = DBUtils.getParentAdapter(DBSObjectSelector.class, databaseNode.getObject());
            if (activeContainer != null) {
                TasksJob.runTask("Select active object", new DBRRunnableWithProgress() {

                    @Override
                    public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                        try {
                            activeContainer.setDefaultObject(monitor, databaseNode.getObject());
                        } catch (DBException e) {
                            throw new InvocationTargetException(e);
                        }
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBSObjectSelector(org.jkiss.dbeaver.model.struct.DBSObjectSelector) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with DBSObjectSelector

use of org.jkiss.dbeaver.model.struct.DBSObjectSelector in project dbeaver by serge-rider.

the class DataSourceManagementToolbar method changeDataBaseSelection.

private void changeDataBaseSelection() {
    if (databaseCombo == null || databaseCombo.isDisposed() || databaseCombo.getSelectionIndex() < 0) {
        return;
    }
    DBPDataSourceContainer dsContainer = getDataSourceContainer();
    final String newName = databaseCombo.getItemText(databaseCombo.getSelectionIndex());
    if (dsContainer != null && dsContainer.isConnected()) {
        final DBPDataSource dataSource = dsContainer.getDataSource();
        new AbstractJob("Change active database") {

            @Override
            protected IStatus run(DBRProgressMonitor monitor) {
                try {
                    DBSObjectContainer oc = DBUtils.getAdapter(DBSObjectContainer.class, dataSource);
                    DBSObjectSelector os = DBUtils.getAdapter(DBSObjectSelector.class, dataSource);
                    if (os != null) {
                        final DBSObject defObject = os.getDefaultObject();
                        if (defObject instanceof DBSObjectContainer) {
                            // USe seconds level of active object
                            DBSObjectSelector os2 = DBUtils.getAdapter(DBSObjectSelector.class, defObject);
                            if (os2 != null && os2.supportsDefaultChange()) {
                                oc = (DBSObjectContainer) defObject;
                                os = os2;
                            }
                        }
                    }
                    if (oc != null && os != null && os.supportsDefaultChange()) {
                        DBSObject newChild = oc.getChild(monitor, newName);
                        if (newChild != null) {
                            os.setDefaultObject(monitor, newChild);
                        } else {
                            throw new DBException(MessageFormat.format(CoreMessages.toolbar_datasource_selector_error_database_not_found, newName));
                        }
                    } else {
                        throw new DBException(CoreMessages.toolbar_datasource_selector_error_database_change_not_supported);
                    }
                } catch (DBException e) {
                    return GeneralUtils.makeExceptionStatus(e);
                }
                return Status.OK_STATUS;
            }
        }.schedule();
    }
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBSObjectSelector(org.jkiss.dbeaver.model.struct.DBSObjectSelector) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 3 with DBSObjectSelector

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

the class DataSourceManagementToolbar method changeDataBaseSelection.

private void changeDataBaseSelection(DBNDatabaseNode node) {
    DBPDataSourceContainer dsContainer = getDataSourceContainer();
    final String newName = node.getNodeName();
    if (dsContainer != null && dsContainer.isConnected()) {
        final DBPDataSource dataSource = dsContainer.getDataSource();
        new AbstractJob("Change active database") {

            @Override
            protected IStatus run(DBRProgressMonitor monitor) {
                try {
                    DBSObjectContainer oc = DBUtils.getAdapter(DBSObjectContainer.class, dataSource);
                    DBSObjectSelector os = DBUtils.getAdapter(DBSObjectSelector.class, dataSource);
                    if (os != null) {
                        final DBSObject defObject = os.getDefaultObject();
                        if (defObject instanceof DBSObjectContainer) {
                            // USe seconds level of active object
                            DBSObjectSelector os2 = DBUtils.getAdapter(DBSObjectSelector.class, defObject);
                            if (os2 != null && os2.supportsDefaultChange()) {
                                oc = (DBSObjectContainer) defObject;
                                os = os2;
                            }
                        }
                    }
                    if (oc != null && os != null && os.supportsDefaultChange()) {
                        DBSObject newChild = oc.getChild(monitor, newName);
                        if (newChild != null) {
                            os.setDefaultObject(monitor, newChild);
                        } else {
                            throw new DBException(MessageFormat.format(CoreMessages.toolbar_datasource_selector_error_database_not_found, newName));
                        }
                    } else {
                        throw new DBException(CoreMessages.toolbar_datasource_selector_error_database_change_not_supported);
                    }
                } catch (DBException e) {
                    return GeneralUtils.makeExceptionStatus(e);
                }
                return Status.OK_STATUS;
            }
        }.schedule();
    }
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBSObjectSelector(org.jkiss.dbeaver.model.struct.DBSObjectSelector) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 4 with DBSObjectSelector

use of org.jkiss.dbeaver.model.struct.DBSObjectSelector in project dbeaver by serge-rider.

the class NavigatorUtils method isDefaultElement.

public static boolean isDefaultElement(Object element) {
    if (element instanceof DBSWrapper) {
        DBSObject object = ((DBSWrapper) element).getObject();
        DBSObjectSelector activeContainer = DBUtils.getParentAdapter(DBSObjectSelector.class, object);
        if (activeContainer != null) {
            return activeContainer.getDefaultObject() == object;
        }
    } else if (element instanceof DBNProject) {
        if (((DBNProject) element).getProject() == DBeaverCore.getInstance().getProjectRegistry().getActiveProject()) {
            return true;
        }
    }
    return false;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DBSObjectSelector(org.jkiss.dbeaver.model.struct.DBSObjectSelector)

Example 5 with DBSObjectSelector

use of org.jkiss.dbeaver.model.struct.DBSObjectSelector in project dbeaver by serge-rider.

the class NavigatorUtils method createContextMenu.

public static MenuManager createContextMenu(final IWorkbenchSite workbenchSite, final Viewer viewer, final IMenuListener menuListener) {
    final Control control = viewer.getControl();
    final MenuManager menuMgr = new MenuManager();
    Menu menu = menuMgr.createContextMenu(control);
    menu.addMenuListener(new MenuListener() {

        @Override
        public void menuHidden(MenuEvent e) {
        }

        @Override
        public void menuShown(MenuEvent e) {
            Menu m = (Menu) e.widget;
            DBNNode node = getSelectedNode(viewer.getSelection());
            if (node != null && !node.isLocked() && node.allowsOpen()) {
                String commandID = NavigatorUtils.getNodeActionCommand(DBXTreeNodeHandler.Action.open, node, CoreCommands.CMD_OBJECT_OPEN);
                // Get contribution item from menu item and check it's ID
                for (MenuItem item : m.getItems()) {
                    Object itemData = item.getData();
                    if (itemData instanceof IContributionItem) {
                        String contribId = ((IContributionItem) itemData).getId();
                        if (contribId != null && contribId.equals(commandID)) {
                            m.setDefaultItem(item);
                        }
                    }
                }
            }
        }
    });
    menuMgr.addMenuListener(new IMenuListener() {

        @Override
        public void menuAboutToShow(final IMenuManager manager) {
            ViewerColumnController columnController = ViewerColumnController.getFromControl(control);
            if (columnController != null && columnController.isClickOnHeader()) {
                columnController.fillConfigMenu(manager);
                manager.add(new Separator());
                return;
            }
            manager.add(new GroupMarker(MB_NAVIGATOR_ADDITIONS));
            final IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            final DBNNode selectedNode = getSelectedNode(viewer);
            if (selectedNode != null && !selectedNode.isLocked() && workbenchSite != null) {
                // Add "Set active object" menu
                if (selectedNode.isPersisted() && selectedNode instanceof DBNDatabaseNode && !(selectedNode instanceof DBNDatabaseFolder) && ((DBNDatabaseNode) selectedNode).getObject() != null) {
                    final DBSObjectSelector activeContainer = DBUtils.getParentAdapter(DBSObjectSelector.class, ((DBNDatabaseNode) selectedNode).getObject());
                    if (activeContainer != null && activeContainer.supportsDefaultChange()) {
                        DBSObject activeChild;
                        activeChild = activeContainer.getDefaultObject();
                        if (activeChild != ((DBNDatabaseNode) selectedNode).getObject()) {
                            DBNDatabaseNode databaseNode = (DBNDatabaseNode) selectedNode;
                            if (databaseNode.getObject() != null && (activeChild == null || activeChild.getClass() == databaseNode.getObject().getClass())) {
                                // + databaseNode.getNodeType();
                                String text = "Set Active ";
                                // Fill context menu
                                IAction action = ActionUtils.makeAction(new NavigatorActionSetActiveObject(), workbenchSite, selection, text, null, null);
                                manager.add(action);
                            }
                        }
                    }
                }
                manager.add(new Separator());
                manager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
                manager.add(new GroupMarker(IActionConstants.MB_ADDITIONS_END));
                // Add properties button
                if (PreferencesUtil.hasPropertiesContributors(selection.getFirstElement())) {
                    manager.add(ActionUtils.makeCommandContribution(workbenchSite, IWorkbenchCommandConstants.FILE_PROPERTIES));
                }
                if (selectedNode.isPersisted()) {
                    // Add refresh button
                    manager.add(ActionUtils.makeCommandContribution(workbenchSite, IWorkbenchCommandConstants.FILE_REFRESH));
                }
            }
            manager.add(new GroupMarker(CoreCommands.GROUP_TOOLS));
            if (menuListener != null) {
                menuListener.menuAboutToShow(manager);
            }
        }
    });
    menuMgr.setRemoveAllWhenShown(true);
    control.setMenu(menu);
    control.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            menuMgr.dispose();
        }
    });
    return menuMgr;
}
Also used : ViewerColumnController(org.jkiss.dbeaver.ui.controls.ViewerColumnController) DisposeListener(org.eclipse.swt.events.DisposeListener) MenuListener(org.eclipse.swt.events.MenuListener) DisposeEvent(org.eclipse.swt.events.DisposeEvent) Control(org.eclipse.swt.widgets.Control) Menu(org.eclipse.swt.widgets.Menu) MenuEvent(org.eclipse.swt.events.MenuEvent) MenuItem(org.eclipse.swt.widgets.MenuItem) NavigatorActionSetActiveObject(org.jkiss.dbeaver.ui.actions.navigator.NavigatorActionSetActiveObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) NavigatorActionSetActiveObject(org.jkiss.dbeaver.ui.actions.navigator.NavigatorActionSetActiveObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObjectSelector(org.jkiss.dbeaver.model.struct.DBSObjectSelector)

Aggregations

DBSObjectSelector (org.jkiss.dbeaver.model.struct.DBSObjectSelector)7 DBException (org.jkiss.dbeaver.DBException)5 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)5 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)3 IStatus (org.eclipse.core.runtime.IStatus)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)2 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)2 DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)2 ISelection (org.eclipse.jface.viewers.ISelection)1 DisposeEvent (org.eclipse.swt.events.DisposeEvent)1 DisposeListener (org.eclipse.swt.events.DisposeListener)1 MenuEvent (org.eclipse.swt.events.MenuEvent)1 MenuListener (org.eclipse.swt.events.MenuListener)1 Control (org.eclipse.swt.widgets.Control)1 Menu (org.eclipse.swt.widgets.Menu)1 MenuItem (org.eclipse.swt.widgets.MenuItem)1 DefaultServerOutputReader (org.jkiss.dbeaver.model.impl.DefaultServerOutputReader)1