1 /*******************************************************************************
\r
2 * Copyright (c) 2011 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.profile;
\r
14 import java.awt.Font;
\r
15 import java.awt.geom.AffineTransform;
\r
16 import java.awt.geom.Rectangle2D;
\r
18 import org.simantics.common.color.Color;
\r
19 import org.simantics.databoard.Bindings;
\r
20 import org.simantics.db.ReadGraph;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.exception.DatabaseException;
\r
23 import org.simantics.db.layer0.variable.Variable;
\r
24 import org.simantics.diagram.elements.TextNode;
\r
25 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
\r
26 import org.simantics.g2d.utils.Alignment;
\r
27 import org.simantics.modeling.ModelingResources;
\r
28 import org.simantics.scenegraph.INode;
\r
29 import org.simantics.scenegraph.profile.EvaluationContext;
\r
30 import org.simantics.scenegraph.profile.common.ProfileVariables;
\r
31 import org.simantics.scenegraph.utils.GeometryUtils;
\r
32 import org.simantics.scenegraph.utils.NodeUtil;
\r
33 import org.simantics.ui.colors.Colors;
\r
36 * @author Tuukka Lehtonen
\r
37 * @author Marko Luukkainen
\r
39 public abstract class TextStyle extends StyleBase<MonitorTextResult> {
\r
41 private final Font FONT = Font.decode("Arial 12");
\r
42 private final java.awt.Color BACKGROUND_COLOR = new java.awt.Color(255, 255, 255, 192);
\r
44 protected double xOffset;
\r
45 protected double yOffset;
\r
47 public TextStyle() {
\r
51 public TextStyle(double xOffset, double yOffset) {
\r
52 this.xOffset = xOffset;
\r
53 this.yOffset = yOffset;
\r
56 public abstract Resource getPropertyRelation(ReadGraph g, Resource module);
\r
59 * @return the name of the scene graph node to create to represent the text
\r
60 * element created by this style
\r
62 public abstract String getNodeName();
\r
65 * Override to customize.
\r
70 * @throws DatabaseException
\r
72 protected Resource getConfigurationComponent(ReadGraph graph, Resource element) throws DatabaseException {
\r
73 ModelingResources mr = ModelingResources.getInstance(graph);
\r
74 Resource config = graph.getPossibleObject(element, mr.ElementToComponent);
\r
79 * Override to customize.
\r
84 * @throws DatabaseException
\r
86 protected String getConfigurationComponentNameForElement(ReadGraph graph, Resource element) throws DatabaseException {
\r
87 Resource config = getConfigurationComponent(graph, element);
\r
90 String name = graph.getPossibleRelatedValue(config, getPropertyRelation(graph,element), Bindings.STRING);
\r
94 public AffineTransform getTransform(AffineTransform parentTransform, Rectangle2D elementBounds, int location) {
\r
95 double scale = GeometryUtils.getScale(parentTransform);
\r
97 AffineTransform at = AffineTransform.getTranslateInstance(
\r
98 parentTransform.getTranslateX() + elementBounds.getCenterX() * scale + xOffset,
\r
99 parentTransform.getTranslateY() + elementBounds.getMinY() * scale + yOffset*location
\r
101 at.scale(0.15, 0.15);
\r
107 public MonitorTextResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration)
\r
108 throws DatabaseException {
\r
109 String name = getConfigurationComponentNameForElement(graph, element);
\r
112 AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element);
\r
113 return new MonitorTextResult(name, transform);
\r
117 public void applyStyleForNode(EvaluationContext observer, INode _node, MonitorTextResult result) {
\r
118 String value = result != null ? result.getText() : null;
\r
119 if (value != null && !value.isEmpty() && !value.trim().isEmpty()) {
\r
121 TextNode node = ProfileVariables.claimChild(_node, "", getNodeName(), TextNode.class, observer);
\r
125 Integer location = observer.getTemporaryProperty(_node, "location");
\r
126 if(location == null) location = 1;
\r
128 Rectangle2D bounds = NodeUtil.getLocalElementBounds(_node);
\r
129 // Rectangle2D elementBounds = _node.getElementUtils.getElementBounds(element);
\r
130 AffineTransform at = getTransform(result.getParentTransform(), bounds, location);
\r
132 Color color = result.getColor();
\r
134 observer.setTemporaryProperty(_node, "location", location+1);
\r
136 node.setTransform(at);
\r
137 node.setHorizontalAlignment((byte) Alignment.CENTER.ordinal());
\r
138 node.setZIndex(3000);
\r
139 if(color != null) node.setColor(Colors.awt(color));
\r
140 else node.setColor(java.awt.Color.DARK_GRAY);
\r
142 node.setEditable(false);
\r
143 node.setFont(FONT);
\r
144 node.setText(value);
\r
146 node.setBackgroundColor(BACKGROUND_COLOR);
\r
147 // node.setBorderColor(Color.LIGHT_GRAY);
\r
148 // node.setBorderWidth(1.0f);
\r
150 cleanupStyleForNode(observer, _node);
\r
155 protected void cleanupStyleForNode(EvaluationContext observer, INode node) {
\r
156 ProfileVariables.denyChild(node, "", getNodeName());
\r