Skip to content

Commit

Permalink
Fix for bug #12 (handle ChartCanvas with null chart)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfree committed Apr 24, 2019
1 parent c80ab95 commit cb58752
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/jfree/chart/fx/ChartCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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): -;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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): -;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/jfree/chart/fx/interaction/PanHandlerFX.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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): -;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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): -;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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): -;
Expand Down Expand Up @@ -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());
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/org/jfree/chart/fx/interaction/ZoomHandlerFX.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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): -;
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit cb58752

Please sign in to comment.