From d14bfb83fddf30539f401de54ce9f61bd0aff25c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Wed, 21 Nov 2018 13:22:52 +0200 Subject: [PATCH] Prevent some NPEs in Graphviz plugin gitlab #209 Change-Id: Id29a4e20470ca63c07e3b0be41b6b12bf2e1281a --- .../META-INF/MANIFEST.MF | 3 +- .../graphviz/drawable/GraphDrawable.java | 7 ++-- .../simantics/graphviz/drawable/JViewer.java | 36 ++++++++++++------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/bundles/org.simantics.graphviz/META-INF/MANIFEST.MF b/bundles/org.simantics.graphviz/META-INF/MANIFEST.MF index 3271c8eff..ea99ad54b 100644 --- a/bundles/org.simantics.graphviz/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.graphviz/META-INF/MANIFEST.MF @@ -4,7 +4,8 @@ Bundle-Name: Graphviz Bundle-SymbolicName: org.simantics.graphviz Bundle-Version: 1.1.0.qualifier Bundle-Activator: org.simantics.graphviz.Activator -Require-Bundle: org.eclipse.core.runtime +Require-Bundle: org.eclipse.core.runtime, + org.slf4j.api;bundle-version="1.7.25" Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Export-Package: org.simantics.graphviz, diff --git a/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/GraphDrawable.java b/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/GraphDrawable.java index b1495788f..d5c80c70d 100644 --- a/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/GraphDrawable.java +++ b/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/GraphDrawable.java @@ -22,6 +22,8 @@ import org.simantics.graphviz.continuation.Computation; import org.simantics.graphviz.continuation.Continuation; import org.simantics.graphviz.internal.xdot.DrawCommand; import org.simantics.graphviz.internal.xdot.DrawCommandParser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * A drawable that draws a given graph. @@ -29,7 +31,8 @@ import org.simantics.graphviz.internal.xdot.DrawCommandParser; * @author Hannu Niemist� */ public class GraphDrawable implements Drawable { - + private static final Logger LOGGER = LoggerFactory.getLogger(GraphDrawable.class); + private static String DEFAULT_ALGORITHM = "dot"; DrawCommand[] commands; @@ -113,7 +116,7 @@ public class GraphDrawable implements Drawable { @Override public synchronized Rectangle2D getBounds() { if(bounds == null) - System.err.println("bounds == null"); + LOGGER.warn("bounds == null"); return bounds; } diff --git a/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/JViewer.java b/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/JViewer.java index 2543a7a66..d50f57756 100644 --- a/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/JViewer.java +++ b/bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/JViewer.java @@ -97,6 +97,8 @@ public class JViewer extends JFrame { addMouseMotionListener(new MouseAdapter() { @Override public void mouseDragged(MouseEvent e) { + if(transform == null) + return; Point cur = e.getPoint(); if (prev == null) prev = cur; @@ -111,16 +113,16 @@ public class JViewer extends JFrame { @Override public void mouseWheelMoved(MouseWheelEvent e) { - if(transform != null) { - double scale = Math.exp(-0.1*e.getUnitsToScroll()); - Point p = e.getPoint(); - AffineTransform mod = new AffineTransform(); - mod.translate(p.getX(), p.getY()); - mod.scale(scale, scale); - mod.translate(-p.getX(), -p.getY()); - transform.preConcatenate(mod); - repaint(); - } + if(transform == null) + return; + double scale = Math.exp(-0.1*e.getUnitsToScroll()); + Point p = e.getPoint(); + AffineTransform mod = new AffineTransform(); + mod.translate(p.getX(), p.getY()); + mod.scale(scale, scale); + mod.translate(-p.getX(), -p.getY()); + transform.preConcatenate(mod); + repaint(); } }); @@ -135,8 +137,11 @@ public class JViewer extends JFrame { private void fit() { Rectangle2D bounds = drawable.getBounds(); - Rectangle2D container = getBounds(); + + if(bounds == null || container == null) + return; + double scale = container == null ? 1 : Math.min( container.getWidth() / bounds.getWidth(), container.getHeight() / bounds.getHeight()); @@ -161,8 +166,15 @@ public class JViewer extends JFrame { g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); - if(transform == null) + if(transform == null) { fit(); + if(transform == null) { + g.setTransform(new AffineTransform()); + g.setColor(Color.BLACK); + g.drawString("No data available yet", 20f, 20f); + return; + } + } g.setTransform(transform); drawable.draw(g, null); -- 2.47.1