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