KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chart2d > WarningRegionProperties


1 /**
2  * Chart2D, a java library for drawing two dimensional charts.
3  * Copyright (C) 2001 Jason J. Simas
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * The author of this library may be contacted at:
19  * E-mail: jjsimas@users.sourceforge.net
20  * Street Address: J J Simas, 887 Tico Road, Ojai, CA 93023-3555 USA
21  */

22
23
24 package net.sourceforge.chart2d;
25
26
27 import java.awt.*;
28 import java.util.*;
29
30
31 /**
32  * The properties of a warning region for GraphChart2D charts. A warning region is a rectangular
33  * region of a graph that when a graph component enters it, the graph component in that region is
34  * painted with a specific color; also the background of that region is also a specific color.
35  * Pass this to any number of GraphChart2D objects.
36  */

37 final public class WarningRegionProperties {
38
39
40   /**
41    * Indicates the high value should be the maximum.
42    * For use with setHigh().
43    * Note that HIGH - N where N is some number is invalid.
44    */

45   public static final float HIGH = Float.POSITIVE_INFINITY;
46
47
48   /**
49    * Indicates the low value should be the minimum.
50    * For use with setHigh().
51    * Note that LOW - N where N is some number is invalid.
52    */

53   public static final float LOW = Float.NEGATIVE_INFINITY;
54
55
56   /**
57    * The default is HIGH.
58    */

59   public static final float HIGH_DEFAULT = HIGH;
60
61
62   /**
63    * The default is LOW.
64    */

65   public static final float LOW_DEFAULT = LOW;
66
67
68   /**
69    * The default is Color.red.
70    */

71   public static final Color COMPONENT_COLOR_DEFAULT = new Color (146, 0, 10);
72
73   /**
74    * The default is true.
75    */

76   public static final boolean BACKGROUND_EXISTENCE_DEFAULT = true;
77
78   /**
79    * The default is Color.pink.
80    */

81   public static final Color BACKGROUND_COLOR_DEFAULT = new Color (222, 177, 180);
82
83
84   private float high;
85   private float low;
86   private Color componentColor;
87   private boolean backgroundExistence;
88   private Color backgroundColor;
89
90   private boolean needsUpdate = true;
91   private final Vector needsUpdateVector = new Vector (5, 5);
92   private final Vector graphChart2DVector = new Vector (5, 5);
93
94
95   /**
96    * Creates a WarningRegionProperties object with the documented default values.
97    */

98   public WarningRegionProperties() {
99
100     needsUpdate = true;
101     setToDefaults();
102   }
103
104
105   /**
106    * Creates a WarningRegionProperties object with property values copied from another object.
107    * The copying is a deep copy.
108    * @param warningRegionProps The properties to copy.
109    */

110   public WarningRegionProperties (WarningRegionProperties warningRegionProps) {
111
112     needsUpdate = true;
113     setWarningRegionProperties (warningRegionProps);
114   }
115
116
117   /**
118    * Sets all properties to their default values.
119    */

120   public final void setToDefaults() {
121
122     needsUpdate = true;
123     setHigh (HIGH_DEFAULT);
124     setLow (LOW_DEFAULT);
125     setComponentColor (COMPONENT_COLOR_DEFAULT);
126     setBackgroundExistence (BACKGROUND_EXISTENCE_DEFAULT);
127     setBackgroundColor (BACKGROUND_COLOR_DEFAULT);
128   }
129
130
131   /**
132    * Sets all properties to be the values of another WarningRegionProperties object.
133    * The copying is a deep copy.
134    * @param warningRegionProps The properties to copy.
135    */

136   public final void setWarningRegionProperties (WarningRegionProperties warningRegionProps) {
137
138     needsUpdate = true;
139     setHigh (warningRegionProps.getHigh());
140     setLow (warningRegionProps.getLow());
141     setComponentColor (warningRegionProps.getComponentColor());
142     setBackgroundExistence (warningRegionProps.getBackgroundExistence());
143     setBackgroundColor (warningRegionProps.getBackgroundColor());
144   }
145
146
147   /**
148    * Sets the high value of this warning region. For example, if your data ranges from 10000 to
149    * 0 and you want an orange region from 6000 to 8000, then set the high to 8000. If you want the
150    * region to extend from 8000 to the top of the graph, then set the high to HIGH.
151    * @param h The high value of this region.
152    */

153   public final void setHigh (float h) {
154
155     high = h;
156     needsUpdate = true;
157   }
158
159
160   /**
161    * Sets the low value of this warning region. For example, if your data ranges from 10000 to
162    * 0 and you want an orange region from 6000 to 8000, then set the low to 6000. If you want the
163    * region to extend from 6000 to the bottom of the graph, then set the low to LOW.
164    * @param l The low value of this region.
165    */

166   public final void setLow (float l) {
167
168     low = l;
169     needsUpdate = true;
170   }
171
172
173   /**
174    * Sets the color that any component entering this region should become. Only the portion of the
175    * component that is in the region will be this color. Examples of components are: bars, lines,
176    * and dots.
177    * @param c The color of the components sections in the region.
178    */

179   public final void setComponentColor (Color c) {
180
181     componentColor = c;
182     needsUpdate = true;
183   }
184
185
186   /**
187    * Sets the existence of the background irrespective of the existence of the graph's background.
188    * @param existence If true, then the background of the warning region will exist.
189    */

190   public final void setBackgroundExistence (boolean existence) {
191
192     backgroundExistence = existence;
193     needsUpdate = true;
194   }
195
196
197   /**
198    * Sets the color that the graph background becomes in this region if the graph background exists.
199    * @param c The color of the section of the graph background.
200    */

201   public final void setBackgroundColor (Color c) {
202
203     backgroundColor = c;
204     needsUpdate = true;
205   }
206
207
208   /**
209    * Gets the high value of this warning region. For example, if your data ranges from 10000 to
210    * 0 and you want an orange region from 6000 to 8000, then set the high to 8000. If you want the
211    * region to extend from 8000 to the top of the graph, then set the high to HIGH.
212    * @return The high value of this region.
213    */

214   public final float getHigh() {
215     return high;
216   }
217
218
219   /**
220    * Gets the low value of this warning region. For example, if your data ranges from 10000 to
221    * 0 and you want an orange region from 6000 to 8000, then set the low to 6000. If you want the
222    * region to extend from 6000 to the bottom of the graph, then set the low to LOW.
223    * @return The low value of this region.
224    */

225   public final float getLow() {
226     return low;
227   }
228
229
230   /**
231    * Gets the color that any component entering this region should become. Only the portion of the
232    * component that is in the region will be this color. Examples of components are: bars, lines,
233    * and dots.
234    * @return The color of the components sections in the region.
235    */

236   public final Color getComponentColor() {
237     return componentColor;
238   }
239
240
241   /**
242    * Gets the existence of the background irrespective of the existence of the graph's background.
243    * @return If true, then the background of the warning region will exist.
244    */

245   public final boolean getBackgroundExistence() {
246     return backgroundExistence;
247   }
248
249
250   /**
251    * Gets the color that the graph background becomes in this region if the graph background exists.
252    * @return The color of the section of the graph background.
253    */

254   public final Color getBackgroundColor() {
255     return backgroundColor;
256   }
257
258
259   /**
260    * Gets whether this object needs to be updated with new properties.
261    * @param graphChart2D The object that may need to be updated.
262    * @return If true then needs update.
263    */

264   final boolean getGraphChart2DNeedsUpdate (GraphChart2D graphChart2D) {
265
266     if (needsUpdate) return true;
267     int index = -1;
268     if ((index = graphChart2DVector.indexOf (graphChart2D)) != -1) {
269       return ((Boolean JavaDoc)needsUpdateVector.get (index)).booleanValue();
270     }
271     return false;
272   }
273
274
275   /**
276    * Adds a GraphChart2D to the set of objects using these properties.
277    * @param graphChart2D The Object2D to add.
278    */

279   final void addGraphChart2D (GraphChart2D graphChart2D) {
280
281     if (!graphChart2DVector.contains (graphChart2D)) {
282       graphChart2DVector.add (graphChart2D);
283       needsUpdateVector.add (new Boolean JavaDoc (true));
284   } }
285
286
287   /**
288    * Removes a GraphChart2D from the set of objects using these properties.
289    * @param graphChart2D The Object2D to remove.
290    */

291   final void removeGraphChart2D (GraphChart2D graphChart2D) {
292
293     int index = -1;
294     if ((index = graphChart2DVector.indexOf (graphChart2D)) != -1) {
295       graphChart2DVector.remove (index);
296       needsUpdateVector.remove (index);
297   } }
298
299
300   /**
301    * Validates the properties of this object.
302    * If debug is true then prints a messages indicating whether each property is valid.
303    * Returns true if all the properties were valid and false otherwise.
304    * @param debug If true then will print status messages.
305    * @return If true then valid.
306    */

307   final boolean validate (boolean debug) {
308
309     if (debug) System.out.println ("Validating WarningRegionProperties");
310
311     boolean valid = true;
312
313     if ((high != HIGH && (low == HIGH || high < low)) ||
314       (low != LOW && (high == LOW || high < low))) {
315       valid = false;
316       if (debug) System.out.println ("High was lower than low");
317     }
318     if (componentColor == null) {
319       valid = false;
320       if (debug) System.out.println ("ComponentColor == null");
321     }
322     if (backgroundColor == null) {
323       valid = false;
324       if (debug) System.out.println ("BackgroundColor == null");
325     }
326
327     if (debug) {
328       if (valid) System.out.println ("WarningRegionProperties was valid");
329       else System.out.println ("WarningRegionProperties was invalid");
330     }
331
332     return valid;
333   }
334
335
336   /**
337    * Updates the properties of this GraphChart2D.
338    * @param graphChart2D The object to update.
339    */

340   final void updateGraphChart2D (GraphChart2D graphChart2D) {
341
342     if (getGraphChart2DNeedsUpdate (graphChart2D)) {
343
344       if (needsUpdate) {
345         for (int i = 0; i < needsUpdateVector.size(); ++i) {
346           needsUpdateVector.set (i, new Boolean JavaDoc (true));
347         }
348         needsUpdate = false;
349       }
350
351       int index = -1;
352       if ((index = graphChart2DVector.indexOf (graphChart2D)) != -1) {
353         needsUpdateVector.set (index, new Boolean JavaDoc (false));
354       }
355     }
356   }
357
358
359   /**
360    * A convencience method for creating a WarningRegion set with these properties.
361    * @return The appropriately set warning region.
362    */

363   final WarningRegion configureWarningRegion() {
364
365     WarningRegion warningRegion = new WarningRegion();
366     warningRegion.setHigh (getHigh());
367     warningRegion.setLow (getLow());
368     warningRegion.setComponentColor (getComponentColor());
369     warningRegion.setBackgroundExistence (getBackgroundExistence());
370     warningRegion.setBackgroundColor (getBackgroundColor());
371     return warningRegion;
372   }
373 }
Popular Tags