1 package org.simantics.modeling.ui;
3 import java.util.Arrays;
5 import org.simantics.browsing.ui.BuiltinKeys;
6 import org.simantics.browsing.ui.GraphExplorer;
7 import org.simantics.browsing.ui.NodeContext;
8 import org.simantics.browsing.ui.PrimitiveQueryProcessor;
9 import org.simantics.browsing.ui.common.processors.IsExpandedProcessor;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.SelectionHints;
12 import org.simantics.ui.DoubleClickEvent;
13 import org.simantics.ui.IDoubleClickAction;
14 import org.simantics.ui.workbench.action.IWorkbenchActionHints;
15 import org.simantics.ui.workbench.editor.EditorAdapter;
16 import org.simantics.ui.workbench.editor.EditorRegistry;
17 import org.simantics.utils.ui.ISelectionUtils;
18 import org.simantics.utils.ui.action.IPriorityAction;
19 import org.simantics.utils.ui.action.PriorityAction;
22 * @author Tuukka Lehtonen
24 public class ExpandNodeHandler implements IDoubleClickAction {
27 public void doubleClickEvent(DoubleClickEvent e) throws DatabaseException {
28 Object selection = e.getResource();
29 NodeContext node = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, NodeContext.class);
33 Object widget = e.getHintContext().getHint(IWorkbenchActionHints.KEY_WIDGET);
34 if (!(widget instanceof GraphExplorer))
36 GraphExplorer explorer = (GraphExplorer) widget;
38 // Get adapters in descending priority order
39 EditorAdapter[] editorAdapters = EditorRegistry.getInstance().getAdaptersFor(e.getGraph(), selection);
40 if (editorAdapters.length > 1)
41 Arrays.sort(editorAdapters, (o1,o2) -> -(o1.getPriority() - o2.getPriority()));
43 // If editor selection is unanimous, use the editor. Otherwise just expand/collapse the clicked node.
44 if (editorAdapters.length == 0) {
45 e.add(expandAction(explorer, node));
47 } else if (editorAdapters.length > 1 && editorAdapters[0].getPriority() == editorAdapters[1].getPriority()) {
48 e.add(expandAction(explorer, node));
53 private IPriorityAction expandAction(GraphExplorer explorer, NodeContext node) {
54 return new PriorityAction(100) {
57 PrimitiveQueryProcessor<?> pqp = explorer.getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);
58 if (pqp instanceof IsExpandedProcessor) {
59 IsExpandedProcessor iep = (IsExpandedProcessor) pqp;
60 boolean expanded = iep.getExpanded(node);
61 iep.replaceExpanded(node, !expanded);