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 * DispatchHandlerFX.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 javafx.scene.input.MouseEvent;
041import org.jfree.chart.entity.ChartEntity;
042import org.jfree.chart.fx.ChartCanvas;
043import org.jfree.chart.fx.ChartViewer;
044
045/**
046 * Handles mouse move and click events on the {@link ChartCanvas} by 
047 * dispatching {@link ChartMouseEventFX} events to listeners that are 
048 * registered with the {@code ChartCanvas} (listeners can also be registered
049 * with a {@link ChartViewer} control).
050 */
051public class DispatchHandlerFX extends AbstractMouseHandlerFX {
052    
053    /** Records the mouse down location. */
054    private Point2D mousePressedPoint;
055    
056    /**
057     * Creates a new instance.
058     * 
059     * @param id  the id ({@code null} not permitted).
060     */
061    public DispatchHandlerFX(String id) {
062        super(id, false, false, false, false);
063    }
064    
065    /**
066     * Handles a mouse pressed event by recording the location of the mouse
067     * pointer (so that later we can check that the click isn't part of a
068     * drag).
069     * 
070     * @param canvas  the chart canvas.
071     * @param e  the mouse event.
072     */
073    @Override
074    public void handleMousePressed(ChartCanvas canvas, MouseEvent e) {
075        this.mousePressedPoint = new Point2D.Double(e.getX(), e.getY());
076    }
077
078    @Override
079    public void handleMouseMoved(ChartCanvas canvas, MouseEvent e) {
080        double x = e.getX();
081        double y = e.getY();
082        ChartEntity entity = canvas.getRenderingInfo().getEntityCollection().getEntity(x, y);
083        ChartMouseEventFX event = new ChartMouseEventFX(canvas.getChart(), e, entity);
084        for (ChartMouseListenerFX listener : canvas.getChartMouseListeners()) {
085            listener.chartMouseMoved(event);
086        }
087     }
088
089    /**
090     * Handles a mouse clicked event by setting the anchor point for the
091     * canvas and redrawing the chart (the anchor point is a reference point
092     * used by the chart to determine crosshair lines).
093     * 
094     * @param canvas  the chart canvas ({@code null} not permitted).
095     * @param e  the mouse event ({@code null} not permitted).
096     */
097    @Override
098    public void handleMouseClicked(ChartCanvas canvas, MouseEvent e) {
099        if (this.mousePressedPoint == null) {
100            return;
101        }
102        double x = e.getX();
103        double y = e.getY();
104        ChartEntity entity = canvas.getRenderingInfo().getEntityCollection().getEntity(x, y);
105        ChartMouseEventFX event = new ChartMouseEventFX(canvas.getChart(), e, entity);
106        for (ChartMouseListenerFX listener : canvas.getChartMouseListeners()) {
107            listener.chartMouseClicked(event);
108        }
109    }
110    
111}