Fix diagram profiles to work with latest DB changes
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / SCLTextGridStyle.java
1 package org.simantics.diagram.profile;
2
3 import java.awt.Font;
4 import java.awt.geom.AffineTransform;
5
6 import org.simantics.Simantics;
7 import org.simantics.databoard.Bindings;
8 import org.simantics.datatypes.literal.Vec2d;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.db.layer0.variable.Variables;
14 import org.simantics.diagram.G2DUtils;
15 import org.simantics.diagram.stubs.DiagramResource;
16 import org.simantics.diagram.stubs.G2DResource;
17 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
18 import org.simantics.modeling.ModelingResources;
19 import org.simantics.scl.runtime.function.Function1;
20 import org.simantics.scl.runtime.tuple.Tuple3;
21 import org.simantics.utils.datastructures.Pair;
22
23 /**
24  * @author Antti Villberg
25  */
26 public class SCLTextGridStyle extends TextGridStyle {
27
28         final Font font;
29
30         public SCLTextGridStyle(ReadGraph graph, Resource style) throws DatabaseException {
31                 super(style);
32                 G2DResource G2D = G2DResource.getInstance(graph);
33                 Resource fontR = graph.getPossibleObject(style, G2D.HasFont);
34                 if(fontR != null) {
35                         font = G2DUtils.getFont(graph, fontR);
36                 } else {
37                         font = null;
38                 }
39
40         }
41
42         @Override
43         protected Font getFont() {
44                 if(font != null) return font;
45                 return super.getFont();
46         }
47
48         @Override
49         public Resource getPropertyRelation(ReadGraph graph, Resource module) {
50                 throw new Error("Fix this");
51         }
52
53         @Override
54         protected Object getIdentity(Resource entry) {
55                 return new Pair<Resource, Resource>(getResource(), entry);
56         }
57
58         @Override
59         protected String rowId() {
60                 return getNodeName();
61         }
62
63         @Override
64         public MonitorTextGridResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration)
65                         throws DatabaseException {
66
67                 DiagramResource DIA = DiagramResource.getInstance(graph);
68                 ModelingResources MOD = ModelingResources.getInstance(graph);
69
70                 String variableURI = graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasVariable, Bindings.STRING);
71                 Variable activeVariable = org.simantics.db.layer0.variable.Variables.getPossibleVariable(graph, variableURI);
72                 if (activeVariable == null)
73                         return null;
74
75                 Resource module = graph.getPossibleObject(element, MOD.ElementToComponent);
76                 if (module == null)
77                         return null;
78
79                 Variable moduleVariable = activeVariable.browsePossible(graph, module);
80                 if (moduleVariable == null)
81                         return null;
82
83                 Variable styleVariable = Variables.getVariable(graph, getResource());
84                 Function1<Variable,Tuple3> function = styleVariable.getPossiblePropertyValue(graph, DIA.SCLTextGridStyle_texts);
85                 Tuple3 result = Simantics.applySCLRead(graph, function, moduleVariable);
86                 
87                 AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element);
88                 Vec2d offset = DiagramGraphUtil.getOffset(graph, element);
89                 boolean enabled = !DiagramGraphUtil.getProfileMonitorsHidden(graph, element);
90                 boolean up = DiagramGraphUtil.getProfileMonitorsUp(graph, element);
91                 double spacing = DiagramGraphUtil.getProfileMonitorSpacing(graph, element);
92
93                 return new MonitorTextGridResult(rowId(), (String)result.c0, (String)result.c1, (String)result.c2, enabled, up, spacing, null, null, ElementTranslation.function(element), transform, offset);
94
95         }
96
97         @Override
98         public String getNodeName() {
99                 return "" + getResource().getResourceId();
100         }
101
102 }