]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
NPE fixes for properties
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 29 Jun 2020 08:32:49 +0000 (11:32 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 29 Jun 2020 08:32:49 +0000 (11:32 +0300)
gitlab #63

bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/DependencyConnectionFactory.java
bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/DependencyEdgeClass.java

index 2846a51bc6a0f50256c4df8395e3dccad221b6e6..d65ce4aa6ed57215694e8692ea1067c85b80ba4b 100644 (file)
@@ -16,9 +16,8 @@ import java.awt.Font;
 import java.io.StringReader;\r
 import java.util.HashMap;\r
 import java.util.List;\r
+import java.util.Map;\r
 import java.util.Set;\r
-import java.util.concurrent.ConcurrentSkipListMap;\r
-import java.util.concurrent.atomic.AtomicInteger;\r
 \r
 import org.eclipse.jface.preference.PreferenceConverter;\r
 import org.eclipse.jface.resource.StringConverter;\r
@@ -31,9 +30,7 @@ import org.simantics.db.Resource;
 import org.simantics.db.Statement;\r
 import org.simantics.db.common.utils.ListUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.procedure.AsyncMultiProcedure;\r
 import org.simantics.db.procedure.AsyncProcedure;\r
-import org.simantics.db.procedure.SyncProcedure;\r
 import org.simantics.diagram.G2DUtils;\r
 import org.simantics.diagram.adapter.ElementFactoryAdapter;\r
 import org.simantics.diagram.stubs.DiagramResource;\r
@@ -79,183 +76,66 @@ public class DependencyConnectionFactory extends ElementFactoryAdapter {
     public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource elementResource,\r
             final IElement element, final AsyncProcedure<IElement> procedure) {\r
 \r
-        final AtomicInteger ready = new AtomicInteger(1);\r
-        final ConcurrentSkipListMap<String, Pair<Resource, Object>> properties = new ConcurrentSkipListMap<String, Pair<Resource, Object>>();\r
-\r
         element.setHint(DiagramHints.ROUTE_ALGORITHM, DependencyRouter.INSTANCE);\r
 \r
-        G2DResource G2D;\r
         try {\r
-            G2D = G2DResource.getInstance(graph.getSession());\r
-        } catch (DatabaseException e) {\r
-            e.printStackTrace();\r
-            return;\r
-        }\r
-\r
-        // Find possible font\r
-        graph.forPossibleStatement(elementResource, G2D.HasFont, new SyncProcedure<Statement>() {\r
-\r
-            @Override\r
-            public void execute(ReadGraph graph, Statement result) throws DatabaseException {\r
-                if(result != null && !result.isAsserted(elementResource)) {\r
-                    element.setHint(ElementHints.KEY_FONT, G2DUtils.getFont(graph, result.getObject()));\r
-                } else {\r
-                    String fontdata = graph.syncRequest(new SysdynDiagramPropertyExternalRead(new Pair<Resource, String>(elementResource, SysdynDiagramPreferences.ARROW_FONT)));\r
-                    if(fontdata != null) {\r
-                        FontData[] fdArray = PreferenceConverter.basicGetFontData(fontdata);\r
-                        if(fdArray != null) {\r
-                            if(fdArray.length == 1) {\r
-                                FontData fd = fdArray[0];\r
-                                if(fd != null) {\r
-                                    Font font = new Font(fd.getName(), fd.getStyle(), fd.getHeight());\r
-                                    element.setHint(ElementHints.KEY_FONT, font);\r
-                                }\r
-                            }\r
+            G2DResource G2D = G2DResource.getInstance(graph.getSession());\r
+\r
+            // Find possible font\r
+            Statement fontStm = graph.getPossibleStatement(elementResource, G2D.HasFont);\r
+            if(fontStm != null && !fontStm.isAsserted(elementResource)) {\r
+                element.setHint(ElementHints.KEY_FONT, G2DUtils.getFont(graph, fontStm.getObject()));\r
+            } else {\r
+                String fontdata = graph.syncRequest(new SysdynDiagramPropertyExternalRead(Pair.make(elementResource, SysdynDiagramPreferences.ARROW_FONT)));\r
+                if(fontdata != null) {\r
+                    FontData[] fdArray = PreferenceConverter.basicGetFontData(fontdata);\r
+                    if(fdArray != null && fdArray.length == 1) {\r
+                        FontData fd = fdArray[0];\r
+                        if(fd != null) {\r
+                            Font font = new Font(fd.getName(), fd.getStyle(), fd.getHeight());\r
+                            element.setHint(ElementHints.KEY_FONT, font);\r
                         }\r
                     }\r
-\r
                 }\r
             }\r
 \r
-            @Override\r
-            public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {\r
-                throwable.printStackTrace();\r
-            }\r
-        });\r
-\r
-        // Find possible color\r
-        graph.forPossibleStatement(elementResource, G2D.HasColor, new SyncProcedure<Statement>() {\r
-\r
-            @Override\r
-            public void execute(ReadGraph graph, Statement result) throws DatabaseException {\r
-                if(result != null && !result.isAsserted(elementResource)) {\r
-                    element.setHint(ElementHints.KEY_TEXT_COLOR, G2DUtils.getColor(graph, result.getObject()));\r
+            // Find possible color\r
+            Statement colorStm = graph.getPossibleStatement(elementResource, G2D.HasColor);\r
+            if(colorStm != null && !colorStm.isAsserted(elementResource)) {\r
+                element.setHint(ElementHints.KEY_TEXT_COLOR, G2DUtils.getColor(graph, colorStm.getObject()));\r
+            } else {\r
+                String color;\r
+                if (isForStockInitialOnly(graph, elementResource)) {\r
+                    color = graph.syncRequest(new SysdynDiagramPropertyExternalRead(new Pair<Resource, String>(elementResource, SysdynDiagramPreferences.ARROW_STOCK_INITIAL_COLOR)));\r
                 } else {\r
-                       String color;\r
-                       if (isForStockInitialOnly(graph, elementResource)) {\r
-                               color = graph.syncRequest(new SysdynDiagramPropertyExternalRead(new Pair<Resource, String>(elementResource, SysdynDiagramPreferences.ARROW_STOCK_INITIAL_COLOR)));\r
-                       } else {\r
-                               color = graph.syncRequest(new SysdynDiagramPropertyExternalRead(new Pair<Resource, String>(elementResource, SysdynDiagramPreferences.ARROW_COLOR)));\r
-                       }\r
-                       if(color != null) {\r
-                        RGB rgb = StringConverter.asRGB(color, null);\r
-                        if(rgb != null) {\r
-                            Color c = new Color(rgb.red, rgb.green, rgb.blue);\r
-                            element.setHint(ElementHints.KEY_TEXT_COLOR, c);\r
-                        }\r
-                    }\r
-\r
+                    color = graph.syncRequest(new SysdynDiagramPropertyExternalRead(new Pair<Resource, String>(elementResource, SysdynDiagramPreferences.ARROW_COLOR)));\r
                 }\r
-            }\r
-\r
-            @Override\r
-            public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {\r
-                throwable.printStackTrace();\r
-            }\r
-        });\r
-\r
-\r
-        // A complicated-looking procedure for obtaining all HasProperties to properties map\r
-        graph.forEachPredicate(elementResource, new AsyncMultiProcedure<Resource>() {\r
-\r
-            @Override\r
-            public void exception(AsyncReadGraph graph, Throwable throwable) {\r
-                throwable.printStackTrace();\r
-            }\r
-\r
-            @Override\r
-            public void execute(AsyncReadGraph graph, final Resource property) {\r
-\r
-                ready.incrementAndGet();\r
-                Layer0 l0;\r
-                try {\r
-                    l0 = Layer0.getInstance(graph.getSession());\r
-                } catch (DatabaseException e) {\r
-                    e.printStackTrace();\r
-                    return;\r
-                }\r
-\r
-                graph.forIsSubrelationOf(property, l0.HasProperty, new AsyncProcedure<Boolean>() {\r
-\r
-                    @Override\r
-                    public void exception(AsyncReadGraph graph, Throwable throwable) {\r
-                        throwable.printStackTrace();\r
+                if(color != null) {\r
+                    RGB rgb = StringConverter.asRGB(color, null);\r
+                    if(rgb != null) {\r
+                        Color c = new Color(rgb.red, rgb.green, rgb.blue);\r
+                        element.setHint(ElementHints.KEY_TEXT_COLOR, c);\r
                     }\r
-\r
-                    @Override\r
-                    public void execute(AsyncReadGraph graph, final Boolean isProperty) {\r
-\r
-                        if(isProperty) {\r
-\r
-                            graph.forPossibleRelatedValue(elementResource, property, new AsyncProcedure<Object>() {\r
-\r
-                                @Override\r
-                                public void exception(AsyncReadGraph graph, Throwable throwable) {\r
-                                    throwable.printStackTrace();\r
-                                }\r
-\r
-                                @Override\r
-                                public void execute(AsyncReadGraph graph, final Object value) {\r
-\r
-                                    Layer0 l0;\r
-                                    try {\r
-                                        l0 = Layer0.getInstance(graph.getSession());\r
-                                    } catch (DatabaseException e) {\r
-                                        e.printStackTrace();\r
-                                        return;\r
-                                    }\r
-\r
-                                    graph.forPossibleRelatedValue(property, l0.HasName, Bindings.STRING, new AsyncProcedure<String>() {\r
-\r
-                                        @Override\r
-                                        public void exception(AsyncReadGraph graph, Throwable throwable) {\r
-                                            throwable.printStackTrace();\r
-                                        }\r
-\r
-                                        @Override\r
-                                        public void execute(AsyncReadGraph graph, String name) {\r
-\r
-                                            properties.put(name, Pair.make(property, value));\r
-                                            if(ready.decrementAndGet() == 0) {\r
-                                                element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));\r
-                                                procedure.execute(graph, element);\r
-                                            }\r
-\r
-                                        }\r
-\r
-                                    });\r
-\r
-                                }\r
-\r
-                            });\r
-\r
-\r
-                        } else {\r
-\r
-                            if(ready.decrementAndGet() == 0) {\r
-                                element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));\r
-                                procedure.execute(graph, element);\r
-                            }\r
-\r
-                        }\r
-\r
-                    }\r
-\r
-                });\r
+                }\r
             }\r
 \r
-            @Override\r
-            public void finished(AsyncReadGraph graph) {\r
-\r
-                if(ready.decrementAndGet() == 0) {\r
-                    element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Object>(properties));\r
-                    procedure.execute(graph, element);\r
+            // Get all HasProperties to properties map\r
+            final Map<String, Pair<Resource, Object>> properties = new HashMap<>();\r
+            Layer0 l0 = graph.l0();\r
+            for (Resource property : graph.getPredicates(elementResource)) {\r
+                if (graph.isSubrelationOf(property, l0.HasProperty)) {\r
+                    Object value = graph.getPossibleRelatedValue(elementResource, property);\r
+                    String name = graph.getPossibleRelatedValue(property, l0.HasName, Bindings.STRING);\r
+                    if (value != null && name != null)\r
+                        properties.put(name, Pair.make(property, value));\r
                 }\r
-\r
             }\r
+            element.setHint(DiagramHints.PROPERTIES, properties);\r
 \r
-        });\r
-\r
+            procedure.execute(graph, element);\r
+        } catch (DatabaseException e) {\r
+            procedure.exception(graph, e);\r
+        }\r
     }\r
 \r
        protected static boolean isForStockInitialOnly(ReadGraph graph,\r
index e7f29f1cae380fa217bcc3756f204a17765189c5..50c7ef43abd47d6e2b204570d5accd5f492bef57 100644 (file)
@@ -20,7 +20,7 @@ import java.awt.geom.Path2D;
 import java.awt.geom.Rectangle2D;\r
 import java.beans.PropertyChangeEvent;\r
 import java.beans.PropertyChangeListener;\r
-import java.util.HashMap;\r
+import java.util.Collections;\r
 import java.util.Map;\r
 \r
 import org.eclipse.core.runtime.IStatus;\r
@@ -133,7 +133,9 @@ public class DependencyEdgeClass {
             Font font = ElementUtils.getTextFont(e);\r
             Color color = ElementUtils.getTextColor(e);\r
             \r
-            HashMap<String, Object> properties = e.getHint(DiagramHints.PROPERTIES);\r
+            Map<String, Object> properties = e.getHint(DiagramHints.PROPERTIES);\r
+            if (properties == null)\r
+                properties = Collections.emptyMap();\r
             Pair<?, ?> polarityPair =  (Pair<?, ?>)properties.get("polarity");\r
             Pair<?, ?> polarityLocationPair =  (Pair<?, ?>)properties.get("polarityLocation");\r
             boolean delayMark = properties.containsKey("delayMark");\r
@@ -213,6 +215,8 @@ public class DependencyEdgeClass {
             \r
             EdgeVisuals vh = e.getElementClass().getSingleItem(EdgeVisuals.class);\r
             Map<String, Pair<Resource, Object>> properties = e.getHint(DiagramHints.PROPERTIES);\r
+            if (properties == null)\r
+                properties = Collections.emptyMap();\r
             Pair<Resource, Object> strokeWidthPair = properties.get("strokeWidth");\r
             \r
             float strokeWidth;\r