001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -------------------
028 * CategoryMarker.java
029 * -------------------
030 * (C) Copyright 2005-2013, 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 * 02-Jul-2013 : Use ParamChecks (DG);
042 *
043 */
044
045package org.jfree.chart.plot;
046
047import java.awt.BasicStroke;
048import java.awt.Color;
049import java.awt.Paint;
050import java.awt.Stroke;
051import java.io.Serializable;
052
053import org.jfree.chart.event.MarkerChangeEvent;
054import org.jfree.chart.util.ParamChecks;
055import 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 */
065public 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        ParamChecks.nullNotPermitted(key, "key");
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    @Override
166    public boolean equals(Object obj) {
167        if (obj == null) {
168            return false;
169        }
170        if (!(obj instanceof CategoryMarker)) {
171            return false;
172        }
173        if (!super.equals(obj)) {
174            return false;
175        }
176        CategoryMarker that = (CategoryMarker) obj;
177        if (!this.key.equals(that.key)) {
178            return false;
179        }
180        if (this.drawAsLine != that.drawAsLine) {
181            return false;
182        }
183        return true;
184    }
185
186}