1 package org.simantics.modeling.ui;
\r
3 import java.util.Arrays;
\r
4 import java.util.Comparator;
\r
6 import org.simantics.browsing.ui.BuiltinKeys;
\r
7 import org.simantics.browsing.ui.GraphExplorer;
\r
8 import org.simantics.browsing.ui.NodeContext;
\r
9 import org.simantics.browsing.ui.PrimitiveQueryProcessor;
\r
10 import org.simantics.browsing.ui.common.processors.IsExpandedProcessor;
\r
11 import org.simantics.db.ReadGraph;
\r
12 import org.simantics.db.exception.DatabaseException;
\r
13 import org.simantics.db.layer0.SelectionHints;
\r
14 import org.simantics.ui.DoubleClickEvent;
\r
15 import org.simantics.ui.IDoubleClickAction;
\r
16 import org.simantics.ui.workbench.action.IWorkbenchActionHints;
\r
17 import org.simantics.ui.workbench.editor.EditorAdapter;
\r
18 import org.simantics.ui.workbench.editor.EditorRegistry;
\r
19 import org.simantics.utils.ui.ISelectionUtils;
\r
20 import org.simantics.utils.ui.action.IPriorityAction;
\r
21 import org.simantics.utils.ui.action.PriorityAction;
\r
24 * @author Tuukka Lehtonen
\r
26 public class ExpandNodeHandler implements IDoubleClickAction {
\r
29 public void doubleClickEvent(DoubleClickEvent e) throws DatabaseException {
\r
30 ReadGraph g = e.getGraph();
\r
31 final NodeContext node = ISelectionUtils.getSinglePossibleKey(e.getResource(), SelectionHints.KEY_MAIN, NodeContext.class);
\r
35 Object widget = e.getHintContext().getHint(IWorkbenchActionHints.KEY_WIDGET);
\r
36 if (!(widget instanceof GraphExplorer))
\r
38 final GraphExplorer explorer = (GraphExplorer) widget;
\r
40 EditorAdapter[] editorAdapters = EditorRegistry.getInstance().getAdaptersFor(g, node);
\r
42 Arrays.sort(editorAdapters, new Comparator<EditorAdapter>() {
\r
44 public int compare(EditorAdapter o1, EditorAdapter o2) {
\r
45 // Sort in descending priority order
\r
46 return -(o1.getPriority() - o2.getPriority());
\r
50 // If editor selection is unanimous, use the editor. Otherwise just expand/collapse the clicked node.
\r
51 if (editorAdapters.length == 0) {
\r
52 e.add(expandAction(explorer, node));
\r
54 } else if (editorAdapters.length > 1 && editorAdapters[0].getPriority() == editorAdapters[1].getPriority()) {
\r
55 e.add(expandAction(explorer, node));
\r
60 private IPriorityAction expandAction(final GraphExplorer explorer, final NodeContext node) {
\r
61 return new PriorityAction(100) {
\r
64 PrimitiveQueryProcessor<?> pqp = explorer.getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);
\r
65 if (pqp instanceof IsExpandedProcessor) {
\r
66 IsExpandedProcessor iep = (IsExpandedProcessor) pqp;
\r
67 boolean expanded = iep.getExpanded(node);
\r
68 iep.replaceExpanded(node, !expanded);
\r