]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/ExpandNodeHandler.java
Red background color & tooltip for invalid derived property expression
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / ExpandNodeHandler.java
1 package org.simantics.modeling.ui;
2
3 import java.util.Arrays;
4
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;
20
21 /**
22  * @author Tuukka Lehtonen
23  */
24 public class ExpandNodeHandler implements IDoubleClickAction {
25
26     @Override
27     public void doubleClickEvent(DoubleClickEvent e) throws DatabaseException {
28         Object selection = e.getResource();
29         NodeContext node = ISelectionUtils.getSinglePossibleKey(selection, SelectionHints.KEY_MAIN, NodeContext.class);
30         if (node == null)
31             return;
32
33         Object widget = e.getHintContext().getHint(IWorkbenchActionHints.KEY_WIDGET);
34         if (!(widget instanceof GraphExplorer))
35             return;
36         GraphExplorer explorer = (GraphExplorer) widget;
37
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()));
42
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));
46             e.consume();
47         } else if (editorAdapters.length > 1 && editorAdapters[0].getPriority() == editorAdapters[1].getPriority()) {
48             e.add(expandAction(explorer, node));
49             e.consume();
50         }
51     }
52
53     private IPriorityAction expandAction(GraphExplorer explorer, NodeContext node) {
54         return new PriorityAction(100) {
55             @Override
56             public void run() {
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);
62                 }
63             }
64         };
65     }
66
67 }