]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.trend.util;\r
12 \r
13 import java.awt.Graphics2D;\r
14 import java.awt.geom.Line2D;\r
15 import java.awt.geom.Rectangle2D;\r
16 import java.util.ArrayList;\r
17 import java.util.List;\r
18 \r
19 /**\r
20  * DeviationBuilder builds a vertical string of rectangles as an AWT Shape.\r
21  * Rectangles are added to the shape, each added to the right hand.\r
22  * \r
23  * @author toni.kalajainen\r
24  */\r
25 public class KvikDeviationBuilder {\r
26 \r
27         List<Rectangle2D.Double> rects = new ArrayList<Rectangle2D.Double>();\r
28         int rectCount = 0;\r
29     \r
30     public KvikDeviationBuilder() {\r
31         reset();\r
32     }\r
33     \r
34     public void addRectangle(double x1, double x2, double y1, double y2)\r
35     {\r
36         if (y1>y2) {\r
37                 double yy = y1;\r
38                 y1 = y2;\r
39                 y2 = yy;\r
40         }\r
41         \r
42         if (y2 == Double.NaN || y1 == Double.NaN || y2 == y1) {\r
43                 return;\r
44         }\r
45         \r
46         Rectangle2D.Double lastRect = lastRect();\r
47         if ( lastRect!=null ) {\r
48                 double lx1 = lastRect.x;\r
49                 double lx2 = lastRect.x + lastRect.width;\r
50                 double ly1 = lastRect.y;\r
51                 double ly2 = lastRect.y + lastRect.height;\r
52                 \r
53                 // Extends min-max of prev rectangle\r
54                 if (x1==lx1 && x2==lx2) {\r
55                         if (y2<ly1) lastRect.y = y2;\r
56                         if (y1>ly2) lastRect.height = y1 - lastRect.y;\r
57                         return;\r
58                 }\r
59                 \r
60                 // Change the last values       \r
61                 if (x1==lx2 && y1==ly1 && y2==ly2) {\r
62                         lastRect.width = x2 - lastRect.x;\r
63                         return;\r
64                 }\r
65         }\r
66 \r
67         // Add new region\r
68                 Rectangle2D.Double r = newRect();\r
69                 r.x=x1;\r
70                 r.y=y1;\r
71                 r.width=x2-x1;\r
72                 r.height=y2-y1;\r
73     }\r
74 \r
75     /**\r
76      * Extend the previous rectangle to the left\r
77      * \r
78      * @param x1\r
79      * @param x2\r
80      * @param low\r
81      * @param high\r
82      */\r
83     public void appendRectangle(double xx, double low, double high)\r
84     {\r
85         Rectangle2D.Double lastRect = lastRect();\r
86         if ( lastRect!=null ) {\r
87                 addRectangle(lastRect.x+lastRect.width, xx, low, high);\r
88         } else {\r
89                 addRectangle(xx, xx, low, high);\r
90         }\r
91     }\r
92 \r
93     public void extendRectangle(double toX)\r
94     {\r
95         Rectangle2D.Double lastRect = lastRect();\r
96         if ( lastRect == null ) return;\r
97         lastRect.width = toX - lastRect.x;\r
98     }\r
99     \r
100     public boolean isEmpty() {\r
101         return rectCount == 0;\r
102     }\r
103     \r
104     public void reset() {\r
105         rectCount = 0;\r
106     }\r
107     \r
108     /**\r
109      * Draw the shape with lines. This is the fastest for screen but not good for printer drawing\r
110      * \r
111      * @param g\r
112      */\r
113     public void drawLines(Graphics2D g) {\r
114         Line2D.Double line = new Line2D.Double();\r
115         for (int i=0; i<rectCount; i++) {\r
116                 Rectangle2D.Double r = rects.get(i);\r
117                 int x1 = (int) Math.round(r.x);\r
118                 int x2 = (int) Math.round(r.x);\r
119                 line.y1 = r.y;\r
120                 line.y2 = r.y + r.height;\r
121                 for (int x = x1; x<=x2; x++) {\r
122                         line.x1 = line.x2 = x;\r
123                         g.draw(line);\r
124                 }\r
125         }\r
126     }\r
127     \r
128     /**\r
129      * Draw the shape with rectangles. Correct, not the fastest\r
130      */\r
131     public void drawRectangles(Graphics2D g) {\r
132         for (int i=0; i<rectCount; i++) {\r
133                 Rectangle2D.Double r = rects.get(i);\r
134                 g.fill(r);\r
135         }\r
136     }\r
137     \r
138     /**\r
139      * Create new rectangle and add to the list. \r
140      * @return new rectangle\r
141      */\r
142     Rectangle2D.Double newRect() {\r
143         if (rectCount < rects.size()) {\r
144                 return rects.get( rectCount++ );\r
145         }\r
146         Rectangle2D.Double rect = new Rectangle2D.Double();\r
147         rectCount++;\r
148         rects.add(rect);\r
149         return rect;\r
150     }\r
151     \r
152     /**\r
153      * Get the last rectangle\r
154      * @return\r
155      */\r
156     Rectangle2D.Double lastRect() {\r
157         if ( rectCount == 0 ) return null;\r
158         return rects.get( rectCount-1 );\r
159     }\r
160     \r
161 \r
162 }\r
163 \r