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