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