--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.profile;\r
+\r
+import java.awt.Font;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.common.color.Color;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.diagram.elements.TextNode;\r
+import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;\r
+import org.simantics.g2d.utils.Alignment;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.scenegraph.INode;\r
+import org.simantics.scenegraph.profile.EvaluationContext;\r
+import org.simantics.scenegraph.profile.common.ProfileVariables;\r
+import org.simantics.scenegraph.utils.GeometryUtils;\r
+import org.simantics.scenegraph.utils.NodeUtil;\r
+import org.simantics.ui.colors.Colors;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ * @author Marko Luukkainen\r
+ */\r
+public abstract class TextStyle extends StyleBase<MonitorTextResult> {\r
+\r
+ private final Font FONT = Font.decode("Arial 12");\r
+ private final java.awt.Color BACKGROUND_COLOR = new java.awt.Color(255, 255, 255, 192);\r
+\r
+ protected double xOffset;\r
+ protected double yOffset;\r
+\r
+ public TextStyle() {\r
+ this(0.0, -2.2);\r
+ }\r
+\r
+ public TextStyle(double xOffset, double yOffset) {\r
+ this.xOffset = xOffset;\r
+ this.yOffset = yOffset;\r
+ }\r
+\r
+ public abstract Resource getPropertyRelation(ReadGraph g, Resource module);\r
+\r
+ /**\r
+ * @return the name of the scene graph node to create to represent the text\r
+ * element created by this style\r
+ */\r
+ public abstract String getNodeName();\r
+\r
+ /**\r
+ * Override to customize.\r
+ * \r
+ * @param graph\r
+ * @param element\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ protected Resource getConfigurationComponent(ReadGraph graph, Resource element) throws DatabaseException {\r
+ ModelingResources mr = ModelingResources.getInstance(graph);\r
+ Resource config = graph.getPossibleObject(element, mr.ElementToComponent);\r
+ return config;\r
+ }\r
+\r
+ /**\r
+ * Override to customize.\r
+ * \r
+ * @param graph\r
+ * @param element\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ protected String getConfigurationComponentNameForElement(ReadGraph graph, Resource element) throws DatabaseException {\r
+ Resource config = getConfigurationComponent(graph, element);\r
+ if (config == null)\r
+ return null;\r
+ String name = graph.getPossibleRelatedValue(config, getPropertyRelation(graph,element), Bindings.STRING);\r
+ return name;\r
+ }\r
+\r
+ public AffineTransform getTransform(AffineTransform parentTransform, Rectangle2D elementBounds, int location) {\r
+ double scale = GeometryUtils.getScale(parentTransform);\r
+\r
+ AffineTransform at = AffineTransform.getTranslateInstance(\r
+ parentTransform.getTranslateX() + elementBounds.getCenterX() * scale + xOffset,\r
+ parentTransform.getTranslateY() + elementBounds.getMinY() * scale + yOffset*location\r
+ );\r
+ at.scale(0.15, 0.15);\r
+\r
+ return at;\r
+ }\r
+\r
+ @Override\r
+ public MonitorTextResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration)\r
+ throws DatabaseException {\r
+ String name = getConfigurationComponentNameForElement(graph, element);\r
+ if (name == null)\r
+ return null;\r
+ AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element);\r
+ return new MonitorTextResult(name, transform);\r
+ }\r
+\r
+ @Override\r
+ public void applyStyleForNode(EvaluationContext observer, INode _node, MonitorTextResult result) {\r
+ String value = result != null ? result.getText() : null;\r
+ if (value != null && !value.isEmpty() && !value.trim().isEmpty()) {\r
+\r
+ TextNode node = ProfileVariables.claimChild(_node, "", getNodeName(), TextNode.class, observer);\r
+ if (node == null)\r
+ return;\r
+\r
+ Integer location = observer.getTemporaryProperty(_node, "location");\r
+ if(location == null) location = 1;\r
+\r
+ Rectangle2D bounds = NodeUtil.getLocalElementBounds(_node); \r
+// Rectangle2D elementBounds = _node.getElementUtils.getElementBounds(element);\r
+ AffineTransform at = getTransform(result.getParentTransform(), bounds, location);\r
+\r
+ Color color = result.getColor();\r
+\r
+ observer.setTemporaryProperty(_node, "location", location+1);\r
+\r
+ node.setTransform(at);\r
+ node.setHorizontalAlignment((byte) Alignment.CENTER.ordinal());\r
+ node.setZIndex(3000);\r
+ if(color != null) node.setColor(Colors.awt(color));\r
+ else node.setColor(java.awt.Color.DARK_GRAY);\r
+\r
+ node.setEditable(false);\r
+ node.setFont(FONT);\r
+ node.setText(value);\r
+\r
+ node.setBackgroundColor(BACKGROUND_COLOR);\r
+// node.setBorderColor(Color.LIGHT_GRAY);\r
+// node.setBorderWidth(1.0f);\r
+ } else {\r
+ cleanupStyleForNode(observer, _node);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ protected void cleanupStyleForNode(EvaluationContext observer, INode node) {\r
+ ProfileVariables.denyChild(node, "", getNodeName());\r
+ }\r
+\r
+}\r