001: /**
002: * Chart2D, a java library for drawing two dimensional charts.
003: * Copyright (C) 2001 Jason J. Simas
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * The author of this library may be contacted at:
019: * E-mail: jjsimas@users.sourceforge.net
020: * Street Address: J J Simas, 887 Tico Road, Ojai, CA 93023-3555 USA
021: */package net.sourceforge.chart2d;
022:
023: import java.awt.*;
024: import java.util.*;
025:
026: /**
027: * The properties of a warning region for GraphChart2D charts. A warning region is a rectangular
028: * region of a graph that when a graph component enters it, the graph component in that region is
029: * painted with a specific color; also the background of that region is also a specific color.
030: * Pass this to any number of GraphChart2D objects.
031: */
032: final public class WarningRegionProperties {
033:
034: /**
035: * Indicates the high value should be the maximum.
036: * For use with setHigh().
037: * Note that HIGH - N where N is some number is invalid.
038: */
039: public static final float HIGH = Float.POSITIVE_INFINITY;
040:
041: /**
042: * Indicates the low value should be the minimum.
043: * For use with setHigh().
044: * Note that LOW - N where N is some number is invalid.
045: */
046: public static final float LOW = Float.NEGATIVE_INFINITY;
047:
048: /**
049: * The default is HIGH.
050: */
051: public static final float HIGH_DEFAULT = HIGH;
052:
053: /**
054: * The default is LOW.
055: */
056: public static final float LOW_DEFAULT = LOW;
057:
058: /**
059: * The default is Color.red.
060: */
061: public static final Color COMPONENT_COLOR_DEFAULT = new Color(146,
062: 0, 10);
063:
064: /**
065: * The default is true.
066: */
067: public static final boolean BACKGROUND_EXISTENCE_DEFAULT = true;
068:
069: /**
070: * The default is Color.pink.
071: */
072: public static final Color BACKGROUND_COLOR_DEFAULT = new Color(222,
073: 177, 180);
074:
075: private float high;
076: private float low;
077: private Color componentColor;
078: private boolean backgroundExistence;
079: private Color backgroundColor;
080:
081: private boolean needsUpdate = true;
082: private final Vector needsUpdateVector = new Vector(5, 5);
083: private final Vector graphChart2DVector = new Vector(5, 5);
084:
085: /**
086: * Creates a WarningRegionProperties object with the documented default values.
087: */
088: public WarningRegionProperties() {
089:
090: needsUpdate = true;
091: setToDefaults();
092: }
093:
094: /**
095: * Creates a WarningRegionProperties object with property values copied from another object.
096: * The copying is a deep copy.
097: * @param warningRegionProps The properties to copy.
098: */
099: public WarningRegionProperties(
100: WarningRegionProperties warningRegionProps) {
101:
102: needsUpdate = true;
103: setWarningRegionProperties(warningRegionProps);
104: }
105:
106: /**
107: * Sets all properties to their default values.
108: */
109: public final void setToDefaults() {
110:
111: needsUpdate = true;
112: setHigh(HIGH_DEFAULT);
113: setLow(LOW_DEFAULT);
114: setComponentColor(COMPONENT_COLOR_DEFAULT);
115: setBackgroundExistence(BACKGROUND_EXISTENCE_DEFAULT);
116: setBackgroundColor(BACKGROUND_COLOR_DEFAULT);
117: }
118:
119: /**
120: * Sets all properties to be the values of another WarningRegionProperties object.
121: * The copying is a deep copy.
122: * @param warningRegionProps The properties to copy.
123: */
124: public final void setWarningRegionProperties(
125: WarningRegionProperties warningRegionProps) {
126:
127: needsUpdate = true;
128: setHigh(warningRegionProps.getHigh());
129: setLow(warningRegionProps.getLow());
130: setComponentColor(warningRegionProps.getComponentColor());
131: setBackgroundExistence(warningRegionProps
132: .getBackgroundExistence());
133: setBackgroundColor(warningRegionProps.getBackgroundColor());
134: }
135:
136: /**
137: * Sets the high value of this warning region. For example, if your data ranges from 10000 to
138: * 0 and you want an orange region from 6000 to 8000, then set the high to 8000. If you want the
139: * region to extend from 8000 to the top of the graph, then set the high to HIGH.
140: * @param h The high value of this region.
141: */
142: public final void setHigh(float h) {
143:
144: high = h;
145: needsUpdate = true;
146: }
147:
148: /**
149: * Sets the low value of this warning region. For example, if your data ranges from 10000 to
150: * 0 and you want an orange region from 6000 to 8000, then set the low to 6000. If you want the
151: * region to extend from 6000 to the bottom of the graph, then set the low to LOW.
152: * @param l The low value of this region.
153: */
154: public final void setLow(float l) {
155:
156: low = l;
157: needsUpdate = true;
158: }
159:
160: /**
161: * Sets the color that any component entering this region should become. Only the portion of the
162: * component that is in the region will be this color. Examples of components are: bars, lines,
163: * and dots.
164: * @param c The color of the components sections in the region.
165: */
166: public final void setComponentColor(Color c) {
167:
168: componentColor = c;
169: needsUpdate = true;
170: }
171:
172: /**
173: * Sets the existence of the background irrespective of the existence of the graph's background.
174: * @param existence If true, then the background of the warning region will exist.
175: */
176: public final void setBackgroundExistence(boolean existence) {
177:
178: backgroundExistence = existence;
179: needsUpdate = true;
180: }
181:
182: /**
183: * Sets the color that the graph background becomes in this region if the graph background exists.
184: * @param c The color of the section of the graph background.
185: */
186: public final void setBackgroundColor(Color c) {
187:
188: backgroundColor = c;
189: needsUpdate = true;
190: }
191:
192: /**
193: * Gets the high value of this warning region. For example, if your data ranges from 10000 to
194: * 0 and you want an orange region from 6000 to 8000, then set the high to 8000. If you want the
195: * region to extend from 8000 to the top of the graph, then set the high to HIGH.
196: * @return The high value of this region.
197: */
198: public final float getHigh() {
199: return high;
200: }
201:
202: /**
203: * Gets the low value of this warning region. For example, if your data ranges from 10000 to
204: * 0 and you want an orange region from 6000 to 8000, then set the low to 6000. If you want the
205: * region to extend from 6000 to the bottom of the graph, then set the low to LOW.
206: * @return The low value of this region.
207: */
208: public final float getLow() {
209: return low;
210: }
211:
212: /**
213: * Gets the color that any component entering this region should become. Only the portion of the
214: * component that is in the region will be this color. Examples of components are: bars, lines,
215: * and dots.
216: * @return The color of the components sections in the region.
217: */
218: public final Color getComponentColor() {
219: return componentColor;
220: }
221:
222: /**
223: * Gets the existence of the background irrespective of the existence of the graph's background.
224: * @return If true, then the background of the warning region will exist.
225: */
226: public final boolean getBackgroundExistence() {
227: return backgroundExistence;
228: }
229:
230: /**
231: * Gets the color that the graph background becomes in this region if the graph background exists.
232: * @return The color of the section of the graph background.
233: */
234: public final Color getBackgroundColor() {
235: return backgroundColor;
236: }
237:
238: /**
239: * Gets whether this object needs to be updated with new properties.
240: * @param graphChart2D The object that may need to be updated.
241: * @return If true then needs update.
242: */
243: final boolean getGraphChart2DNeedsUpdate(GraphChart2D graphChart2D) {
244:
245: if (needsUpdate)
246: return true;
247: int index = -1;
248: if ((index = graphChart2DVector.indexOf(graphChart2D)) != -1) {
249: return ((Boolean) needsUpdateVector.get(index))
250: .booleanValue();
251: }
252: return false;
253: }
254:
255: /**
256: * Adds a GraphChart2D to the set of objects using these properties.
257: * @param graphChart2D The Object2D to add.
258: */
259: final void addGraphChart2D(GraphChart2D graphChart2D) {
260:
261: if (!graphChart2DVector.contains(graphChart2D)) {
262: graphChart2DVector.add(graphChart2D);
263: needsUpdateVector.add(new Boolean(true));
264: }
265: }
266:
267: /**
268: * Removes a GraphChart2D from the set of objects using these properties.
269: * @param graphChart2D The Object2D to remove.
270: */
271: final void removeGraphChart2D(GraphChart2D graphChart2D) {
272:
273: int index = -1;
274: if ((index = graphChart2DVector.indexOf(graphChart2D)) != -1) {
275: graphChart2DVector.remove(index);
276: needsUpdateVector.remove(index);
277: }
278: }
279:
280: /**
281: * Validates the properties of this object.
282: * If debug is true then prints a messages indicating whether each property is valid.
283: * Returns true if all the properties were valid and false otherwise.
284: * @param debug If true then will print status messages.
285: * @return If true then valid.
286: */
287: final boolean validate(boolean debug) {
288:
289: if (debug)
290: System.out.println("Validating WarningRegionProperties");
291:
292: boolean valid = true;
293:
294: if ((high != HIGH && (low == HIGH || high < low))
295: || (low != LOW && (high == LOW || high < low))) {
296: valid = false;
297: if (debug)
298: System.out.println("High was lower than low");
299: }
300: if (componentColor == null) {
301: valid = false;
302: if (debug)
303: System.out.println("ComponentColor == null");
304: }
305: if (backgroundColor == null) {
306: valid = false;
307: if (debug)
308: System.out.println("BackgroundColor == null");
309: }
310:
311: if (debug) {
312: if (valid)
313: System.out.println("WarningRegionProperties was valid");
314: else
315: System.out
316: .println("WarningRegionProperties was invalid");
317: }
318:
319: return valid;
320: }
321:
322: /**
323: * Updates the properties of this GraphChart2D.
324: * @param graphChart2D The object to update.
325: */
326: final void updateGraphChart2D(GraphChart2D graphChart2D) {
327:
328: if (getGraphChart2DNeedsUpdate(graphChart2D)) {
329:
330: if (needsUpdate) {
331: for (int i = 0; i < needsUpdateVector.size(); ++i) {
332: needsUpdateVector.set(i, new Boolean(true));
333: }
334: needsUpdate = false;
335: }
336:
337: int index = -1;
338: if ((index = graphChart2DVector.indexOf(graphChart2D)) != -1) {
339: needsUpdateVector.set(index, new Boolean(false));
340: }
341: }
342: }
343:
344: /**
345: * A convencience method for creating a WarningRegion set with these properties.
346: * @return The appropriately set warning region.
347: */
348: final WarningRegion configureWarningRegion() {
349:
350: WarningRegion warningRegion = new WarningRegion();
351: warningRegion.setHigh(getHigh());
352: warningRegion.setLow(getLow());
353: warningRegion.setComponentColor(getComponentColor());
354: warningRegion.setBackgroundExistence(getBackgroundExistence());
355: warningRegion.setBackgroundColor(getBackgroundColor());
356: return warningRegion;
357: }
358: }
|