001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ------------------- 028 * CategoryMarker.java 029 * ------------------- 030 * (C) Copyright 2005-2008, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Nicolas Brodu; 034 * 035 * Changes 036 * ------- 037 * 20-May-2005 : Version 1 (DG); 038 * 19-Aug-2005 : Implemented equals(), fixed bug in constructor (DG); 039 * ------------- JFREECHART 1.0.x --------------------------------------------- 040 * 05-Sep-2006 : Added MarkerChangeListener support (DG); 041 * 042 */ 043 044 package org.jfree.chart.plot; 045 046 import java.awt.BasicStroke; 047 import java.awt.Color; 048 import java.awt.Paint; 049 import java.awt.Stroke; 050 import java.io.Serializable; 051 052 import org.jfree.chart.event.MarkerChangeEvent; 053 import org.jfree.ui.LengthAdjustmentType; 054 055 /** 056 * A marker for a category. 057 * <br><br> 058 * Note that for serialization to work correctly, the category key must be an 059 * instance of a serializable class. 060 * 061 * @see CategoryPlot#addDomainMarker(CategoryMarker) 062 */ 063 public class CategoryMarker extends Marker implements Cloneable, Serializable { 064 065 /** The category key. */ 066 private Comparable key; 067 068 /** 069 * A hint that the marker should be drawn as a line rather than a region. 070 */ 071 private boolean drawAsLine = false; 072 073 /** 074 * Creates a new category marker for the specified category. 075 * 076 * @param key the category key. 077 */ 078 public CategoryMarker(Comparable key) { 079 this(key, Color.gray, new BasicStroke(1.0f)); 080 } 081 082 /** 083 * Creates a new category marker. 084 * 085 * @param key the key. 086 * @param paint the paint (<code>null</code> not permitted). 087 * @param stroke the stroke (<code>null</code> not permitted). 088 */ 089 public CategoryMarker(Comparable key, Paint paint, Stroke stroke) { 090 this(key, paint, stroke, paint, stroke, 1.0f); 091 } 092 093 /** 094 * Creates a new category marker. 095 * 096 * @param key the key. 097 * @param paint the paint (<code>null</code> not permitted). 098 * @param stroke the stroke (<code>null</code> not permitted). 099 * @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 }