]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/flag/BasicFlagType.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / flag / BasicFlagType.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/BasicFlagType.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/BasicFlagType.java
new file mode 100644 (file)
index 0000000..7475931
--- /dev/null
@@ -0,0 +1,101 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in 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.flag;\r
+\r
+import java.awt.Shape;\r
+import java.awt.geom.Path2D;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.query.DiagramRequests;\r
+import org.simantics.g2d.elementclass.FlagClass;\r
+import org.simantics.g2d.elementclass.FlagClass.Mode;\r
+import org.simantics.g2d.elementclass.FlagClass.Type;\r
+import org.simantics.g2d.utils.Alignment;\r
+import org.simantics.structural2.modelingRules.IModelingRules;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class BasicFlagType extends AbstractFlagType {\r
+\r
+    public BasicFlagType(Resource flag, IModelingRules modelingRules) {\r
+        super(flag, modelingRules);\r
+    }\r
+\r
+    @Override\r
+    public FlagInfo getInfo(ReadGraph graph) throws DatabaseException {\r
+        Type type = getType(graph);\r
+        Mode mode = getMode(graph);\r
+\r
+        return FlagInfoBuilder.fill(type)\r
+        .shape(getShape(graph, type, mode))\r
+        .text(getText(graph))\r
+        .textArea(getArea(graph, type, mode))\r
+        .horizontalAlignment(Alignment.LEADING)\r
+        .verticalAlignment(Alignment.CENTER)\r
+        .create();\r
+    }\r
+\r
+    protected Rectangle2D getArea(ReadGraph graph, Type type, Mode mode) {\r
+        return getArea(type, mode);\r
+    }\r
+\r
+    public static Rectangle2D getArea(Type type, Mode mode) {\r
+        final double width = FlagClass.DEFAULT_WIDTH;\r
+        final double height = FlagClass.DEFAULT_HEIGHT;\r
+        final double beakLength = FlagClass.getBeakLength(height, FlagClass.DEFAULT_BEAK_ANGLE);\r
+        return getArea(type, mode, width, height, beakLength);\r
+    }\r
+\r
+    public static Rectangle2D getArea(Type type, Mode mode, double width, double height, double beakLength) {\r
+        double x = 0;\r
+        double y = -height / 2;\r
+        double w = width;\r
+        double h = height;\r
+\r
+        if (type == Type.In) {\r
+            if (mode instanceof FlagClass.External) {\r
+                x = -width-beakLength;\r
+            } else if (mode == Mode.Internal) {\r
+                x = -beakLength;\r
+                w = beakLength;\r
+            }\r
+        } else if (type == Type.Out) {\r
+            if (mode == Mode.Internal) {\r
+                w = beakLength;\r
+            }\r
+        }\r
+\r
+        return new Rectangle2D.Double(x, y, w, h);\r
+    }\r
+\r
+    protected Shape getShape(ReadGraph graph, Type type, Mode mode) throws DatabaseException {\r
+        return getShape(type, mode);\r
+    }\r
+\r
+    public static Shape getShape(Type type, Mode mode) {\r
+        final double width = FlagClass.DEFAULT_WIDTH;\r
+        final double height = FlagClass.DEFAULT_HEIGHT;\r
+        final double beakLength = FlagClass.getBeakLength(height, FlagClass.DEFAULT_BEAK_ANGLE);\r
+        Path2D path = new Path2D.Double();\r
+        FlagClass.createFlagShape(path, type, mode, width, height, beakLength);\r
+        return path;\r
+    }\r
+\r
+    protected String[] getText(ReadGraph graph) throws DatabaseException {\r
+        return graph.syncRequest(DiagramRequests.getFlagText(flag));\r
+    }\r
+\r
+}
\ No newline at end of file