]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Prevent some NPEs in Graphviz plugin 82/2482/1
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 21 Nov 2018 11:22:52 +0000 (13:22 +0200)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Wed, 21 Nov 2018 11:22:52 +0000 (13:22 +0200)
gitlab #209

Change-Id: Id29a4e20470ca63c07e3b0be41b6b12bf2e1281a

bundles/org.simantics.graphviz/META-INF/MANIFEST.MF
bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/GraphDrawable.java
bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/JViewer.java

index 3271c8eff542dd15af5a7f7324c63715dfc818ae..ea99ad54b6ab66b80a64316f3250de0b56ba0d65 100644 (file)
@@ -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,
index b1495788f2ecd9f961870eb6bde26b56cffa0a21..d5c80c70db7061f394cb427370db095f9ff1558f 100644 (file)
@@ -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;
        }
        
index 2543a7a667429d10626e7a92fe8e507fad4ef493..d50f577567977129748c62e246c1c7883ae345f9 100644 (file)
@@ -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);