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