001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, 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 * CategoryItemRendererState.java
029 * ------------------------------
030 * (C) Copyright 2003-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Peter Kolb (patch 2497611);
034 *
035 */
036
037package org.jfree.chart.renderer.category;
038
039import org.jfree.chart.plot.CategoryCrosshairState;
040import org.jfree.chart.plot.PlotRenderingInfo;
041import org.jfree.chart.renderer.RendererState;
042
043/**
044 * An object that retains temporary state information for a
045 * {@link CategoryItemRenderer}.
046 */
047public class CategoryItemRendererState extends RendererState {
048
049    /** The bar width. */
050    private double barWidth;
051
052    /** The series running total. */
053    private double seriesRunningTotal;
054
055    /** The array with the indices of the visible series.*/
056    private int[] visibleSeries;
057
058    /**
059     * State information for crosshairs in the plot (this is updated by the
060     * renderer, but may be passed to several renderers in one chart).
061     */
062    private CategoryCrosshairState crosshairState;
063
064    /**
065     * Creates a new object for recording temporary state information for a
066     * renderer.
067     *
068     * @param info  the plot rendering info ({@code null} permitted).
069     */
070    public CategoryItemRendererState(PlotRenderingInfo info) {
071        super(info);
072        this.barWidth = 0.0;
073        this.seriesRunningTotal = 0.0;
074    }
075
076    /**
077     * Returns the bar width.
078     *
079     * @return The bar width.
080     *
081     * @see #setBarWidth(double)
082     */
083    public double getBarWidth() {
084        return this.barWidth;
085    }
086
087    /**
088     * Sets the bar width.  The renderer calculates this value and stores it
089     * here - it is not intended that users can manually set the bar width.
090     *
091     * @param width  the width.
092     *
093     * @see #getBarWidth()
094     */
095    public void setBarWidth(double width) {
096        this.barWidth = width;
097    }
098
099    /**
100     * Returns the series running total.
101     *
102     * @return The running total.
103     *
104     * @see #setSeriesRunningTotal(double)
105     */
106    public double getSeriesRunningTotal() {
107        return this.seriesRunningTotal;
108    }
109
110    /**
111     * Sets the series running total (this method is intended for the use of
112     * the renderer only).
113     *
114     * @param total  the new total.
115     *
116     * @see #getSeriesRunningTotal()
117     */
118    void setSeriesRunningTotal(double total) {
119        this.seriesRunningTotal = total;
120    }
121
122    /**
123     * Returns the crosshair state, if any.
124     *
125     * @return The crosshair state (possibly {@code null}).
126     *
127     * @see #setCrosshairState(CategoryCrosshairState)
128     */
129    public CategoryCrosshairState getCrosshairState() {
130        return this.crosshairState;
131    }
132
133    /**
134     * Sets the crosshair state.
135     *
136     * @param state  the new state ({@code null} permitted).
137     *
138     * @see #getCrosshairState()
139     */
140    public void setCrosshairState(CategoryCrosshairState state) {
141        this.crosshairState = state;
142    }
143
144    /**
145     * Returns the index of the row relative to the visible rows.  If no
146     * visible rows have been specified, the original row index is returned.
147     * If the row index is not included in the array of visible rows,
148     * -1 is returned.
149     *
150     * @param rowIndex  the row index.
151     *
152     * @return The new row index or -1.
153     */
154    public int getVisibleSeriesIndex(int rowIndex) {
155        if (this.visibleSeries == null) {
156            return rowIndex;
157        }
158        int index = -1;
159        for (int vRow = 0; vRow < this.visibleSeries.length; vRow++) {
160            if (this.visibleSeries[vRow] == rowIndex) {
161                index = vRow;
162                break;
163            }
164        }
165        return index;
166    }
167
168    /**
169     * Returns the number of visible series or -1 if no visible series have
170     * been specified.
171     *
172     * @return The number or -1.
173     */
174    public int getVisibleSeriesCount() {
175        if (this.visibleSeries == null) {
176            return -1;
177        }
178        return this.visibleSeries.length;
179    }
180
181    /**
182     * Returns a copy of the visible series array.
183     * 
184     * @return The visible series array (possibly {@code null}).
185     */
186    public int[] getVisibleSeriesArray() {
187        if (this.visibleSeries == null) {
188            return null;
189        }
190        int[] result = new int[this.visibleSeries.length];
191        System.arraycopy(this.visibleSeries, 0, result, 0,
192                this.visibleSeries.length);
193        return result;
194    }
195
196    /**
197     * Sets an array with the indices of the visible rows.
198     *
199     * @param visibleSeries the array ({@code null} permitted).
200     */
201    public void setVisibleSeriesArray(int[] visibleSeries) {
202        this.visibleSeries = visibleSeries;
203    }
204
205}