    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2005, 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     * PlotRenderingInfo.java
    029     * ----------------------
    030     * (C) Copyright 2003-2005, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: PlotRenderingInfo.java,v 1.5.2.2 2005/11/02 12:56:10 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 16-Sep-2003 : Version 1 (DG);
    040     * 23-Sep-2003 : Added Javadocs (DG);
    041     * 12-Nov-2004 : Added getSubplotCount() and findSubplot() methods (DG);
    042     * 01-Nov-2005 : Made 'owner' non-transient to fix bug 1344048 (DG);
    043     *
    044     */
    045
    046    package org.jfree.chart.plot;
    047
    048    import java.awt.geom.Point2D;
    049    import java.awt.geom.Rectangle2D;
    050    import java.io.IOException;
    051    import java.io.ObjectInputStream;
    052    import java.io.ObjectOutputStream;
    053    import java.io.Serializable;
    054    import java.util.List;
    055
    056    import org.jfree.chart.ChartRenderingInfo;
    057    import org.jfree.io.SerialUtilities;
    058    import org.jfree.util.ObjectUtilities;
    059
    060    /**
    061     * Stores information about the dimensions of a plot and its subplots.
    062     */
    063    public class PlotRenderingInfo implements Cloneable, Serializable {
    064
    065        /** For serialization. */
    066        private static final long serialVersionUID = 8446720134379617220L;
    067
    068        /** The owner of this info. */
    069        private ChartRenderingInfo owner;
    070
    071        /** The plot area. */
    072        private transient Rectangle2D plotArea;
    073
    074        /** The data area. */
    075        private transient Rectangle2D dataArea;
    076
    077        /**
    078         * Storage for the plot rendering info objects belonging to the subplots.
    079         */
    080        private List subplotInfo;
    081
    082        /**
    083         * Default constructor.
    084         *
    085         * @param owner  the owner.
    086         */
    087        public PlotRenderingInfo(ChartRenderingInfo owner) {
    088            this.owner = owner;
    089            this.dataArea = new Rectangle2D.Double();
    090            this.subplotInfo = new java.util.ArrayList();
    091        }
    092
    093        /**
    094         * Returns the owner.
    095         *
    096         * @return The owner.
    097         */
    098        public ChartRenderingInfo getOwner() {
    099            return this.owner;
    100        }
    101
    102        /**
    103         * Returns the plot area (in Java2D space).
    104         *
    105         * @return The plot area.
    106         */
    107        public Rectangle2D getPlotArea() {
    108            return this.plotArea;
    109        }
    110
    111        /**
    112         * Sets the plot area.
    113         *
    114         * @param area  the plot area (in Java2D space)
    115         */
    116        public void setPlotArea(Rectangle2D area) {
    117            this.plotArea = area;
    118        }
    119
    120        /**
    121         * Returns the plot's data area (in Java2D space).
    122         *
    123         * @return The data area.
    124         */
    125        public Rectangle2D getDataArea() {
    126            return this.dataArea;
    127        }
    128
    129        /**
    130         * Sets the data area.
    131         *
    132         * @param area  the data area (in Java2D space).
    133         */
    134        public void setDataArea(Rectangle2D area) {
    135            this.dataArea = area;
    136        }
    137
    138        /**
    139         * Returns the number of subplots.
    140         *
    141         * @return The subplot count.
    142         */
    143        public int getSubplotCount() {
    144            return this.subplotInfo.size();
    145        }
    146
    147        /**
    148         * Adds the info for a subplot.
    149         *
    150         * @param info  the subplot info.
    151         */
    152        public void addSubplotInfo(PlotRenderingInfo info) {
    153            this.subplotInfo.add(info);
    154        }
    155
    156        /**
    157         * Returns the info for a subplot.
    158         *
    159         * @param index  the subplot index.
    160         *
    161         * @return The info.
    162         */
    163        public PlotRenderingInfo getSubplotInfo(int index) {
    164            return (PlotRenderingInfo) this.subplotInfo.get(index);
    165        }
    166
    167        /**
    168         * Returns the index of the subplot that contains the specified
    169         * (x, y) point (the "source" point).  The source point will usually
    170         * come from a mouse click on a {@link org.jfree.chart.ChartPanel},
    171         * and this method is then used to determine the subplot that
    172         * contains the source point.
    173         *
    174         * @param source  the source point (in Java2D space).
    175         *
    176         * @return The subplot index (or -1 if no subplot contains the point).
    177         */
    178        public int getSubplotIndex(Point2D source) {
    179            int subplotCount = getSubplotCount();
    180            for (int i = 0; i < subplotCount; i++) {
    181                PlotRenderingInfo info = getSubplotInfo(i);
    182                Rectangle2D area = info.getDataArea();
    183                if (area.contains(source)) {
    184                    return i;
    185                }
    186            }
    187            return -1;
    188        }
    189
    190        /**
    191         * Tests this instance for equality against an arbitrary object.
    192         *
    193         * @param obj  the object (<code>null</code> permitted).
    194         *
    195         * @return A boolean.
    196         */
    197        public boolean equals(Object obj) {
    198            if (this == obj) {
    199                return true;
    200            }
    201            if (!(obj instanceof PlotRenderingInfo)) {
    202                return false;
    203            }
    204            PlotRenderingInfo that = (PlotRenderingInfo) obj;
    205            if (!ObjectUtilities.equal(this.dataArea, that.dataArea)) {
    206                return false;
    207            }
    208            if (!ObjectUtilities.equal(this.plotArea, that.plotArea)) {
    209                return false;
    210            }
    211            if (!ObjectUtilities.equal(this.subplotInfo, that.subplotInfo)) {
    212                return false;
    213            }
    214            return true;
    215        }
    216
    217        /**
    218         * Returns a clone of this object.
    219         *
    220         * @return A clone.
    221         *
    222         * @throws CloneNotSupportedException if there is a problem cloning.
    223         */
    224        public Object clone() throws CloneNotSupportedException {
    225            return super.clone();
    226        }
    227
    228        /**
    229         * Provides serialization support.
    230         *
    231         * @param stream  the output stream.
    232         *
    233         * @throws IOException  if there is an I/O error.
    234         */
    235        private void writeObject(ObjectOutputStream stream) throws IOException {
    236            stream.defaultWriteObject();
    237            SerialUtilities.writeShape(this.dataArea, stream);
    238            SerialUtilities.writeShape(this.plotArea, stream);
    239        }
    240
    241        /**
    242         * Provides serialization support.
    243         *
    244         * @param stream  the input stream.
    245         *
    246         * @throws IOException  if there is an I/O error.
    247         * @throws ClassNotFoundException  if there is a classpath problem.
    248         */
    249        private void readObject(ObjectInputStream stream)
    250                throws IOException, ClassNotFoundException {
    251            stream.defaultReadObject();
    252            this.dataArea = (Rectangle2D) SerialUtilities.readShape(stream);
    253            this.plotArea = (Rectangle2D) SerialUtilities.readShape(stream);
    254        }
    255
    256    }