]> gerrit.simantics Code Review - simantics/platform.git/blob
370345f4f2ac87cf1b8fcdf90315e6dac78bf3b1
[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     @Override
34     public GraphLayer createLayer(WriteGraph graph, String layerName, boolean active) throws DatabaseException {
35         Layer0 L0 = Layer0.getInstance(graph);
36         DiagramResource DIA = DiagramResource.getInstance(graph);
37         
38         Resource layer = graph.newResource();
39         graph.claim(layer, L0.InstanceOf, null, DIA.Layer);
40
41         // Assign tagging relations
42         Resource visibleTag = newTag(graph, L0, DIA.IsVisible);
43         Resource focusableTag = newTag(graph, L0, DIA.IsFocusable);
44         graph.claim(layer, DIA.HasVisibleTag, visibleTag);
45         graph.claim(layer, DIA.HasFocusableTag, focusableTag);
46
47         // Assign shared name property for all, the layer and the tags
48         Resource name = graph.newResource();
49         graph.claim(name, L0.InstanceOf, null, L0.String);
50         graph.claimValue(name, layerName);
51
52         graph.claim(layer, L0.HasName, name);
53         graph.claim(visibleTag, L0.HasName, name);
54         graph.claim(focusableTag, L0.HasName, name);
55
56         setLayerActive(graph, DIA, layer, active);
57
58         Map<String, Resource> properties = new HashMap<>();
59         properties.put(GraphLayer.PROP_FOCUSABLE, focusableTag);
60         properties.put(GraphLayer.PROP_VISIBLE, visibleTag);
61         
62         return new GraphLayer(layerName, layer, properties);
63     }
64
65     public static Resource newTag(WriteGraph graph, Layer0 L0, Resource baseTag) throws DatabaseException {
66         Resource tag = graph.newResource();
67         graph.claim(tag, L0.SubrelationOf, baseTag);
68         graph.claim(tag, L0.InverseOf, tag);
69         return tag;
70     }
71
72     public static void setLayerActive(WriteGraph graph, DiagramResource DIA, Resource layer, boolean active) throws ManyObjectsForFunctionalRelationException, ServiceException {
73         graph.claimLiteral(layer, DIA.IsActive, Boolean.valueOf(active));
74     }
75
76     @Override
77     public GraphLayer loadLayer(ReadGraph graph, Resource layer) throws DatabaseException {
78         Layer0 L0 = Layer0.getInstance(graph);
79         DiagramResource DIA = DiagramResource.getInstance(graph);
80         String name = graph.getRelatedValue(layer, L0.HasName);
81         Resource visible = graph.getSingleObject(layer, DIA.HasVisibleTag);
82         Resource focusable = graph.getSingleObject(layer, DIA.HasFocusableTag);
83         
84         Map<String, Resource> properties = new HashMap<>();
85         properties.put(GraphLayer.PROP_FOCUSABLE, focusable);
86         properties.put(GraphLayer.PROP_VISIBLE, visible);
87
88         return new GraphLayer(name, layer, properties);
89     }
90
91 }