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