From d11fef0101853949671492de5b49ea94892ced78 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Thu, 12 Jan 2017 13:21:00 +0200 Subject: [PATCH] Some enhancements to GraphLayer-related utilities for Diagram layers * Fixes suggested by Tuukka * Use forEachTag-functional method * getPossibleAdapter -> adapt refs #6953 Change-Id: I322ab90fbb228e4540414fd175582466d4ccb9d3 --- bundles/org.simantics.diagram/adapters.xml | 73 +------------------ .../graph/layer/GraphLayer.java | 40 +++++++--- .../graph/layer/GraphLayerManager.java | 38 +++++----- .../graph/layer/GraphLayerUtil.java | 72 +++++++++++------- .../graph/layer/IGraphLayerUtil.java | 14 ++++ 5 files changed, 109 insertions(+), 128 deletions(-) create mode 100644 bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/IGraphLayerUtil.java diff --git a/bundles/org.simantics.diagram/adapters.xml b/bundles/org.simantics.diagram/adapters.xml index 4b0463102..f4baea1c4 100644 --- a/bundles/org.simantics.diagram/adapters.xml +++ b/bundles/org.simantics.diagram/adapters.xml @@ -1,72 +1 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - icons/diagram.png icons/diagram.png A A Z 1 1 9 org.simantics.diagram.elements.TextNode org.simantics.scenegraph.g2d.nodes.SVGNode org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode - + icons/diagram.png icons/diagram.png A A Z 1 1 9 org.simantics.diagram.elements.TextNode org.simantics.scenegraph.g2d.nodes.SVGNode org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode \ No newline at end of file diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayer.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayer.java index 87746246d..33df689be 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayer.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayer.java @@ -11,30 +11,37 @@ *******************************************************************************/ package org.simantics.diagram.synchronization.graph.layer; +import java.util.Collection; +import java.util.Map; + import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.function.DbConsumer; +import org.simantics.g2d.layers.ILayer; +import org.simantics.g2d.layers.SimpleLayer; /** * @author Tuukka Lehtonen */ public class GraphLayer { + public static final String PROP_VISIBLE = "PROP_VISIBLE"; + public static final String PROP_FOCUSABLE = "PROP_FOCUSABLE"; + private final String name; private final Resource layer; + + private final Map tags; - private final Resource visible; - - private final Resource focusable; - - public GraphLayer(String name, Resource layer, Resource visible, Resource focusable) { + public GraphLayer(String name, Resource layer, Map tags) { this.name = name; this.layer = layer; - this.visible = visible; - this.focusable = focusable; + this.tags = tags; } public GraphLayer withName(String name) { - return new GraphLayer(name, layer, visible, focusable); + return new GraphLayer(name, layer, tags); } public String getName() { @@ -46,11 +53,24 @@ public class GraphLayer { } public Resource getVisible() { - return visible; + return tags.get(PROP_VISIBLE); } public Resource getFocusable() { - return focusable; + return tags.get(PROP_FOCUSABLE); + } + + public ILayer getILayer() { + return new SimpleLayer(name); + } + + public Collection getTags() { + return tags.values(); + } + + public void forEachTag(DbConsumer consumer) throws DatabaseException { + for (Resource r : tags.values()) + consumer.accept(r); } } \ No newline at end of file diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerManager.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerManager.java index 5b6e0f63a..41947ef99 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerManager.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerManager.java @@ -45,7 +45,6 @@ import org.simantics.g2d.layers.ILayer; import org.simantics.g2d.layers.ILayers; import org.simantics.g2d.layers.ILayersEditor; import org.simantics.g2d.layers.ILayersEditor.ILayersEditorListener; -import org.simantics.g2d.layers.SimpleLayer; import org.simantics.g2d.layers.SimpleLayers; import org.simantics.layer0.Layer0; @@ -175,15 +174,17 @@ public class GraphLayerManager { @Override public void perform(WriteGraph g) throws Exception { String newName = layer.getName(); - for (Resource l : g.getObjects(diagram, dia.HasLayer)) { - String name = g.getRelatedValue(l, l0.HasName); + for (Resource layer : g.getObjects(diagram, dia.HasLayer)) { + String name = g.getRelatedValue(layer, l0.HasName); if (newName.equals(name)) { return; } } - GraphLayer l = new GraphLayerUtil(g).createLayer(newName, false); - g.claim(diagram, dia.HasLayer, l.getLayer()); - layers.put(newName, l); + + IGraphLayerUtil util = g.adapt(DiagramResource.getInstance(g).Layer, IGraphLayerUtil.class); + GraphLayer layer = util.createLayer(g, newName, false); + g.claim(diagram, dia.HasLayer, layer.getLayer()); + layers.put(newName, layer); } } @@ -292,14 +293,8 @@ public class GraphLayerManager { } } - GraphLayer loadLayer(ReadGraph g, Resource layer) throws DatabaseException { - String name = g.getRelatedValue(layer, l0.HasName); - Resource visible = g.getSingleObject(layer, dia.HasVisibleTag); - Resource focusable = g.getSingleObject(layer, dia.HasFocusableTag); - return new GraphLayer(name, layer, visible, focusable); - } - public ILayersEditor loadLayers(IDiagram diagram, ReadGraph g, Resource diagramResource) throws DatabaseException { + SimpleLayers result = new SimpleLayers(); ConcurrentMap newLayers = new ConcurrentHashMap(); @@ -314,10 +309,12 @@ public class GraphLayerManager { // We need to put GraphLayer to newLayers so... for (Resource layer : g.getObjects(diagramResource, dia.HasLayer)) { - GraphLayer gl = loadLayer(g, layer); + IGraphLayerUtil layerUtil = g.adapt(g.getSingleObject(layer, Layer0.getInstance(g).InstanceOf), IGraphLayerUtil.class); + + GraphLayer gl = layerUtil.loadLayer(g, layer); for (String name : fixed) { if (name.equals(gl.getName())) { - SimpleLayer l = new SimpleLayer(gl.getName()); + ILayer l = gl.getILayer(); newLayers.put(gl.getName(), gl); result.addLayer(l); result.activate(l); @@ -331,8 +328,9 @@ public class GraphLayerManager { System.out.println("Loading layers"); for (Resource layer : g.getObjects(diagramResource, dia.HasLayer)) { - GraphLayer gl = loadLayer(g, layer); - SimpleLayer l = new SimpleLayer(gl.getName()); + IGraphLayerUtil layerUtil = g.adapt(g.getSingleObject(layer, Layer0.getInstance(g).InstanceOf), IGraphLayerUtil.class); + GraphLayer gl = layerUtil.loadLayer(g, layer); + ILayer l = gl.getILayer(); newLayers.put(gl.getName(), gl); result.addLayer(l); @@ -344,7 +342,8 @@ public class GraphLayerManager { if (DEBUG_LAYERS) System.out.println(" Loaded " + (active ? "active" : "inactive") + " layer '" + gl.getName() + "'"); - l.addLayerListener(layerListener); + if (l instanceof IEditableLayer) + ((IEditableLayer) l).addLayerListener(layerListener); if (active) result.activate(l); } @@ -517,8 +516,7 @@ public class GraphLayerManager { for (ILayer layer : visibleLayers) { GraphLayer gl = layers.get(layer.getName()); if (gl != null) { - DiagramGraphUtil.tag(g, element, gl.getVisible(), true); - DiagramGraphUtil.tag(g, element, gl.getFocusable(), true); + gl.forEachTag(tag -> DiagramGraphUtil.tag(g, element, tag, true)); } } } diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerUtil.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerUtil.java index 198452ed1..6a4ecd667 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerUtil.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerUtil.java @@ -11,6 +11,10 @@ *******************************************************************************/ package org.simantics.diagram.synchronization.graph.layer; +import java.util.HashMap; +import java.util.Map; + +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; @@ -24,51 +28,67 @@ import org.simantics.layer0.Layer0; * * @author Tuukka Lehtonen */ -public final class GraphLayerUtil { - - WriteGraph graph; - Layer0 l0; - DiagramResource dia; +public final class GraphLayerUtil implements IGraphLayerUtil { - public GraphLayerUtil(WriteGraph graph) { - this.graph = graph; - this.l0 = Layer0.getInstance(graph); - this.dia = DiagramResource.getInstance(graph); + public GraphLayerUtil(Resource layer) { } - public GraphLayer createLayer(String layerName, boolean active) throws DatabaseException { + @Override + public GraphLayer createLayer(WriteGraph graph, String layerName, boolean active) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + DiagramResource DIA = DiagramResource.getInstance(graph); + Resource layer = graph.newResource(); - graph.claim(layer, l0.InstanceOf, null, dia.Layer); + graph.claim(layer, L0.InstanceOf, null, DIA.Layer); // Assign tagging relations - Resource visibleTag = newTag(dia.IsVisible); - Resource focusableTag = newTag(dia.IsFocusable); - graph.claim(layer, dia.HasVisibleTag, visibleTag); - graph.claim(layer, dia.HasFocusableTag, focusableTag); + Resource visibleTag = newTag(graph, L0, DIA.IsVisible); + Resource focusableTag = newTag(graph, L0, DIA.IsFocusable); + graph.claim(layer, DIA.HasVisibleTag, visibleTag); + graph.claim(layer, DIA.HasFocusableTag, focusableTag); // Assign shared name property for all, the layer and the tags Resource name = graph.newResource(); - graph.claim(name, l0.InstanceOf, null, l0.String); + graph.claim(name, L0.InstanceOf, null, L0.String); graph.claimValue(name, layerName); - graph.claim(layer, l0.HasName, name); - graph.claim(visibleTag, l0.HasName, name); - graph.claim(focusableTag, l0.HasName, name); + graph.claim(layer, L0.HasName, name); + graph.claim(visibleTag, L0.HasName, name); + graph.claim(focusableTag, L0.HasName, name); - setLayerActive(layer, active); + setLayerActive(graph, DIA, layer, active); - return new GraphLayer(layerName, layer, visibleTag, focusableTag); + Map properties = new HashMap<>(); + properties.put(GraphLayer.PROP_FOCUSABLE, focusableTag); + properties.put(GraphLayer.PROP_VISIBLE, visibleTag); + + return new GraphLayer(layerName, layer, properties); } - public Resource newTag(Resource baseTag) throws DatabaseException { + public static Resource newTag(WriteGraph graph, Layer0 L0, Resource baseTag) throws DatabaseException { Resource tag = graph.newResource(); - graph.claim(tag, l0.SubrelationOf, baseTag); - graph.claim(tag, l0.InverseOf, tag); + graph.claim(tag, L0.SubrelationOf, baseTag); + graph.claim(tag, L0.InverseOf, tag); return tag; } - public void setLayerActive(Resource layer, boolean active) throws ManyObjectsForFunctionalRelationException, ServiceException { - graph.claimLiteral(layer, dia.IsActive, Boolean.valueOf(active)); + public static void setLayerActive(WriteGraph graph, DiagramResource DIA, Resource layer, boolean active) throws ManyObjectsForFunctionalRelationException, ServiceException { + graph.claimLiteral(layer, DIA.IsActive, Boolean.valueOf(active)); + } + + @Override + public GraphLayer loadLayer(ReadGraph graph, Resource layer) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + DiagramResource DIA = DiagramResource.getInstance(graph); + String name = graph.getRelatedValue(layer, L0.HasName); + Resource visible = graph.getSingleObject(layer, DIA.HasVisibleTag); + Resource focusable = graph.getSingleObject(layer, DIA.HasFocusableTag); + + Map properties = new HashMap<>(); + properties.put(GraphLayer.PROP_FOCUSABLE, focusable); + properties.put(GraphLayer.PROP_VISIBLE, visible); + + return new GraphLayer(name, layer, properties); } } diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/IGraphLayerUtil.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/IGraphLayerUtil.java new file mode 100644 index 000000000..9b0f8fb14 --- /dev/null +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/IGraphLayerUtil.java @@ -0,0 +1,14 @@ +package org.simantics.diagram.synchronization.graph.layer; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; + +public interface IGraphLayerUtil { + + GraphLayer loadLayer(ReadGraph graph, Resource layer) throws DatabaseException; + + GraphLayer createLayer(WriteGraph graph, String newName, boolean b) throws DatabaseException; + +} -- 2.47.1