]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/EvaluatorLabeler.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / EvaluatorLabeler.java
1 package org.simantics.browsing.ui.graph.impl;
2
3 import java.util.Map;
4
5 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
6 import org.eclipse.swt.widgets.Composite;
7 import org.eclipse.swt.widgets.Event;
8 import org.simantics.Simantics;
9 import org.simantics.browsing.ui.NodeContext;
10 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
11 import org.simantics.browsing.ui.graph.impl.contribution.LabelerContributionImpl;
12 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
13 import org.simantics.browsing.ui.model.tooltips.TooltipContribution;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.UndoContext;
16 import org.simantics.db.common.request.UniqueRead;
17 import org.simantics.db.common.utils.RequestUtil;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.ui.SimanticsUI;
20
21 public class EvaluatorLabeler extends LabelerContributionImpl {
22         
23         final BrowseContext browseContext;
24         final boolean useNodeBrowseContexts;
25         
26     private TooltipContribution currentTooltipContribution;
27         
28     public EvaluatorLabeler(PrimitiveQueryUpdater updater, NodeContext context,
29                         LabelerKey key,
30                         BrowseContext browseContext, boolean useNodeBrowseContexts) {
31                 super(updater, context, key);
32                 this.browseContext = browseContext;
33                 this.useNodeBrowseContexts = useNodeBrowseContexts;
34         }
35
36         @Override
37     public Map<String, String> labels(
38             ReadGraph graph,
39             NodeContext context)
40             throws DatabaseException {
41         boolean oldSynchronous = graph.setSynchronous(false); 
42         try {
43                 return BrowseContext.get(graph,context,browseContext,useNodeBrowseContexts).getLabel(graph, context);
44         } finally {
45                 graph.setSynchronous(oldSynchronous);
46         }
47     }
48
49     @Override
50     public int category(ReadGraph graph,
51             NodeContext context)
52     throws DatabaseException {
53         return 0;
54     }
55     
56     public Modifier 
57     getModifier(ReadGraph graph, UndoContext undoContext, 
58             NodeContext context, String columnKey) throws DatabaseException {
59         return BrowseContext.get(graph,context,browseContext,useNodeBrowseContexts).getModifier(graph, context, columnKey);
60     }
61     
62     public String toString() {
63         return "EvaluatorLabeler[" + browseContext + "] " + context;
64     }
65     
66     
67     @Override
68     public boolean shouldCreateToolTip(Event event, NodeContext context) {
69         try {
70             currentTooltipContribution = RequestUtil.trySyncRequest(
71                     Simantics.getSession(),
72                     SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
73                     SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
74                     null,
75                     new UniqueRead<TooltipContribution>() {
76                 @Override
77                 public TooltipContribution perform(ReadGraph graph) throws DatabaseException {
78                     return BrowseContext.get(graph,context,browseContext,useNodeBrowseContexts).shouldCreateToolTip(graph, event, context);
79                 }
80             });
81             if (currentTooltipContribution != null)
82                 return true;
83         } catch (DatabaseException | InterruptedException e) {
84             e.printStackTrace();
85         }
86         return false;
87     }
88     
89     @Override
90     public Composite createToolTipContentArea(Event event, Composite parent, NodeContext nodeContext) {
91         return (Composite) currentTooltipContribution.getTooltip(event, parent, nodeContext);
92     }
93
94 }