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