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 * TooltipHandlerFX.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 javafx.scene.input.MouseEvent;
044import org.jfree.chart.ChartRenderingInfo;
045import org.jfree.chart.entity.ChartEntity;
046import org.jfree.chart.entity.EntityCollection;
047import org.jfree.chart.fx.ChartCanvas;
048
049/**
050 * Handles the updating of tooltips on a {@link ChartCanvas}.
051 * 
052 * <p>THE API FOR THIS CLASS IS SUBJECT TO CHANGE IN FUTURE RELEASES.  This is
053 * so that we can incorporate feedback on the (new) JavaFX support in 
054 * JFreeChart.</p>
055 * 
056 * @since 1.0.18
057 */
058public class TooltipHandlerFX extends AbstractMouseHandlerFX 
059        implements MouseHandlerFX {
060    
061    /**
062     * Creates a new instance with the specified ID.
063     * 
064     * @param id  the handler id (<code>null</code> not permitted).
065     */
066    public TooltipHandlerFX(String id) {
067        super(id, false, false, false, false);
068    }
069
070    /**
071     * Handles a mouse moved event by updating the tooltip.
072     * 
073     * @param canvas  the chart canvas (<code>null</code> not permitted).
074     * @param e  the mouse event.
075     */
076    @Override
077    public void handleMouseMoved(ChartCanvas canvas, MouseEvent e) {
078        if (!canvas.isTooltipEnabled()) {
079            return;
080        }
081        String text = getTooltipText(canvas, e.getX(), e.getY());
082        canvas.setTooltip(text, e.getScreenX(), e.getScreenY());
083    }
084    
085    /**
086     * Returns the tooltip text.
087     * 
088     * @param canvas  the canvas that is displaying the chart.
089     * @param x  the x-coordinate of the mouse pointer.
090     * @param y  the y-coordinate of the mouse pointer.
091     * 
092     * @return String The tooltip text (possibly <code>null</code>).
093      */
094    private String getTooltipText(ChartCanvas canvas, double x, double y) {
095        ChartRenderingInfo info = canvas.getRenderingInfo();
096        if (info == null) {
097            return null;
098        }
099        EntityCollection entities = info.getEntityCollection();
100        if (entities == null) {
101            return null;
102        }
103        ChartEntity entity = entities.getEntity(x, y);
104        if (entity == null) {
105            return null;
106        }
107        return entity.getToolTipText();
108    }
109    
110}