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