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 * MouseWheelHandler.java
029 * ----------------------
030 * (C) Copyright 2009-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Ulrich Voigt - patch 2686040;
034 *                   Jim Goodwin - bug fix;
035 *
036 */
037
038package org.jfree.chart;
039
040import java.awt.event.MouseWheelEvent;
041import java.awt.event.MouseWheelListener;
042import java.awt.geom.Point2D;
043import java.io.Serializable;
044
045import org.jfree.chart.plot.PiePlot;
046import org.jfree.chart.plot.Plot;
047import org.jfree.chart.plot.PlotRenderingInfo;
048import org.jfree.chart.plot.Zoomable;
049
050/**
051 * A class that handles mouse wheel events for the {@link ChartPanel} class.
052 */
053public class MouseWheelHandler implements MouseWheelListener, Serializable {
054
055    /** The chart panel. */
056    private ChartPanel chartPanel;
057
058    /** The zoom factor. */
059    double zoomFactor;
060
061    /**
062     * Creates a new instance for the specified chart panel.
063     *
064     * @param chartPanel  the chart panel ({@code null} not permitted).
065     */
066    public MouseWheelHandler(ChartPanel chartPanel) {
067        this.chartPanel = chartPanel;
068        this.zoomFactor = 0.10;
069        this.chartPanel.addMouseWheelListener(this);
070    }
071
072    /**
073     * Returns the current zoom factor.  The default value is 0.10 (ten
074     * percent).
075     *
076     * @return The zoom factor.
077     *
078     * @see #setZoomFactor(double)
079     */
080    public double getZoomFactor() {
081        return this.zoomFactor;
082    }
083
084    /**
085     * Sets the zoom factor.
086     *
087     * @param zoomFactor  the zoom factor.
088     *
089     * @see #getZoomFactor()
090     */
091    public void setZoomFactor(double zoomFactor) {
092        this.zoomFactor = zoomFactor;
093    }
094
095    /**
096     * Handles a mouse wheel event from the underlying chart panel.
097     *
098     * @param e  the event.
099     */
100    @Override
101    public void mouseWheelMoved(MouseWheelEvent e) {
102        JFreeChart chart = this.chartPanel.getChart();
103        if (chart == null) {
104            return;
105        }
106        Plot plot = chart.getPlot();
107        if (plot instanceof Zoomable) {
108            Zoomable zoomable = (Zoomable) plot;
109            handleZoomable(zoomable, e);
110        }
111        else if (plot instanceof PiePlot) {
112            PiePlot pp = (PiePlot) plot;
113            pp.handleMouseWheelRotation(e.getWheelRotation());
114        }
115    }
116
117    /**
118     * Handle the case where a plot implements the {@link Zoomable} interface.
119     *
120     * @param zoomable  the zoomable plot.
121     * @param e  the mouse wheel event.
122     */
123    private void handleZoomable(Zoomable zoomable, MouseWheelEvent e) {
124        // don't zoom unless the mouse pointer is in the plot's data area
125        ChartRenderingInfo info = this.chartPanel.getChartRenderingInfo();
126        PlotRenderingInfo pinfo = info.getPlotInfo();
127        Point2D p = this.chartPanel.translateScreenToJava2D(e.getPoint());
128        if (!pinfo.getDataArea().contains(p)) {
129            return;
130        }
131
132        Plot plot = (Plot) zoomable;
133        // do not notify while zooming each axis
134        boolean notifyState = plot.isNotify();
135        plot.setNotify(false);
136        int clicks = e.getWheelRotation();
137        double zf = 1.0 + this.zoomFactor;
138        if (clicks < 0) {
139            zf = 1.0 / zf;
140        }
141        if (chartPanel.isDomainZoomable()) {
142            zoomable.zoomDomainAxes(zf, pinfo, p, true);
143        }
144        if (chartPanel.isRangeZoomable()) {
145            zoomable.zoomRangeAxes(zf, pinfo, p, true);
146        }
147        plot.setNotify(notifyState);  // this generates the change event too
148    }
149
150}