001/* ================================================
002 * JFreeChart-FX : JavaFX extensions for JFreeChart
003 * ================================================
004 *
005 * (C) Copyright 2017-2021, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  https://github.com/jfree/jfreechart-fx
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 * PanHandlerFX.java
029 * -----------------
030 * (C) Copyright 2014-2021 by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.fx.interaction;
038
039import java.awt.geom.Point2D;
040import java.awt.geom.Rectangle2D;
041import javafx.scene.input.MouseEvent;
042import org.jfree.chart.JFreeChart;
043import org.jfree.chart.fx.ChartCanvas;
044import org.jfree.chart.plot.Pannable;
045import org.jfree.chart.plot.Plot;
046import org.jfree.chart.plot.PlotRenderingInfo;
047
048/**
049 * Handles panning of charts on a {@link ChartCanvas}.  This handler
050 * should be configured with the required modifier keys and installed as a
051 * live handler (not an auxiliary handler).
052 */
053public class PanHandlerFX extends AbstractMouseHandlerFX {
054
055    /** The last mouse location seen during panning. */
056    private Point2D panLast;
057 
058    private double panW;
059    private double panH;
060    
061    /**
062     * Creates a new instance that requires no modifier keys.
063     * 
064     * @param id  the id ({@code null} not permitted).
065     */
066    public PanHandlerFX(String id) { 
067        this(id, false, false, false, false);
068    }
069    
070    /**
071     * Creates a new instance that will be activated using the specified 
072     * combination of modifier keys.
073     * 
074     * @param id  the id ({@code null} not permitted).
075     * @param altKey  require ALT key?
076     * @param ctrlKey  require CTRL key?
077     * @param metaKey  require META key?
078     * @param shiftKey   require SHIFT key?
079     */
080    public PanHandlerFX(String id, boolean altKey, boolean ctrlKey, 
081            boolean metaKey, boolean shiftKey) {
082        super(id, altKey, ctrlKey, metaKey, shiftKey);
083    }
084    
085    /**
086     * Handles a mouse pressed event by recording the initial mouse pointer
087     * location.
088     * 
089     * @param canvas  the JavaFX canvas ({@code null} not permitted).
090     * @param e  the mouse event ({@code null} not permitted).
091     */
092    @Override
093    public void handleMousePressed(ChartCanvas canvas, MouseEvent e) {
094        if (canvas.getChart() == null) {
095                return;
096        }
097        Plot plot = canvas.getChart().getPlot();
098        if (!(plot instanceof Pannable)) {
099            canvas.clearLiveHandler();
100            return;
101        }
102        Pannable pannable = (Pannable) plot;
103        if (pannable.isDomainPannable() || pannable.isRangePannable()) {
104            Point2D point = new Point2D.Double(e.getX(), e.getY());
105            Rectangle2D dataArea = canvas.findDataArea(point);
106            if (dataArea != null && dataArea.contains(point)) {
107                this.panW = dataArea.getWidth();
108                this.panH = dataArea.getHeight();
109                this.panLast = point;
110                canvas.setCursor(javafx.scene.Cursor.MOVE);
111            }
112        }
113        // the actual panning occurs later in the mouseDragged() method
114    }
115    
116    /**
117     * Handles a mouse dragged event by calculating the distance panned and
118     * updating the axes accordingly.
119     * 
120     * @param canvas  the JavaFX canvas ({@code null} not permitted).
121     * @param e  the mouse event ({@code null} not permitted).
122     */
123    @Override
124    public void handleMouseDragged(ChartCanvas canvas, MouseEvent e) {
125        if (this.panLast == null) {
126            //handle panning if we have a start point else unregister
127            canvas.clearLiveHandler();
128            return;
129        }
130
131        JFreeChart chart = canvas.getChart();
132        if (chart == null) {
133            return;
134        }
135        double dx = e.getX() - this.panLast.getX();
136        double dy = e.getY() - this.panLast.getY();
137        if (dx == 0.0 && dy == 0.0) {
138            return;
139        }
140        double wPercent = -dx / this.panW;
141        double hPercent = dy / this.panH;
142        boolean old = chart.getPlot().isNotify();
143        chart.getPlot().setNotify(false);
144        Pannable p = (Pannable) chart.getPlot();
145        PlotRenderingInfo info = canvas.getRenderingInfo().getPlotInfo();
146        if (p.getOrientation().isVertical()) {
147            p.panDomainAxes(wPercent, info, this.panLast);
148            p.panRangeAxes(hPercent, info, this.panLast);
149        }
150        else {
151            p.panDomainAxes(hPercent, info, this.panLast);
152            p.panRangeAxes(wPercent, info, this.panLast);
153        }
154        this.panLast = new Point2D.Double(e.getX(), e.getY());
155        chart.getPlot().setNotify(old);
156    }
157
158    @Override
159    public void handleMouseReleased(ChartCanvas canvas, MouseEvent e) {  
160        //if we have been panning reset the cursor
161        //unregister in any case
162        if (this.panLast != null) {
163            canvas.setCursor(javafx.scene.Cursor.DEFAULT);
164        }
165        this.panLast = null;
166        canvas.clearLiveHandler();
167    }
168
169}