Source for org.jfree.chart.plot.MeterInterval

   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:  * MeterInterval.java
  29:  * ------------------
  30:  * (C) Copyright 2005, 2007, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 22-Mar-2005 : Version 1 (DG);
  38:  * 29-Mar-2005 : Fixed serialization (DG);
  39:  * 
  40:  */
  41: 
  42: package org.jfree.chart.plot;
  43: 
  44: import java.awt.BasicStroke;
  45: import java.awt.Color;
  46: import java.awt.Paint;
  47: import java.awt.Stroke;
  48: import java.io.IOException;
  49: import java.io.ObjectInputStream;
  50: import java.io.ObjectOutputStream;
  51: import java.io.Serializable;
  52: 
  53: import org.jfree.data.Range;
  54: import org.jfree.io.SerialUtilities;
  55: import org.jfree.util.ObjectUtilities;
  56: import org.jfree.util.PaintUtilities;
  57: 
  58: /**
  59:  * An interval to be highlighted on a {@link MeterPlot}.  Instances of this
  60:  * class are immutable.
  61:  */
  62: public class MeterInterval implements Serializable {
  63: 
  64:     /** For serialization. */
  65:     private static final long serialVersionUID = 1530982090622488257L;
  66:     
  67:     /** The interval label. */
  68:     private String label;
  69:     
  70:     /** The interval range. */
  71:     private Range range;
  72:     
  73:     /** The outline paint (used for the arc marking the interval). */
  74:     private transient Paint outlinePaint;
  75:     
  76:     /** The outline stroke (used for the arc marking the interval). */
  77:     private transient Stroke outlineStroke;
  78:     
  79:     /** The background paint for the interval. */
  80:     private transient Paint backgroundPaint;
  81:     
  82:     /**
  83:      * Creates a new interval.
  84:      * 
  85:      * @param label  the label (<code>null</code> not permitted).
  86:      * @param range  the range (<code>null</code> not permitted).
  87:      */
  88:     public MeterInterval(String label, Range range) {
  89:         this(label, range, Color.yellow, new BasicStroke(2.0f), null);
  90:     }
  91:     
  92:     /**
  93:      * Creates a new interval.
  94:      * 
  95:      * @param label  the label (<code>null</code> not permitted).
  96:      * @param range  the range (<code>null</code> not permitted).
  97:      * @param outlinePaint  the outline paint (<code>null</code> permitted).
  98:      * @param outlineStroke  the outline stroke (<code>null</code> permitted).
  99:      * @param backgroundPaint  the background paint (<code>null</code> 
 100:      *                         permitted).
 101:      */
 102:     public MeterInterval(String label, Range range, Paint outlinePaint, 
 103:                          Stroke outlineStroke, Paint backgroundPaint) {
 104:         if (label == null) {
 105:             throw new IllegalArgumentException("Null 'label' argument.");
 106:         }
 107:         if (range == null) {
 108:             throw new IllegalArgumentException("Null 'range' argument.");
 109:         }
 110:         this.label = label;
 111:         this.range = range;
 112:         this.outlinePaint = outlinePaint;
 113:         this.outlineStroke = outlineStroke;
 114:         this.backgroundPaint = backgroundPaint;
 115:     }
 116:     
 117:     /**
 118:      * Returns the label.
 119:      * 
 120:      * @return The label (never <code>null</code>).
 121:      */
 122:     public String getLabel() {
 123:         return this.label;
 124:     }
 125: 
 126:     /**
 127:      * Returns the range.
 128:      * 
 129:      * @return The range (never <code>null</code>).
 130:      */
 131:     public Range getRange() {
 132:         return this.range;
 133:     }
 134: 
 135:     /**
 136:      * Returns the background paint.  If <code>null</code>, the background
 137:      * should remain unfilled.
 138:      * 
 139:      * @return The background paint (possibly <code>null</code>).
 140:      */
 141:     public Paint getBackgroundPaint() {
 142:         return this.backgroundPaint;
 143:     }
 144:     
 145:     /**
 146:      * Returns the outline paint.
 147:      * 
 148:      * @return The outline paint (possibly <code>null</code>).
 149:      */
 150:     public Paint getOutlinePaint() {
 151:         return this.outlinePaint;
 152:     }
 153:     
 154:     /**
 155:      * Returns the outline stroke.
 156:      * 
 157:      * @return The outline stroke (possibly <code>null</code>).
 158:      */
 159:     public Stroke getOutlineStroke() {
 160:         return this.outlineStroke;
 161:     }
 162:     
 163:     /**
 164:      * Checks this instance for equality with an arbitrary object.
 165:      * 
 166:      * @param obj  the object (<code>null</code> permitted).
 167:      * 
 168:      * @return A boolean.
 169:      */
 170:     public boolean equals(Object obj) {
 171:         if (obj == this) {
 172:             return true;
 173:         }
 174:         if (!(obj instanceof MeterInterval)) {
 175:             return false;
 176:         }
 177:         MeterInterval that = (MeterInterval) obj;
 178:         if (!this.label.equals(that.label)) {
 179:             return false;
 180:         }
 181:         if (!this.range.equals(that.range)) {
 182:             return false;
 183:         }
 184:         if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) {
 185:             return false;
 186:         }
 187:         if (!ObjectUtilities.equal(this.outlineStroke, that.outlineStroke)) {
 188:             return false;
 189:         }
 190:         if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) {
 191:             return false;
 192:         }
 193:         return true;
 194:     }
 195:     
 196:     /**
 197:      * Provides serialization support.
 198:      *
 199:      * @param stream  the output stream.
 200:      *
 201:      * @throws IOException  if there is an I/O error.
 202:      */
 203:     private void writeObject(ObjectOutputStream stream) throws IOException {
 204:         stream.defaultWriteObject();
 205:         SerialUtilities.writePaint(this.outlinePaint, stream);
 206:         SerialUtilities.writeStroke(this.outlineStroke, stream);
 207:         SerialUtilities.writePaint(this.backgroundPaint, stream);
 208:     }
 209:     
 210:     /**
 211:      * Provides serialization support.
 212:      *
 213:      * @param stream  the input stream.
 214:      *
 215:      * @throws IOException  if there is an I/O error.
 216:      * @throws ClassNotFoundException  if there is a classpath problem.
 217:      */
 218:     private void readObject(ObjectInputStream stream) 
 219:         throws IOException, ClassNotFoundException {
 220:         stream.defaultReadObject();
 221:         this.outlinePaint = SerialUtilities.readPaint(stream);
 222:         this.outlineStroke = SerialUtilities.readStroke(stream);
 223:         this.backgroundPaint = SerialUtilities.readPaint(stream);
 224:     }
 225: 
 226: }