1 /* =========================================================== 2 * JFreeChart : a free chart library for the Java(tm) platform 3 * =========================================================== 4 * 5 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors. 6 * 7 * Project Info: http://www.jfree.org/jfreechart/index.html 8 * 9 * This library is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU Lesser General Public License as published by 11 * the Free Software Foundation; either version 2.1 of the License, or 12 * (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17 * License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22 * USA. 23 * 24 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 25 * Other names may be trademarks of their respective owners.] 26 * 27 * ---------------------- 28 * MouseWheelHandler.java 29 * ---------------------- 30 * (C) Copyright 2009-2013 by Object Refinery Limited and Contributors. 31 * 32 * Original Author: David Gilbert (for Object Refinery Limited); 33 * Contributor(s): Ulrich Voigt - patch 2686040; 34 * Jim Goodwin - bug fix; 35 * 36 * Changes 37 * ------- 38 * 18-Mar-2009 : Version 1, based on ideas by UV in patch 2686040 (DG); 39 * 26-Mar-2009 : Implemented Serializable (DG); 40 * 10-Sep-2009 : Bug fix by Jim Goodwin to respect domain/rangeZoomable flags 41 * in the ChartPanel (DG); 42 * 04-Nov-2009 : Pass mouse wheel notification to PiePlot (DG); 43 * 44 */ 45 46 package org.jfree.chart; 47 48 import java.awt.event.MouseWheelEvent; 49 import java.awt.event.MouseWheelListener; 50 import java.awt.geom.Point2D; 51 52 import java.io.Serializable; 53 import org.jfree.chart.plot.PiePlot; 54 import org.jfree.chart.plot.Plot; 55 import org.jfree.chart.plot.PlotRenderingInfo; 56 import org.jfree.chart.plot.Zoomable; 57 58 /** 59 * A class that handles mouse wheel events for the {@link ChartPanel} class. 60 * Mouse wheel event support was added in JDK 1.4, so this class will be omitted 61 * from JFreeChart if you build it using JDK 1.3. 62 * 63 * @since 1.0.13 64 */ 65 class MouseWheelHandler implements MouseWheelListener, Serializable { 66 67 /** The chart panel. */ 68 private ChartPanel chartPanel; 69 70 /** The zoom factor. */ 71 double zoomFactor; 72 73 /** 74 * Creates a new instance for the specified chart panel. 75 * 76 * @param chartPanel the chart panel (<code>null</code> not permitted). 77 */ MouseWheelHandler(ChartPanel chartPanel)78 public MouseWheelHandler(ChartPanel chartPanel) { 79 this.chartPanel = chartPanel; 80 this.zoomFactor = 0.10; 81 this.chartPanel.addMouseWheelListener(this); 82 } 83 84 /** 85 * Returns the current zoom factor. The default value is 0.10 (ten 86 * percent). 87 * 88 * @return The zoom factor. 89 * 90 * @see #setZoomFactor(double) 91 */ getZoomFactor()92 public double getZoomFactor() { 93 return this.zoomFactor; 94 } 95 96 /** 97 * Sets the zoom factor. 98 * 99 * @param zoomFactor the zoom factor. 100 * 101 * @see #getZoomFactor() 102 */ setZoomFactor(double zoomFactor)103 public void setZoomFactor(double zoomFactor) { 104 this.zoomFactor = zoomFactor; 105 } 106 107 /** 108 * Handles a mouse wheel event from the underlying chart panel. 109 * 110 * @param e the event. 111 */ 112 @Override mouseWheelMoved(MouseWheelEvent e)113 public void mouseWheelMoved(MouseWheelEvent e) { 114 JFreeChart chart = this.chartPanel.getChart(); 115 if (chart == null) { 116 return; 117 } 118 Plot plot = chart.getPlot(); 119 if (plot instanceof Zoomable) { 120 Zoomable zoomable = (Zoomable) plot; 121 handleZoomable(zoomable, e); 122 } 123 else if (plot instanceof PiePlot) { 124 PiePlot pp = (PiePlot) plot; 125 pp.handleMouseWheelRotation(e.getWheelRotation()); 126 } 127 } 128 129 /** 130 * Handle the case where a plot implements the {@link Zoomable} interface. 131 * 132 * @param zoomable the zoomable plot. 133 * @param e the mouse wheel event. 134 */ handleZoomable(Zoomable zoomable, MouseWheelEvent e)135 private void handleZoomable(Zoomable zoomable, MouseWheelEvent e) { 136 // don't zoom unless the mouse pointer is in the plot's data area 137 ChartRenderingInfo info = this.chartPanel.getChartRenderingInfo(); 138 PlotRenderingInfo pinfo = info.getPlotInfo(); 139 Point2D p = this.chartPanel.translateScreenToJava2D(e.getPoint()); 140 if (!pinfo.getDataArea().contains(p)) { 141 return; 142 } 143 144 Plot plot = (Plot) zoomable; 145 // do not notify while zooming each axis 146 boolean notifyState = plot.isNotify(); 147 plot.setNotify(false); 148 int clicks = e.getWheelRotation(); 149 double zf = 1.0 + this.zoomFactor; 150 if (clicks < 0) { 151 zf = 1.0 / zf; 152 } 153 if (chartPanel.isDomainZoomable()) { 154 zoomable.zoomDomainAxes(zf, pinfo, p, true); 155 } 156 if (chartPanel.isRangeZoomable()) { 157 zoomable.zoomRangeAxes(zf, pinfo, p, true); 158 } 159 plot.setNotify(notifyState); // this generates the change event too 160 } 161 162 } 163