001/* ================================================
002 * JFreeChart-FX : JavaFX extensions for JFreeChart
003 * ================================================
004 *
005 * (C) Copyright 2017, 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, 2017 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        Plot plot = canvas.getChart().getPlot();
095        if (!(plot instanceof Pannable)) {
096            canvas.clearLiveHandler();
097            return;
098        }
099        Pannable pannable = (Pannable) plot;
100        if (pannable.isDomainPannable() || pannable.isRangePannable()) {
101            Point2D point = new Point2D.Double(e.getX(), e.getY());
102            Rectangle2D dataArea = canvas.findDataArea(point);
103            if (dataArea != null && dataArea.contains(point)) {
104                this.panW = dataArea.getWidth();
105                this.panH = dataArea.getHeight();
106                this.panLast = point;
107                canvas.setCursor(javafx.scene.Cursor.MOVE);
108            }
109        }
110        // the actual panning occurs later in the mouseDragged() method
111    }
112    
113    /**
114     * Handles a mouse dragged event by calculating the distance panned and
115     * updating the axes accordingly.
116     * 
117     * @param canvas  the JavaFX canvas ({@code null} not permitted).
118     * @param e  the mouse event ({@code null} not permitted).
119     */
120    @Override
121    public void handleMouseDragged(ChartCanvas canvas, MouseEvent e) {
122        if (this.panLast == null) {
123            //handle panning if we have a start point else unregister
124            canvas.clearLiveHandler();
125            return;
126        }
127
128        JFreeChart chart = canvas.getChart();
129        double dx = e.getX() - this.panLast.getX();
130        double dy = e.getY() - this.panLast.getY();
131        if (dx == 0.0 && dy == 0.0) {
132            return;
133        }
134        double wPercent = -dx / this.panW;
135        double hPercent = dy / this.panH;
136        boolean old = chart.getPlot().isNotify();
137        chart.getPlot().setNotify(false);
138        Pannable p = (Pannable) chart.getPlot();
139        PlotRenderingInfo info = canvas.getRenderingInfo().getPlotInfo();
140        if (p.getOrientation().isVertical()) {
141            p.panDomainAxes(wPercent, info, this.panLast);
142            p.panRangeAxes(hPercent, info, this.panLast);
143        }
144        else {
145            p.panDomainAxes(hPercent, info, this.panLast);
146            p.panRangeAxes(wPercent, info, this.panLast);
147        }
148        this.panLast = new Point2D.Double(e.getX(), e.getY());
149        chart.getPlot().setNotify(old);
150    }
151
152    @Override
153    public void handleMouseReleased(ChartCanvas canvas, MouseEvent e) {  
154        //if we have been panning reset the cursor
155        //unregister in any case
156        if (this.panLast != null) {
157            canvas.setCursor(javafx.scene.Cursor.DEFAULT);
158        }
159        this.panLast = null;
160        canvas.clearLiveHandler();
161    }
162
163}