]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.trend/src/org/simantics/trend/util/KvikDeviationBuilder.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.trend / src / org / simantics / trend / util / KvikDeviationBuilder.java
diff --git a/bundles/org.simantics.trend/src/org/simantics/trend/util/KvikDeviationBuilder.java b/bundles/org.simantics.trend/src/org/simantics/trend/util/KvikDeviationBuilder.java
new file mode 100644 (file)
index 0000000..174249a
--- /dev/null
@@ -0,0 +1,163 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\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.trend.util;\r
+\r
+import java.awt.Graphics2D;\r
+import java.awt.geom.Line2D;\r
+import java.awt.geom.Rectangle2D;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+/**\r
+ * DeviationBuilder builds a vertical string of rectangles as an AWT Shape.\r
+ * Rectangles are added to the shape, each added to the right hand.\r
+ * \r
+ * @author toni.kalajainen\r
+ */\r
+public class KvikDeviationBuilder {\r
+\r
+       List<Rectangle2D.Double> rects = new ArrayList<Rectangle2D.Double>();\r
+       int rectCount = 0;\r
+    \r
+    public KvikDeviationBuilder() {\r
+       reset();\r
+    }\r
+    \r
+    public void addRectangle(double x1, double x2, double y1, double y2)\r
+    {\r
+       if (y1>y2) {\r
+               double yy = y1;\r
+               y1 = y2;\r
+               y2 = yy;\r
+       }\r
+       \r
+       if (y2 == Double.NaN || y1 == Double.NaN || y2 == y1) {\r
+               return;\r
+       }\r
+       \r
+       Rectangle2D.Double lastRect = lastRect();\r
+       if ( lastRect!=null ) {\r
+               double lx1 = lastRect.x;\r
+               double lx2 = lastRect.x + lastRect.width;\r
+               double ly1 = lastRect.y;\r
+               double ly2 = lastRect.y + lastRect.height;\r
+               \r
+               // Extends min-max of prev rectangle\r
+               if (x1==lx1 && x2==lx2) {\r
+                       if (y2<ly1) lastRect.y = y2;\r
+                       if (y1>ly2) lastRect.height = y1 - lastRect.y;\r
+                       return;\r
+               }\r
+               \r
+               // Change the last values       \r
+               if (x1==lx2 && y1==ly1 && y2==ly2) {\r
+                       lastRect.width = x2 - lastRect.x;\r
+                       return;\r
+               }\r
+       }\r
+\r
+       // Add new region\r
+               Rectangle2D.Double r = newRect();\r
+               r.x=x1;\r
+               r.y=y1;\r
+               r.width=x2-x1;\r
+               r.height=y2-y1;\r
+    }\r
+\r
+    /**\r
+     * Extend the previous rectangle to the left\r
+     * \r
+     * @param x1\r
+     * @param x2\r
+     * @param low\r
+     * @param high\r
+     */\r
+    public void appendRectangle(double xx, double low, double high)\r
+    {\r
+       Rectangle2D.Double lastRect = lastRect();\r
+       if ( lastRect!=null ) {\r
+               addRectangle(lastRect.x+lastRect.width, xx, low, high);\r
+       } else {\r
+               addRectangle(xx, xx, low, high);\r
+       }\r
+    }\r
+\r
+    public void extendRectangle(double toX)\r
+    {\r
+       Rectangle2D.Double lastRect = lastRect();\r
+       if ( lastRect == null ) return;\r
+       lastRect.width = toX - lastRect.x;\r
+    }\r
+    \r
+    public boolean isEmpty() {\r
+       return rectCount == 0;\r
+    }\r
+    \r
+    public void reset() {\r
+       rectCount = 0;\r
+    }\r
+    \r
+    /**\r
+     * Draw the shape with lines. This is the fastest for screen but not good for printer drawing\r
+     * \r
+     * @param g\r
+     */\r
+    public void drawLines(Graphics2D g) {\r
+       Line2D.Double line = new Line2D.Double();\r
+       for (int i=0; i<rectCount; i++) {\r
+               Rectangle2D.Double r = rects.get(i);\r
+               int x1 = (int) Math.round(r.x);\r
+               int x2 = (int) Math.round(r.x);\r
+               line.y1 = r.y;\r
+               line.y2 = r.y + r.height;\r
+               for (int x = x1; x<=x2; x++) {\r
+                       line.x1 = line.x2 = x;\r
+                       g.draw(line);\r
+               }\r
+       }\r
+    }\r
+    \r
+    /**\r
+     * Draw the shape with rectangles. Correct, not the fastest\r
+     */\r
+    public void drawRectangles(Graphics2D g) {\r
+       for (int i=0; i<rectCount; i++) {\r
+               Rectangle2D.Double r = rects.get(i);\r
+               g.fill(r);\r
+       }\r
+    }\r
+    \r
+    /**\r
+     * Create new rectangle and add to the list. \r
+     * @return new rectangle\r
+     */\r
+    Rectangle2D.Double newRect() {\r
+       if (rectCount < rects.size()) {\r
+               return rects.get( rectCount++ );\r
+       }\r
+       Rectangle2D.Double rect = new Rectangle2D.Double();\r
+       rectCount++;\r
+       rects.add(rect);\r
+       return rect;\r
+    }\r
+    \r
+    /**\r
+     * Get the last rectangle\r
+     * @return\r
+     */\r
+    Rectangle2D.Double lastRect() {\r
+       if ( rectCount == 0 ) return null;\r
+       return rects.get( rectCount-1 );\r
+    }\r
+    \r
+\r
+}\r
+\r