Source for org.jfree.chart.plot.CategoryMarker

   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:  * CategoryMarker.java
  29:  * -------------------
  30:  * (C) Copyright 2005-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   Nicolas Brodu;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 20-May-2005 : Version 1 (DG);
  38:  * 19-Aug-2005 : Implemented equals(), fixed bug in constructor (DG);
  39:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  40:  * 05-Sep-2006 : Added MarkerChangeListener support (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.chart.plot;
  45: 
  46: import java.awt.BasicStroke;
  47: import java.awt.Color;
  48: import java.awt.Paint;
  49: import java.awt.Stroke;
  50: import java.io.Serializable;
  51: 
  52: import org.jfree.chart.event.MarkerChangeEvent;
  53: import org.jfree.ui.LengthAdjustmentType;
  54: 
  55: /**
  56:  * A marker for a category.
  57:  * <br><br>
  58:  * Note that for serialization to work correctly, the category key must be an
  59:  * instance of a serializable class.
  60:  * 
  61:  * @see CategoryPlot#addDomainMarker(CategoryMarker)
  62:  */
  63: public class CategoryMarker extends Marker implements Cloneable, Serializable {
  64: 
  65:     /** The category key. */
  66:     private Comparable key;
  67:     
  68:     /** 
  69:      * A hint that the marker should be drawn as a line rather than a region. 
  70:      */
  71:     private boolean drawAsLine = false;
  72:     
  73:     /**
  74:      * Creates a new category marker for the specified category.
  75:      * 
  76:      * @param key  the category key.
  77:      */
  78:     public CategoryMarker(Comparable key) {
  79:         this(key, Color.gray, new BasicStroke(1.0f));    
  80:     }
  81:     
  82:     /**
  83:      * Creates a new category marker.
  84:      * 
  85:      * @param key  the key.
  86:      * @param paint  the paint (<code>null</code> not permitted).
  87:      * @param stroke  the stroke (<code>null</code> not permitted).
  88:      */
  89:     public CategoryMarker(Comparable key, Paint paint, Stroke stroke) {
  90:         this(key, paint, stroke, paint, stroke, 1.0f);
  91:     }
  92:     
  93:     /**
  94:      * Creates a new category marker.
  95:      * 
  96:      * @param key  the key.
  97:      * @param paint  the paint (<code>null</code> not permitted).
  98:      * @param stroke  the stroke (<code>null</code> not permitted).
  99:      * @param outlinePaint  the outline paint (<code>null</code> permitted).
 100:      * @param outlineStroke  the outline stroke (<code>null</code> permitted).
 101:      * @param alpha  the alpha transparency.
 102:      */
 103:     public CategoryMarker(Comparable key, Paint paint, Stroke stroke, 
 104:                           Paint outlinePaint, Stroke outlineStroke, 
 105:                           float alpha) {
 106:         super(paint, stroke, outlinePaint, outlineStroke, alpha);
 107:         this.key = key;
 108:         setLabelOffsetType(LengthAdjustmentType.EXPAND);
 109:     }
 110:     
 111:     /**
 112:      * Returns the key.
 113:      * 
 114:      * @return The key.
 115:      */
 116:     public Comparable getKey() {
 117:         return this.key;   
 118:     }
 119:     
 120:     /**
 121:      * Sets the key and sends a {@link MarkerChangeEvent} to all registered
 122:      * listeners.
 123:      * 
 124:      * @param key  the key (<code>null</code> not permitted).
 125:      * 
 126:      * @since 1.0.3
 127:      */
 128:     public void setKey(Comparable key) {
 129:         if (key == null) {
 130:             throw new IllegalArgumentException("Null 'key' argument.");
 131:         }
 132:         this.key = key;
 133:         notifyListeners(new MarkerChangeEvent(this));
 134:     }
 135:     
 136:     /**
 137:      * Returns the flag that controls whether the marker is drawn as a region 
 138:      * or a line.
 139:      * 
 140:      * @return A line.
 141:      */
 142:     public boolean getDrawAsLine() {
 143:         return this.drawAsLine;   
 144:     }
 145:     
 146:     /**
 147:      * Sets the flag that controls whether the marker is drawn as a region or
 148:      * as a line, and sends a {@link MarkerChangeEvent} to all registered
 149:      * listeners.
 150:      * 
 151:      * @param drawAsLine  the flag.
 152:      */
 153:     public void setDrawAsLine(boolean drawAsLine) {
 154:         this.drawAsLine = drawAsLine;
 155:         notifyListeners(new MarkerChangeEvent(this));
 156:     }
 157:     
 158:     /**
 159:      * Tests the marker for equality with an arbitrary object.
 160:      * 
 161:      * @param obj  the object (<code>null</code> permitted).
 162:      * 
 163:      * @return A boolean.
 164:      */
 165:     public boolean equals(Object obj) {
 166:         if (obj == null) {
 167:             return false;
 168:         }
 169:         if (!(obj instanceof CategoryMarker)) {
 170:             return false;
 171:         }
 172:         if (!super.equals(obj)) {
 173:             return false;
 174:         }
 175:         CategoryMarker that = (CategoryMarker) obj;
 176:         if (!this.key.equals(that.key)) {
 177:             return false;
 178:         }
 179:         if (this.drawAsLine != that.drawAsLine) {
 180:             return false;
 181:         }
 182:         return true;
 183:     }
 184:     
 185: }