]> gerrit.simantics Code Review - simantics/platform.git/blob
6a4ecd667bc1190f7576d64b3c340da3fe86f18b
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.synchronization.graph.layer;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
22 import org.simantics.db.exception.ServiceException;
23 import org.simantics.diagram.stubs.DiagramResource;
24 import org.simantics.layer0.Layer0;
25
26 /**
27  * Utilities for manipulating diagram layers in the graph.
28  * 
29  * @author Tuukka Lehtonen
30  */
31 public final class GraphLayerUtil implements IGraphLayerUtil {
32
33     public GraphLayerUtil(Resource layer) {
34     }
35
36     @Override
37     public GraphLayer createLayer(WriteGraph graph, String layerName, boolean active) throws DatabaseException {
38         Layer0 L0 = Layer0.getInstance(graph);
39         DiagramResource DIA = DiagramResource.getInstance(graph);
40         
41         Resource layer = graph.newResource();
42         graph.claim(layer, L0.InstanceOf, null, DIA.Layer);
43
44         // Assign tagging relations
45         Resource visibleTag = newTag(graph, L0, DIA.IsVisible);
46         Resource focusableTag = newTag(graph, L0, DIA.IsFocusable);
47         graph.claim(layer, DIA.HasVisibleTag, visibleTag);
48         graph.claim(layer, DIA.HasFocusableTag, focusableTag);
49
50         // Assign shared name property for all, the layer and the tags
51         Resource name = graph.newResource();
52         graph.claim(name, L0.InstanceOf, null, L0.String);
53         graph.claimValue(name, layerName);
54
55         graph.claim(layer, L0.HasName, name);
56         graph.claim(visibleTag, L0.HasName, name);
57         graph.claim(focusableTag, L0.HasName, name);
58
59         setLayerActive(graph, DIA, layer, active);
60
61         Map<String, Resource> properties = new HashMap<>();
62         properties.put(GraphLayer.PROP_FOCUSABLE, focusableTag);
63         properties.put(GraphLayer.PROP_VISIBLE, visibleTag);
64         
65         return new GraphLayer(layerName, layer, properties);
66     }
67
68     public static Resource newTag(WriteGraph graph, Layer0 L0, Resource baseTag) throws DatabaseException {
69         Resource tag = graph.newResource();
70         graph.claim(tag, L0.SubrelationOf, baseTag);
71         graph.claim(tag, L0.InverseOf, tag);
72         return tag;
73     }
74
75     public static void setLayerActive(WriteGraph graph, DiagramResource DIA, Resource layer, boolean active) throws ManyObjectsForFunctionalRelationException, ServiceException {
76         graph.claimLiteral(layer, DIA.IsActive, Boolean.valueOf(active));
77     }
78
79     @Override
80     public GraphLayer loadLayer(ReadGraph graph, Resource layer) throws DatabaseException {
81         Layer0 L0 = Layer0.getInstance(graph);
82         DiagramResource DIA = DiagramResource.getInstance(graph);
83         String name = graph.getRelatedValue(layer, L0.HasName);
84         Resource visible = graph.getSingleObject(layer, DIA.HasVisibleTag);
85         Resource focusable = graph.getSingleObject(layer, DIA.HasFocusableTag);
86         
87         Map<String, Resource> properties = new HashMap<>();
88         properties.put(GraphLayer.PROP_FOCUSABLE, focusable);
89         properties.put(GraphLayer.PROP_VISIBLE, visible);
90
91         return new GraphLayer(name, layer, properties);
92     }
93
94 }