]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/profile/TextStyle.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / TextStyle.java
1 /*******************************************************************************
2  * Copyright (c) 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.profile;
13
14 import java.awt.Font;
15 import java.awt.geom.AffineTransform;
16 import java.awt.geom.Rectangle2D;
17
18 import org.simantics.common.color.Color;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.variable.Variable;
24 import org.simantics.diagram.elements.TextNode;
25 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
26 import org.simantics.g2d.utils.Alignment;
27 import org.simantics.modeling.ModelingResources;
28 import org.simantics.scenegraph.INode;
29 import org.simantics.scenegraph.profile.EvaluationContext;
30 import org.simantics.scenegraph.profile.common.ProfileVariables;
31 import org.simantics.scenegraph.utils.GeometryUtils;
32 import org.simantics.scenegraph.utils.NodeUtil;
33 import org.simantics.ui.colors.Colors;
34
35 /**
36  * @author Tuukka Lehtonen
37  * @author Marko Luukkainen
38  */
39 public abstract class TextStyle extends StyleBase<MonitorTextResult> {
40
41     private final Font  FONT             = Font.decode("Arial 12");
42     private final java.awt.Color BACKGROUND_COLOR = new java.awt.Color(255, 255, 255, 192);
43
44     protected double xOffset;
45     protected double yOffset;
46
47     public TextStyle() {
48         this(0.0, -2.2);
49     }
50
51     public TextStyle(double xOffset, double yOffset) {
52         this.xOffset = xOffset;
53         this.yOffset = yOffset;
54     }
55
56     public abstract Resource getPropertyRelation(ReadGraph g, Resource module);
57
58     /**
59      * @return the name of the scene graph node to create to represent the text
60      *         element created by this style
61      */
62     public abstract String getNodeName();
63
64     /**
65      * Override to customize.
66      * 
67      * @param graph
68      * @param element
69      * @return
70      * @throws DatabaseException
71      */
72     protected Resource getConfigurationComponent(ReadGraph graph, Resource element) throws DatabaseException {
73         ModelingResources mr = ModelingResources.getInstance(graph);
74         Resource config = graph.getPossibleObject(element, mr.ElementToComponent);
75         return config;
76     }
77
78     /**
79      * Override to customize.
80      * 
81      * @param graph
82      * @param element
83      * @return
84      * @throws DatabaseException
85      */
86     protected String getConfigurationComponentNameForElement(ReadGraph graph, Resource element) throws DatabaseException {
87         Resource config = getConfigurationComponent(graph, element);
88         if (config == null)
89             return null;
90         String name = graph.getPossibleRelatedValue(config, getPropertyRelation(graph,element), Bindings.STRING);
91         return name;
92     }
93
94     public AffineTransform getTransform(AffineTransform parentTransform, Rectangle2D elementBounds, int location) {
95         double scale = GeometryUtils.getScale(parentTransform);
96
97         AffineTransform at = AffineTransform.getTranslateInstance(
98                 parentTransform.getTranslateX() + elementBounds.getCenterX() * scale + xOffset,
99                 parentTransform.getTranslateY() + elementBounds.getMinY() * scale + yOffset*location
100         );
101         at.scale(0.15, 0.15);
102
103         return at;
104     }
105
106     @Override
107     public MonitorTextResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration)
108     throws DatabaseException {
109         String name = getConfigurationComponentNameForElement(graph, element);
110         if (name == null)
111             return null;
112         AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element);
113         return new MonitorTextResult(name, transform);
114     }
115
116     @Override
117     public void applyStyleForNode(EvaluationContext observer, INode _node, MonitorTextResult result) {
118         String value = result != null ? result.getText() : null;
119         if (value != null && !value.isEmpty() && !value.trim().isEmpty()) {
120
121             TextNode node = ProfileVariables.claimChild(_node, "", getNodeName(), TextNode.class, observer);
122             if (node == null)
123                 return;
124
125             Integer location = observer.getTemporaryProperty(_node, "location");
126             if(location == null) location = 1;
127
128             Rectangle2D bounds = NodeUtil.getLocalElementBounds(_node); 
129 //            Rectangle2D elementBounds = _node.getElementUtils.getElementBounds(element);
130             AffineTransform at = getTransform(result.getParentTransform(), bounds, location);
131
132             Color color = result.getColor();
133
134             observer.setTemporaryProperty(_node, "location", location+1);
135
136             node.setTransform(at);
137             node.setHorizontalAlignment((byte) Alignment.CENTER.ordinal());
138             node.setZIndex(3000);
139             if(color != null) node.setColor(Colors.awt(color));
140             else node.setColor(java.awt.Color.DARK_GRAY);
141
142             node.setEditable(false);
143             node.setFont(FONT);
144             node.setText(value);
145
146             node.setBackgroundColor(BACKGROUND_COLOR);
147 //            node.setBorderColor(Color.LIGHT_GRAY);
148 //            node.setBorderWidth(1.0f);
149         } else {
150             cleanupStyleForNode(observer, _node);
151         }
152     }
153
154     @Override
155     protected void cleanupStyleForNode(EvaluationContext observer, INode node) {
156         ProfileVariables.denyChild(node, "", getNodeName());
157     }
158
159 }