001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2006, 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, 2006, by Object Refinery Limited.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): Nicolas Brodu;
034 *
035 * $Id: CategoryMarker.java,v 1.1.2.4 2006/10/24 15:39:20 mungady Exp $
036 *
037 * Changes
038 * -------
039 * 20-May-2005 : Version 1 (DG);
040 * 19-Aug-2005 : Implemented equals(), fixed bug in constructor (DG);
041 * ------------- JFREECHART 1.0.x ---------------------------------------------
042 * 05-Sep-2006 : Added MarkerChangeListener support (DG);
043 *
044 */
045
046 package org.jfree.chart.plot;
047
048 import java.awt.BasicStroke;
049 import java.awt.Color;
050 import java.awt.Paint;
051 import java.awt.Stroke;
052 import java.io.Serializable;
053
054 import org.jfree.chart.event.MarkerChangeEvent;
055 import org.jfree.ui.LengthAdjustmentType;
056
057 /**
058 * A marker for a category.
059 * <br><br>
060 * Note that for serialization to work correctly, the category key must be an
061 * instance of a serializable class.
062 *
063 * @see CategoryPlot#addDomainMarker(CategoryMarker)
064 */
065 public class CategoryMarker extends Marker implements Cloneable, Serializable {
066
067 /** The category key. */
068 private Comparable key;
069
070 /**
071 * A hint that the marker should be drawn as a line rather than a region.
072 */
073 private boolean drawAsLine = false;
074
075 /**
076 * Creates a new category marker for the specified category.
077 *
078 * @param key the category key.
079 */
080 public CategoryMarker(Comparable key) {
081 this(key, Color.gray, new BasicStroke(1.0f));
082 }
083
084 /**
085 * Creates a new category marker.
086 *
087 * @param key the key.
088 * @param paint the paint (<code>null</code> not permitted).
089 * @param stroke the stroke (<code>null</code> not permitted).
090 */
091 public CategoryMarker(Comparable key, Paint paint, Stroke stroke) {
092 this(key, paint, stroke, paint, stroke, 1.0f);
093 }
094
095 /**
096 * Creates a new category marker.
097 *
098 * @param key the key.
099 * @param paint the paint (<code>null</code> not permitted).
100 * @param stroke the stroke (<code>null</code> not permitted).
101 * @param outlinePaint the outline paint (<code>null</code> permitted).
102 * @param outlineStroke the outline stroke (<code>null</code> permitted).
103 * @param alpha the alpha transparency.
104 */
105 public CategoryMarker(Comparable key, Paint paint, Stroke stroke,
106 Paint outlinePaint, Stroke outlineStroke,
107 float alpha) {
108 super(paint, stroke, outlinePaint, outlineStroke, alpha);
109 this.key = key;
110 setLabelOffsetType(LengthAdjustmentType.EXPAND);
111 }
112
113 /**
114 * Returns the key.
115 *
116 * @return The key.
117 */
118 public Comparable getKey() {
119 return this.key;
120 }
121
122 /**
123 * Sets the key and sends a {@link MarkerChangeEvent} to all registered
124 * listeners.
125 *
126 * @param key the key (<code>null</code> not permitted).
127 *
128 * @since 1.0.3
129 */
130 public void setKey(Comparable key) {
131 if (key == null) {
132 throw new IllegalArgumentException("Null 'key' argument.");
133 }
134 this.key = key;
135 notifyListeners(new MarkerChangeEvent(this));
136 }
137
138 /**
139 * Returns the flag that controls whether the marker is drawn as a region
140 * or a line.
141 *
142 * @return A line.
143 */
144 public boolean getDrawAsLine() {
145 return this.drawAsLine;
146 }
147
148 /**
149 * Sets the flag that controls whether the marker is drawn as a region or
150 * as a line, and sends a {@link MarkerChangeEvent} to all registered
151 * listeners.
152 *
153 * @param drawAsLine the flag.
154 */
155 public void setDrawAsLine(boolean drawAsLine) {
156 this.drawAsLine = drawAsLine;
157 notifyListeners(new MarkerChangeEvent(this));
158 }
159
160 /**
161 * Tests the marker for equality with an arbitrary object.
162 *
163 * @param obj the object (<code>null</code> permitted).
164 *
165 * @return A boolean.
166 */
167 public boolean equals(Object obj) {
168 if (obj == null) {
169 return false;
170 }
171 if (!(obj instanceof CategoryMarker)) {
172 return false;
173 }
174 if (!super.equals(obj)) {
175 return false;
176 }
177 CategoryMarker that = (CategoryMarker) obj;
178 if (!this.key.equals(that.key)) {
179 return false;
180 }
181 if (this.drawAsLine != that.drawAsLine) {
182 return false;
183 }
184 return true;
185 }
186
187 }