Source for org.jfree.chart.plot.dial.DialBackground

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, 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:  * DialBackground.java
  29:  * -------------------
  30:  * (C) Copyright 2006, 2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 03-Nov-2006 : Version 1 (DG);
  38:  * 16-Oct-2007 : The equals() method needs to call super.equals() (DG);
  39:  * 
  40:  */
  41: 
  42: package org.jfree.chart.plot.dial;
  43: 
  44: import java.awt.Color;
  45: import java.awt.GradientPaint;
  46: import java.awt.Graphics2D;
  47: import java.awt.Paint;
  48: import java.awt.geom.Rectangle2D;
  49: import java.io.IOException;
  50: import java.io.ObjectInputStream;
  51: import java.io.ObjectOutputStream;
  52: import java.io.Serializable;
  53: 
  54: import org.jfree.chart.HashUtilities;
  55: import org.jfree.io.SerialUtilities;
  56: import org.jfree.ui.GradientPaintTransformer;
  57: import org.jfree.ui.StandardGradientPaintTransformer;
  58: import org.jfree.util.PaintUtilities;
  59: import org.jfree.util.PublicCloneable;
  60: 
  61: /**
  62:  * A regular dial layer that can be used to draw the background for a dial.
  63:  * 
  64:  * @since 1.0.7
  65:  */
  66: public class DialBackground extends AbstractDialLayer implements DialLayer, 
  67:         Cloneable, PublicCloneable, Serializable {
  68:     
  69:     /** For serialization. */
  70:     static final long serialVersionUID = -9019069533317612375L;
  71: 
  72:     /** 
  73:      * The background paint.  This field is transient because serialization
  74:      * requires special handling.
  75:      */
  76:     private transient Paint paint;
  77:     
  78:     /** 
  79:      * The transformer used when the background paint is an instance of
  80:      * <code>GradientPaint</code>. 
  81:      */
  82:     private GradientPaintTransformer gradientPaintTransformer;
  83:     
  84:     /** 
  85:      * Creates a new instance of <code>DialBackground</code>.  The 
  86:      * default background paint is <code>Color.white</code>.
  87:      */
  88:     public DialBackground() {
  89:         this(Color.white);
  90:     }
  91:     
  92:     /**
  93:      * Creates a new instance of <code>DialBackground</code>.  The 
  94:      *
  95:      * @param paint  the paint (<code>null</code> not permitted).
  96:      *
  97:      * @throws IllegalArgumentException if <code>paint</code> is 
  98:      *     <code>null</code>.
  99:      */
 100:     public DialBackground(Paint paint) {
 101:         if (paint == null) {
 102:             throw new IllegalArgumentException("Null 'paint' argument.");
 103:         }
 104:         this.paint = paint;
 105:         this.gradientPaintTransformer = new StandardGradientPaintTransformer();
 106:     }
 107:     
 108:     /**
 109:      * Returns the paint used to fill the background. 
 110:      *
 111:      * @return The paint (never <code>null</code>).
 112:      *
 113:      * @see #setPaint(Paint)
 114:      */
 115:     public Paint getPaint() {
 116:         return this.paint;
 117:     }
 118:     
 119:     /**
 120:      * Sets the paint for the dial background and sends a 
 121:      * {@link DialLayerChangeEvent} to all registered listeners.
 122:      *
 123:      * @param paint  the paint (<code>null</code> not permitted).
 124:      *
 125:      * @see #getPaint()
 126:      */
 127:     public void setPaint(Paint paint) {
 128:         if (paint == null) {
 129:             throw new IllegalArgumentException("Null 'paint' argument.");
 130:         }
 131:         this.paint = paint;
 132:         notifyListeners(new DialLayerChangeEvent(this));
 133:     }
 134:     
 135:     /**
 136:      * Returns the transformer used to adjust the coordinates of any 
 137:      * <code>GradientPaint</code> instance used for the background paint.
 138:      *
 139:      * @return The transformer (never <code>null</code>).
 140:      *
 141:      * @see #setGradientPaintTransformer(GradientPaintTransformer)
 142:      */
 143:     public GradientPaintTransformer getGradientPaintTransformer() {
 144:         return this.gradientPaintTransformer;
 145:     }
 146:     
 147:     /**
 148:      * Sets the transformer used to adjust the coordinates of any
 149:      * <code>GradientPaint</code> instance used for the background paint, and
 150:      * sends a {@link DialLayerChangeEvent} to all registered listeners.
 151:      *
 152:      * @param t  the transformer (<code>null</code> not permitted).
 153:      *
 154:      * @see #getGradientPaintTransformer()
 155:      */
 156:     public void setGradientPaintTransformer(GradientPaintTransformer t) {
 157:         if (t == null) {
 158:             throw new IllegalArgumentException("Null 't' argument.");
 159:         }
 160:         this.gradientPaintTransformer = t;
 161:         notifyListeners(new DialLayerChangeEvent(this));
 162:     }
 163:     
 164:     /**
 165:      * Returns <code>true</code> to indicate that this layer should be 
 166:      * clipped within the dial window. 
 167:      *
 168:      * @return <code>true</code>.
 169:      */
 170:     public boolean isClippedToWindow() {
 171:         return true;
 172:     }
 173:     
 174:     /**
 175:      * Draws the background to the specified graphics device.  If the dial
 176:      * frame specifies a window, the clipping region will already have been 
 177:      * set to this window before this method is called.
 178:      *
 179:      * @param g2  the graphics device (<code>null</code> not permitted).
 180:      * @param plot  the plot (ignored here).
 181:      * @param frame  the dial frame (ignored here).
 182:      * @param view  the view rectangle (<code>null</code> not permitted). 
 183:      */
 184:     public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, 
 185:             Rectangle2D view) {
 186: 
 187:         Paint p = this.paint;
 188:         if (p instanceof GradientPaint) {
 189:             p = this.gradientPaintTransformer.transform((GradientPaint) p, 
 190:                     view);
 191:         }
 192:         g2.setPaint(p);
 193:         g2.fill(view);
 194:     }
 195:     
 196:     /**
 197:      * Tests this instance for equality with an arbitrary object.
 198:      *
 199:      * @param obj  the object (<code>null</code> permitted).
 200:      *
 201:      * @return A boolean.
 202:      */
 203:     public boolean equals(Object obj) {
 204:         if (obj == this) {
 205:             return true;
 206:         }
 207:         if (!(obj instanceof DialBackground)) {
 208:             return false;
 209:         }
 210:         DialBackground that = (DialBackground) obj;
 211:         if (!PaintUtilities.equal(this.paint, that.paint)) {
 212:             return false;
 213:         }
 214:         if (!this.gradientPaintTransformer.equals(
 215:                 that.gradientPaintTransformer)) {
 216:             return false;
 217:         }
 218:         return super.equals(obj);
 219:     }
 220:     
 221:     /**
 222:      * Returns a hash code for this instance.
 223:      * 
 224:      * @return The hash code.
 225:      */
 226:     public int hashCode() {
 227:         int result = 193;
 228:         result = 37 * result + HashUtilities.hashCodeForPaint(this.paint);
 229:         result = 37 * result + this.gradientPaintTransformer.hashCode();
 230:         return result;
 231:     }
 232:     
 233:     /**
 234:      * Returns a clone of this instance.
 235:      *
 236:      * @return The clone.
 237:      *
 238:      * @throws CloneNotSupportedException if some attribute of this instance
 239:      *     cannot be cloned.
 240:      */
 241:     public Object clone() throws CloneNotSupportedException {
 242:         return super.clone();
 243:     }
 244:     
 245:     /**
 246:      * Provides serialization support.
 247:      *
 248:      * @param stream  the output stream.
 249:      *
 250:      * @throws IOException  if there is an I/O error.
 251:      */
 252:     private void writeObject(ObjectOutputStream stream) throws IOException {
 253:         stream.defaultWriteObject();
 254:         SerialUtilities.writePaint(this.paint, stream);
 255:     }
 256: 
 257:     /**
 258:      * Provides serialization support.
 259:      *
 260:      * @param stream  the input stream.
 261:      *
 262:      * @throws IOException  if there is an I/O error.
 263:      * @throws ClassNotFoundException  if there is a classpath problem.
 264:      */
 265:     private void readObject(ObjectInputStream stream) 
 266:             throws IOException, ClassNotFoundException {
 267:         stream.defaultReadObject();
 268:         this.paint = SerialUtilities.readPaint(stream);
 269:     }
 270:     
 271: }