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