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 * DispatchHandlerFX.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 javafx.scene.input.MouseEvent;
045import org.jfree.chart.fx.ChartCanvas;
046import org.jfree.chart.fx.ChartViewer;
047
048/**
049 * Handles mouse move and click events on the {@link ChartCanvas} by 
050 * dispatching {@link ChartMouseEventFX} events to listeners that are 
051 * registered with the {@code ChartCanvas} (listeners can also be registered
052 * with a {@link ChartViewer} control).
053 * 
054 * <p>THE API FOR THIS CLASS IS SUBJECT TO CHANGE IN FUTURE RELEASES.  This is
055 * so that we can incorporate feedback on the (new) JavaFX support in 
056 * JFreeChart.</p>
057 * 
058 * @since 1.0.18
059 */
060public class DispatchHandlerFX extends AbstractMouseHandlerFX {
061    
062    /** Records the mouse down location. */
063    private Point2D mousePressedPoint;
064    
065    /**
066     * Creates a new instance.
067     * 
068     * @param id  the id (<code>null</code> not permitted).
069     */
070    public DispatchHandlerFX(String id) {
071        super(id, false, false, false, false);
072    }
073    
074    /**
075     * Handles a mouse pressed event by recording the location of the mouse
076     * pointer (so that later we can check that the click isn't part of a
077     * drag).
078     * 
079     * @param canvas  the chart canvas.
080     * @param e  the mouse event.
081     */
082    @Override
083    public void handleMousePressed(ChartCanvas canvas, MouseEvent e) {
084        this.mousePressedPoint = new Point2D.Double(e.getX(), e.getY());
085    }
086
087    @Override
088    public void handleMouseMoved(ChartCanvas canvas, MouseEvent e) {
089        Point2D currPt = new Point2D.Double(e.getX(), e.getY());
090        canvas.dispatchMouseMovedEvent(currPt, e);
091     }
092
093    /**
094     * Handles a mouse clicked event by setting the anchor point for the
095     * canvas and redrawing the chart (the anchor point is a reference point
096     * used by the chart to determine crosshair lines).
097     * 
098     * @param canvas  the chart canvas (<code>null</code> not permitted).
099     * @param e  the mouse event (<code>null</code> not permitted).
100     */
101    @Override
102    public void handleMouseClicked(ChartCanvas canvas, MouseEvent e) {
103        if (this.mousePressedPoint == null) {
104            return;
105        }
106        Point2D currPt = new Point2D.Double(e.getX(), e.getY());
107        if (this.mousePressedPoint.distance(currPt) < 2) {
108            canvas.dispatchMouseClickedEvent(currPt, e);
109        }
110        this.mousePressedPoint = null;
111    }
112    
113}