KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > experimental > chart > renderer > LookupPaintScale


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------------
28  * LookupPaintScale.java
29  * ---------------------
30  * (C) Copyright 2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: LookupPaintScale.java,v 1.1.2.1 2006/10/12 09:30:49 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 05-Jul-2006 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.experimental.chart.renderer;
44
45 import java.awt.Color JavaDoc;
46 import java.awt.Paint JavaDoc;
47 import java.io.Serializable JavaDoc;
48 import java.util.List JavaDoc;
49
50 import org.jfree.util.PaintUtilities;
51 import org.jfree.util.PublicCloneable;
52
53 /**
54  * A paint scale that uses a lookup table to associate paint instances
55  * with data value ranges.
56  *
57  * WARNING: THIS CLASS IS NOT PART OF THE STANDARD JFREECHART API AND IS
58  * SUBJECT TO ALTERATION OR REMOVAL. DO NOT RELY ON THIS CLASS FOR
59  * PRODUCTION USE. Please experiment with this code and provide feedback.
60  */

61 public class LookupPaintScale
62         implements PaintScale, PublicCloneable, Serializable JavaDoc {
63
64     /**
65      * Stores the paint for a value.
66      */

67     class PaintItem {
68         
69         Number JavaDoc value;
70         
71         Paint JavaDoc paint;
72         
73         /**
74          * Creates a new instance.
75          *
76          * @param value the value.
77          * @param paint the paint.
78          */

79         public PaintItem(Number JavaDoc value, Paint JavaDoc paint) {
80             this.value = value;
81             this.paint = paint;
82         }
83     }
84     
85     private Paint JavaDoc defaultPaint;
86     
87     private List JavaDoc lookupTable;
88     
89     /**
90      * Creates a new paint scale.
91      */

92     public LookupPaintScale() {
93         this.defaultPaint = Color.lightGray;
94         this.lookupTable = new java.util.ArrayList JavaDoc();
95     }
96     
97     /**
98      * Adds an entry to the lookup table.
99      *
100      * @param n the data value.
101      * @param p the paint.
102      */

103     public void add(Number JavaDoc n, Paint JavaDoc p) {
104         this.lookupTable.add(new PaintItem(n, p));
105     }
106     
107     /**
108      * Returns the paint associated with the specified value.
109      *
110      * @param value the value.
111      *
112      * @return The paint.
113      */

114     public Paint JavaDoc getPaint(double value) {
115         Paint JavaDoc result = defaultPaint;
116         int index = this.lookupTable.size();
117         boolean done = false;
118         while (index > 0 && !done) {
119             PaintItem item = (PaintItem) lookupTable.get(--index);
120             if (value >= item.value.doubleValue()) {
121                 result = item.paint;
122                 done = true;
123             }
124         }
125         return result;
126     }
127     
128     /**
129      * Tests this instance for equality with an arbitrary object.
130      *
131      * @param obj the object (<code>null</code> permitted).
132      *
133      * @return A boolean.
134      */

135     public boolean equals(Object JavaDoc obj) {
136         if (obj == this) {
137             return true;
138         }
139         if (!(obj instanceof LookupPaintScale)) {
140             return false;
141         }
142         LookupPaintScale that = (LookupPaintScale) obj;
143         if (!PaintUtilities.equal(this.defaultPaint, that.defaultPaint)) {
144             return false;
145         }
146         return true;
147     }
148     
149     /**
150      * Returns a clone of the instance.
151      *
152      * @return A clone.
153      *
154      * @throws CloneNotSupportedException if there is a problem cloning the
155      * instance.
156      */

157     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
158         return super.clone();
159     }
160
161 }
162
Popular Tags