package org.simantics.modeling.ui; import java.util.Arrays; import org.simantics.browsing.ui.BuiltinKeys; import org.simantics.browsing.ui.GraphExplorer; import org.simantics.browsing.ui.NodeContext; import org.simantics.browsing.ui.PrimitiveQueryProcessor; import org.simantics.browsing.ui.common.processors.IsExpandedProcessor; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.SelectionHints; import org.simantics.ui.DoubleClickEvent; import org.simantics.ui.IDoubleClickAction; import org.simantics.ui.workbench.action.IWorkbenchActionHints; import org.simantics.ui.workbench.editor.EditorAdapter; import org.simantics.ui.workbench.editor.EditorRegistry; import org.simantics.utils.ui.ISelectionUtils; import org.simantics.utils.ui.action.IPriorityAction; import org.simantics.utils.ui.action.PriorityAction; /** * @author Tuukka Lehtonen */ public class ExpandNodeHandler implements IDoubleClickAction { @Override public void doubleClickEvent(DoubleClickEvent e) throws DatabaseException { Object selection = e.getResource(); NodeContext node = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, NodeContext.class); if (node == null) return; Object widget = e.getHintContext().getHint(IWorkbenchActionHints.KEY_WIDGET); if (!(widget instanceof GraphExplorer)) return; GraphExplorer explorer = (GraphExplorer) widget; // Get adapters in descending priority order EditorAdapter[] editorAdapters = EditorRegistry.getInstance().getAdaptersFor(e.getGraph(), selection); if (editorAdapters.length > 1) Arrays.sort(editorAdapters, (o1,o2) -> -(o1.getPriority() - o2.getPriority())); // If editor selection is unanimous, use the editor. Otherwise just expand/collapse the clicked node. if (editorAdapters.length == 0) { e.add(expandAction(explorer, node)); e.consume(); } else if (editorAdapters.length > 1 && editorAdapters[0].getPriority() == editorAdapters[1].getPriority()) { e.add(expandAction(explorer, node)); e.consume(); } } private IPriorityAction expandAction(GraphExplorer explorer, NodeContext node) { return new PriorityAction(100) { @Override public void run() { PrimitiveQueryProcessor pqp = explorer.getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED); if (pqp instanceof IsExpandedProcessor) { IsExpandedProcessor iep = (IsExpandedProcessor) pqp; boolean expanded = iep.getExpanded(node); iep.replaceExpanded(node, !expanded); } } }; } }