]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/layer/GraphLayerUtil.java
Some enhancements to GraphLayer-related utilities for Diagram layers
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / graph / layer / GraphLayerUtil.java
index e3cf13527469ff7092f128d95f184dca5bd02122..6a4ecd667bc1190f7576d64b3c340da3fe86f18b 100644 (file)
@@ -1,74 +1,94 @@
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.diagram.synchronization.graph.layer;\r
-\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
-import org.simantics.db.exception.ServiceException;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.layer0.Layer0;\r
-\r
-/**\r
- * Utilities for manipulating diagram layers in the graph.\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public final class GraphLayerUtil {\r
-\r
-    WriteGraph      graph;\r
-    Layer0          l0;\r
-    DiagramResource dia;\r
-\r
-    public GraphLayerUtil(WriteGraph graph) {\r
-        this.graph = graph;\r
-        this.l0 = Layer0.getInstance(graph);\r
-        this.dia = DiagramResource.getInstance(graph);\r
-    }\r
-\r
-    public GraphLayer createLayer(String layerName, boolean active) throws DatabaseException {\r
-        Resource layer = graph.newResource();\r
-        graph.claim(layer, l0.InstanceOf, null, dia.Layer);\r
-\r
-        // Assign tagging relations\r
-        Resource visibleTag = newTag(dia.IsVisible);\r
-        Resource focusableTag = newTag(dia.IsFocusable);\r
-        graph.claim(layer, dia.HasVisibleTag, visibleTag);\r
-        graph.claim(layer, dia.HasFocusableTag, focusableTag);\r
-\r
-        // Assign shared name property for all, the layer and the tags\r
-        Resource name = graph.newResource();\r
-        graph.claim(name, l0.InstanceOf, null, l0.String);\r
-        graph.claimValue(name, layerName);\r
-\r
-        graph.claim(layer, l0.HasName, name);\r
-        graph.claim(visibleTag, l0.HasName, name);\r
-        graph.claim(focusableTag, l0.HasName, name);\r
-\r
-        setLayerActive(layer, active);\r
-\r
-        return new GraphLayer(layerName, layer, visibleTag, focusableTag);\r
-    }\r
-\r
-    public Resource newTag(Resource baseTag) throws DatabaseException {\r
-        Resource tag = graph.newResource();\r
-        graph.claim(tag, l0.SubrelationOf, baseTag);\r
-        graph.claim(tag, l0.InverseOf, tag);\r
-        return tag;\r
-    }\r
-\r
-    public void setLayerActive(Resource layer, boolean active) throws ManyObjectsForFunctionalRelationException, ServiceException {\r
-        graph.claimLiteral(layer, dia.IsActive, Boolean.valueOf(active));\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+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;
+import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
+import org.simantics.db.exception.ServiceException;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.layer0.Layer0;
+
+/**
+ * Utilities for manipulating diagram layers in the graph.
+ * 
+ * @author Tuukka Lehtonen
+ */
+public final class GraphLayerUtil implements IGraphLayerUtil {
+
+    public GraphLayerUtil(Resource layer) {
+    }
+
+    @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);
+
+        // Assign tagging relations
+        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.claimValue(name, layerName);
+
+        graph.claim(layer, L0.HasName, name);
+        graph.claim(visibleTag, L0.HasName, name);
+        graph.claim(focusableTag, L0.HasName, name);
+
+        setLayerActive(graph, DIA, layer, active);
+
+        Map<String, Resource> properties = new HashMap<>();
+        properties.put(GraphLayer.PROP_FOCUSABLE, focusableTag);
+        properties.put(GraphLayer.PROP_VISIBLE, visibleTag);
+        
+        return new GraphLayer(layerName, layer, properties);
+    }
+
+    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);
+        return tag;
+    }
+
+    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<String, Resource> properties = new HashMap<>();
+        properties.put(GraphLayer.PROP_FOCUSABLE, focusable);
+        properties.put(GraphLayer.PROP_VISIBLE, visible);
+
+        return new GraphLayer(name, layer, properties);
+    }
+
+}