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 * DispatchHandlerFX.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 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 /** 079 * Handles a mouse moved event by passing on an event to all registered 080 * listeners. 081 * 082 * @param canvas the chart canvas ({@code null} not permitted). 083 * @param e the mouse event. 084 */ 085 @Override 086 public void handleMouseMoved(ChartCanvas canvas, MouseEvent e) { 087 if (canvas.getChart() == null) { 088 return; // do nothing 089 } 090 double x = e.getX(); 091 double y = e.getY(); 092 ChartEntity entity = canvas.getRenderingInfo().getEntityCollection().getEntity(x, y); 093 ChartMouseEventFX event = new ChartMouseEventFX(canvas.getChart(), e, entity); 094 for (ChartMouseListenerFX listener : canvas.getChartMouseListeners()) { 095 listener.chartMouseMoved(event); 096 } 097 } 098 099 /** 100 * Handles a mouse clicked event by setting the anchor point for the 101 * canvas and redrawing the chart (the anchor point is a reference point 102 * used by the chart to determine crosshair lines). 103 * 104 * @param canvas the chart canvas ({@code null} not permitted). 105 * @param e the mouse event ({@code null} not permitted). 106 */ 107 @Override 108 public void handleMouseClicked(ChartCanvas canvas, MouseEvent e) { 109 if (this.mousePressedPoint == null || canvas.getChart() == null) { 110 return; 111 } 112 double x = e.getX(); 113 double y = e.getY(); 114 ChartEntity entity = canvas.getRenderingInfo().getEntityCollection().getEntity(x, y); 115 ChartMouseEventFX event = new ChartMouseEventFX(canvas.getChart(), e, entity); 116 for (ChartMouseListenerFX listener : canvas.getChartMouseListeners()) { 117 listener.chartMouseClicked(event); 118 } 119 } 120 121}