Search in sources :

Example 1 with NavigatorActionSetActiveObject

use of org.jkiss.dbeaver.ui.actions.navigator.NavigatorActionSetActiveObject 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

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 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)1 DBSObjectSelector (org.jkiss.dbeaver.model.struct.DBSObjectSelector)1 NavigatorActionSetActiveObject (org.jkiss.dbeaver.ui.actions.navigator.NavigatorActionSetActiveObject)1 ViewerColumnController (org.jkiss.dbeaver.ui.controls.ViewerColumnController)1