1 /*******************************************************************************
2 * Copyright (c) 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.profile;
15 import java.awt.geom.AffineTransform;
16 import java.awt.geom.Rectangle2D;
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;
36 * @author Tuukka Lehtonen
37 * @author Marko Luukkainen
39 public abstract class TextStyle extends StyleBase<MonitorTextResult> {
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);
44 protected double xOffset;
45 protected double yOffset;
51 public TextStyle(double xOffset, double yOffset) {
52 this.xOffset = xOffset;
53 this.yOffset = yOffset;
56 public abstract Resource getPropertyRelation(ReadGraph g, Resource module);
59 * @return the name of the scene graph node to create to represent the text
60 * element created by this style
62 public abstract String getNodeName();
65 * Override to customize.
70 * @throws DatabaseException
72 protected Resource getConfigurationComponent(ReadGraph graph, Resource element) throws DatabaseException {
73 ModelingResources mr = ModelingResources.getInstance(graph);
74 Resource config = graph.getPossibleObject(element, mr.ElementToComponent);
79 * Override to customize.
84 * @throws DatabaseException
86 protected String getConfigurationComponentNameForElement(ReadGraph graph, Resource element) throws DatabaseException {
87 Resource config = getConfigurationComponent(graph, element);
90 String name = graph.getPossibleRelatedValue(config, getPropertyRelation(graph,element), Bindings.STRING);
94 public AffineTransform getTransform(AffineTransform parentTransform, Rectangle2D elementBounds, int location) {
95 double scale = GeometryUtils.getScale(parentTransform);
97 AffineTransform at = AffineTransform.getTranslateInstance(
98 parentTransform.getTranslateX() + elementBounds.getCenterX() * scale + xOffset,
99 parentTransform.getTranslateY() + elementBounds.getMinY() * scale + yOffset*location
101 at.scale(0.15, 0.15);
107 public MonitorTextResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration)
108 throws DatabaseException {
109 String name = getConfigurationComponentNameForElement(graph, element);
112 AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element);
113 return new MonitorTextResult(name, transform);
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()) {
121 TextNode node = ProfileVariables.claimChild(_node, "", getNodeName(), TextNode.class, observer);
125 Integer location = observer.getTemporaryProperty(_node, "location");
126 if(location == null) location = 1;
128 Rectangle2D bounds = NodeUtil.getLocalElementBounds(_node);
129 // Rectangle2D elementBounds = _node.getElementUtils.getElementBounds(element);
130 AffineTransform at = getTransform(result.getParentTransform(), bounds, location);
132 Color color = result.getColor();
134 observer.setTemporaryProperty(_node, "location", location+1);
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);
142 node.setEditable(false);
146 node.setBackgroundColor(BACKGROUND_COLOR);
147 // node.setBorderColor(Color.LIGHT_GRAY);
148 // node.setBorderWidth(1.0f);
150 cleanupStyleForNode(observer, _node);
155 protected void cleanupStyleForNode(EvaluationContext observer, INode node) {
156 ProfileVariables.denyChild(node, "", getNodeName());