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;
}
Aggregations