From cb587527991f27a1c01f7b3e08ef9a8a536f74d6 Mon Sep 17 00:00:00 2001 From: David Gilbert Date: Wed, 24 Apr 2019 20:28:07 +0200 Subject: [PATCH] Fix for bug #12 (handle ChartCanvas with null chart) --- .../java/org/jfree/chart/fx/ChartCanvas.java | 6 +++--- .../chart/fx/interaction/DispatchHandlerFX.java | 16 +++++++++++++--- .../jfree/chart/fx/interaction/PanHandlerFX.java | 10 ++++++++-- .../chart/fx/interaction/ScrollHandlerFX.java | 11 +++++++++-- .../chart/fx/interaction/TooltipHandlerFX.java | 6 +++--- .../chart/fx/interaction/ZoomHandlerFX.java | 15 ++++++++++++--- 6 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/jfree/chart/fx/ChartCanvas.java b/src/main/java/org/jfree/chart/fx/ChartCanvas.java index 656f323..7d059c1 100644 --- a/src/main/java/org/jfree/chart/fx/ChartCanvas.java +++ b/src/main/java/org/jfree/chart/fx/ChartCanvas.java @@ -2,7 +2,7 @@ * JFreeChart-FX : JavaFX extensions for JFreeChart * ================================================ * - * (C) Copyright 2017, by Object Refinery Limited and Contributors. + * (C) Copyright 2017-2019, by Object Refinery Limited and Contributors. * * Project Info: https://github.com/jfree/jfreechart-fx * @@ -27,7 +27,7 @@ * ---------------- * ChartCanvas.java * ---------------- - * (C) Copyright 2014-2017, by Object Refinery Limited and Contributors. + * (C) Copyright 2014-2019, by Object Refinery Limited and Contributors. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; @@ -150,7 +150,7 @@ public class ChartCanvas extends Canvas implements ChartChangeListener, * Creates a new canvas to display the supplied chart in JavaFX. If * {@code chart} is {@code null}, a blank canvas will be displayed. * - * @param chart the chart. + * @param chart the chart ({@code null} permitted). */ public ChartCanvas(JFreeChart chart) { this.chart = chart; diff --git a/src/main/java/org/jfree/chart/fx/interaction/DispatchHandlerFX.java b/src/main/java/org/jfree/chart/fx/interaction/DispatchHandlerFX.java index c7363b0..16f6841 100644 --- a/src/main/java/org/jfree/chart/fx/interaction/DispatchHandlerFX.java +++ b/src/main/java/org/jfree/chart/fx/interaction/DispatchHandlerFX.java @@ -2,7 +2,7 @@ * JFreeChart-FX : JavaFX extensions for JFreeChart * ================================================ * - * (C) Copyright 2017, by Object Refinery Limited and Contributors. + * (C) Copyright 2017-2019, by Object Refinery Limited and Contributors. * * Project Info: https://github.com/jfree/jfreechart-fx * @@ -27,7 +27,7 @@ * ---------------------- * DispatchHandlerFX.java * ---------------------- - * (C) Copyright 2014, 2017 by Object Refinery Limited and Contributors. + * (C) Copyright 2014-2019 by Object Refinery Limited and Contributors. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; @@ -75,8 +75,18 @@ public void handleMousePressed(ChartCanvas canvas, MouseEvent e) { this.mousePressedPoint = new Point2D.Double(e.getX(), e.getY()); } + /** + * Handles a mouse moved event by passing on an event to all registered + * listeners. + * + * @param canvas the chart canvas ({@code null} not permitted). + * @param e the mouse event. + */ @Override public void handleMouseMoved(ChartCanvas canvas, MouseEvent e) { + if (canvas.getChart() == null) { + return; // do nothing + } double x = e.getX(); double y = e.getY(); ChartEntity entity = canvas.getRenderingInfo().getEntityCollection().getEntity(x, y); @@ -96,7 +106,7 @@ public void handleMouseMoved(ChartCanvas canvas, MouseEvent e) { */ @Override public void handleMouseClicked(ChartCanvas canvas, MouseEvent e) { - if (this.mousePressedPoint == null) { + if (this.mousePressedPoint == null || canvas.getChart() == null) { return; } double x = e.getX(); diff --git a/src/main/java/org/jfree/chart/fx/interaction/PanHandlerFX.java b/src/main/java/org/jfree/chart/fx/interaction/PanHandlerFX.java index 8b004bc..e3cc03e 100644 --- a/src/main/java/org/jfree/chart/fx/interaction/PanHandlerFX.java +++ b/src/main/java/org/jfree/chart/fx/interaction/PanHandlerFX.java @@ -2,7 +2,7 @@ * JFreeChart-FX : JavaFX extensions for JFreeChart * ================================================ * - * (C) Copyright 2017, by Object Refinery Limited and Contributors. + * (C) Copyright 2017-2019, by Object Refinery Limited and Contributors. * * Project Info: https://github.com/jfree/jfreechart-fx * @@ -27,7 +27,7 @@ * ----------------- * PanHandlerFX.java * ----------------- - * (C) Copyright 2014, 2017 by Object Refinery Limited and Contributors. + * (C) Copyright 2014-2019 by Object Refinery Limited and Contributors. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; @@ -91,6 +91,9 @@ public PanHandlerFX(String id, boolean altKey, boolean ctrlKey, */ @Override public void handleMousePressed(ChartCanvas canvas, MouseEvent e) { + if (canvas.getChart() == null) { + return; + } Plot plot = canvas.getChart().getPlot(); if (!(plot instanceof Pannable)) { canvas.clearLiveHandler(); @@ -126,6 +129,9 @@ public void handleMouseDragged(ChartCanvas canvas, MouseEvent e) { } JFreeChart chart = canvas.getChart(); + if (chart == null) { + return; + } double dx = e.getX() - this.panLast.getX(); double dy = e.getY() - this.panLast.getY(); if (dx == 0.0 && dy == 0.0) { diff --git a/src/main/java/org/jfree/chart/fx/interaction/ScrollHandlerFX.java b/src/main/java/org/jfree/chart/fx/interaction/ScrollHandlerFX.java index e767346..ed855b7 100644 --- a/src/main/java/org/jfree/chart/fx/interaction/ScrollHandlerFX.java +++ b/src/main/java/org/jfree/chart/fx/interaction/ScrollHandlerFX.java @@ -2,7 +2,7 @@ * JFreeChart-FX : JavaFX extensions for JFreeChart * ================================================ * - * (C) Copyright 2017, by Object Refinery Limited and Contributors. + * (C) Copyright 2017-2019, by Object Refinery Limited and Contributors. * * Project Info: https://github.com/jfree/jfreechart-fx * @@ -27,7 +27,7 @@ * -------------------- * ScrollHandlerFX.java * -------------------- - * (C) Copyright 2014-2017, by Object Refinery Limited and Contributors. + * (C) Copyright 2014-2019, by Object Refinery Limited and Contributors. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; @@ -87,6 +87,9 @@ public void setZoomFactor(double zoomFactor) { @Override public void handleScroll(ChartCanvas canvas, ScrollEvent e) { JFreeChart chart = canvas.getChart(); + if (chart == null) { + return; + } Plot plot = chart.getPlot(); if (plot instanceof Zoomable) { Zoomable zoomable = (Zoomable) plot; @@ -101,11 +104,15 @@ else if (plot instanceof PiePlot) { /** * Handle the case where a plot implements the {@link Zoomable} interface. * + * @param canvas the chart canvas. * @param zoomable the zoomable plot. * @param e the mouse wheel event. */ private void handleZoomable(ChartCanvas canvas, Zoomable zoomable, ScrollEvent e) { + if (canvas.getChart() == null) { + return; + } // don't zoom unless the mouse pointer is in the plot's data area ChartRenderingInfo info = canvas.getRenderingInfo(); PlotRenderingInfo pinfo = info.getPlotInfo(); diff --git a/src/main/java/org/jfree/chart/fx/interaction/TooltipHandlerFX.java b/src/main/java/org/jfree/chart/fx/interaction/TooltipHandlerFX.java index 74fd268..a07a625 100644 --- a/src/main/java/org/jfree/chart/fx/interaction/TooltipHandlerFX.java +++ b/src/main/java/org/jfree/chart/fx/interaction/TooltipHandlerFX.java @@ -2,7 +2,7 @@ * JFreeChart-FX : JavaFX extensions for JFreeChart * ================================================ * - * (C) Copyright 2017, by Object Refinery Limited and Contributors. + * (C) Copyright 2017-2019, by Object Refinery Limited and Contributors. * * Project Info: https://github.com/jfree/jfreechart-fx * @@ -27,7 +27,7 @@ * --------------------- * TooltipHandlerFX.java * --------------------- - * (C) Copyright 2014, by Object Refinery Limited and Contributors. + * (C) Copyright 2014-2019, by Object Refinery Limited and Contributors. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; @@ -65,7 +65,7 @@ public TooltipHandlerFX(String id) { */ @Override public void handleMouseMoved(ChartCanvas canvas, MouseEvent e) { - if (!canvas.isTooltipEnabled()) { + if (canvas.getChart() == null || !canvas.isTooltipEnabled()) { return; } String text = getTooltipText(canvas, e.getX(), e.getY()); diff --git a/src/main/java/org/jfree/chart/fx/interaction/ZoomHandlerFX.java b/src/main/java/org/jfree/chart/fx/interaction/ZoomHandlerFX.java index 51bebd5..b4df505 100644 --- a/src/main/java/org/jfree/chart/fx/interaction/ZoomHandlerFX.java +++ b/src/main/java/org/jfree/chart/fx/interaction/ZoomHandlerFX.java @@ -2,7 +2,7 @@ * JFreeChart-FX : JavaFX extensions for JFreeChart * ================================================ * - * (C) Copyright 2017, 2018, by Object Refinery Limited and Contributors. + * (C) Copyright 2017-2019, by Object Refinery Limited and Contributors. * * Project Info: https://github.com/jfree/jfreechart-fx * @@ -27,7 +27,7 @@ * ------------------ * ZoomHandlerFX.java * ------------------ - * (C) Copyright 2014-2018, by Object Refinery Limited and Contributors. + * (C) Copyright 2014-2019, by Object Refinery Limited and Contributors. * * Original Author: David Gilbert (for Object Refinery Limited); * Contributor(s): -; @@ -97,6 +97,9 @@ public ZoomHandlerFX(String id, ChartViewer parent, boolean altKey, */ @Override public void handleMousePressed(ChartCanvas canvas, MouseEvent e) { + if (canvas.getChart() == null) { + return; + } Point2D pt = new Point2D.Double(e.getX(), e.getY()); Rectangle2D dataArea = canvas.findDataArea(pt); if (dataArea != null) { @@ -123,6 +126,9 @@ public void handleMouseDragged(ChartCanvas canvas, MouseEvent e) { canvas.clearLiveHandler(); return; } + if (canvas.getChart() == null) { + return; + } boolean hZoom, vZoom; Plot p = canvas.getChart().getPlot(); @@ -166,7 +172,10 @@ else if (vZoom) { } @Override - public void handleMouseReleased(ChartCanvas canvas, MouseEvent e) { + public void handleMouseReleased(ChartCanvas canvas, MouseEvent e) { + if (canvas.getChart() == null) { + return; + } Plot p = canvas.getChart().getPlot(); if (!(p instanceof Zoomable)) { return;