001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, by David Gilbert 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 * IntervalMarker.java
029 * -------------------
030 * (C) Copyright 2002-2022, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
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;
044import java.util.Objects;
045import org.jfree.chart.api.LengthAdjustmentType;
046
047import org.jfree.chart.event.MarkerChangeEvent;
048import org.jfree.chart.util.GradientPaintTransformer;
049
050/**
051 * Represents an interval to be highlighted in some way.
052 */
053public class IntervalMarker extends Marker implements Cloneable, Serializable {
054
055    /** For serialization. */
056    private static final long serialVersionUID = -1762344775267627916L;
057
058    /** The start value. */
059    private double startValue;
060
061    /** The end value. */
062    private double endValue;
063
064    /** The gradient paint transformer (optional). */
065    private GradientPaintTransformer gradientPaintTransformer;
066
067    /**
068     * Constructs an interval marker.
069     *
070     * @param start  the start of the interval.
071     * @param end  the end of the interval.
072     */
073    public IntervalMarker(double start, double end) {
074        this(start, end, Color.GRAY, new BasicStroke(0.5f), Color.GRAY,
075                new BasicStroke(0.5f), 0.8f);
076    }
077
078    /**
079     * Creates a new interval marker with the specified range and fill paint.
080     * The outline paint and stroke default to {@code null}.
081     *
082     * @param start  the lower bound of the interval.
083     * @param end  the upper bound of the interval.
084     * @param paint  the fill paint ({@code null} not permitted).
085     */
086    public IntervalMarker(double start, double end, Paint paint) {
087        this(start, end, paint, new BasicStroke(0.5f), null, null, 0.8f);
088    }
089
090    /**
091     * Constructs an interval marker.
092     *
093     * @param start  the start of the interval.
094     * @param end  the end of the interval.
095     * @param paint  the paint ({@code null} not permitted).
096     * @param stroke  the stroke ({@code null} not permitted).
097     * @param outlinePaint  the outline paint.
098     * @param outlineStroke  the outline stroke.
099     * @param alpha  the alpha transparency.
100     */
101    public IntervalMarker(double start, double end,
102                          Paint paint, Stroke stroke,
103                          Paint outlinePaint, Stroke outlineStroke,
104                          float alpha) {
105
106        super(paint, stroke, outlinePaint, outlineStroke, alpha);
107        this.startValue = start;
108        this.endValue = end;
109        this.gradientPaintTransformer = null;
110        setLabelOffsetType(LengthAdjustmentType.CONTRACT);
111    }
112
113    /**
114     * Returns the start value for the interval.
115     *
116     * @return The start value.
117     */
118    public double getStartValue() {
119        return this.startValue;
120    }
121
122    /**
123     * Sets the start value for the marker and sends a
124     * {@link MarkerChangeEvent} to all registered listeners.
125     *
126     * @param value  the value.
127     */
128    public void setStartValue(double value) {
129        this.startValue = value;
130        notifyListeners(new MarkerChangeEvent(this));
131    }
132
133    /**
134     * Returns the end value for the interval.
135     *
136     * @return The end value.
137     */
138    public double getEndValue() {
139        return this.endValue;
140    }
141
142    /**
143     * Sets the end value for the marker and sends a
144     * {@link MarkerChangeEvent} to all registered listeners.
145     *
146     * @param value  the value.
147     */
148    public void setEndValue(double value) {
149        this.endValue = value;
150        notifyListeners(new MarkerChangeEvent(this));
151    }
152
153    /**
154     * Returns the gradient paint transformer.
155     *
156     * @return The gradient paint transformer (possibly {@code null}).
157     */
158    public GradientPaintTransformer getGradientPaintTransformer() {
159        return this.gradientPaintTransformer;
160    }
161
162    /**
163     * Sets the gradient paint transformer and sends a
164     * {@link MarkerChangeEvent} to all registered listeners.
165     *
166     * @param transformer  the transformer ({@code null} permitted).
167     */
168    public void setGradientPaintTransformer(
169            GradientPaintTransformer transformer) {
170        this.gradientPaintTransformer = transformer;
171        notifyListeners(new MarkerChangeEvent(this));
172    }
173
174    /**
175     * Tests the marker for equality with an arbitrary object.
176     *
177     * @param obj  the object ({@code null} permitted).
178     *
179     * @return A boolean.
180     */
181    @Override
182    public boolean equals(Object obj) {
183        if (obj == this) {
184            return true;
185        }
186        if (!(obj instanceof IntervalMarker)) {
187            return false;
188        }
189        if (!super.equals(obj)) {
190            return false;
191        }
192        IntervalMarker that = (IntervalMarker) obj;
193        if (this.startValue != that.startValue) {
194            return false;
195        }
196        if (this.endValue != that.endValue) {
197            return false;
198        }
199        if (!Objects.equals(this.gradientPaintTransformer, that.gradientPaintTransformer)) {
200            return false;
201        }
202        return true;
203    }
204
205    /**
206     * Returns a clone of the marker.
207     *
208     * @return A clone.
209     *
210     * @throws CloneNotSupportedException Not thrown by this class, but the
211     *         exception is declared for the use of subclasses.
212     */
213    @Override
214    public Object clone() throws CloneNotSupportedException {
215        return super.clone();
216    }
217
218}