]> gerrit.simantics Code Review - simantics/platform.git/blob
198452ed1a2fe7886e480f6facbaa584092bcbcd
[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 org.simantics.db.Resource;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
18 import org.simantics.db.exception.ServiceException;
19 import org.simantics.diagram.stubs.DiagramResource;
20 import org.simantics.layer0.Layer0;
21
22 /**
23  * Utilities for manipulating diagram layers in the graph.
24  * 
25  * @author Tuukka Lehtonen
26  */
27 public final class GraphLayerUtil {
28
29     WriteGraph      graph;
30     Layer0          l0;
31     DiagramResource dia;
32
33     public GraphLayerUtil(WriteGraph graph) {
34         this.graph = graph;
35         this.l0 = Layer0.getInstance(graph);
36         this.dia = DiagramResource.getInstance(graph);
37     }
38
39     public GraphLayer createLayer(String layerName, boolean active) throws DatabaseException {
40         Resource layer = graph.newResource();
41         graph.claim(layer, l0.InstanceOf, null, dia.Layer);
42
43         // Assign tagging relations
44         Resource visibleTag = newTag(dia.IsVisible);
45         Resource focusableTag = newTag(dia.IsFocusable);
46         graph.claim(layer, dia.HasVisibleTag, visibleTag);
47         graph.claim(layer, dia.HasFocusableTag, focusableTag);
48
49         // Assign shared name property for all, the layer and the tags
50         Resource name = graph.newResource();
51         graph.claim(name, l0.InstanceOf, null, l0.String);
52         graph.claimValue(name, layerName);
53
54         graph.claim(layer, l0.HasName, name);
55         graph.claim(visibleTag, l0.HasName, name);
56         graph.claim(focusableTag, l0.HasName, name);
57
58         setLayerActive(layer, active);
59
60         return new GraphLayer(layerName, layer, visibleTag, focusableTag);
61     }
62
63     public Resource newTag(Resource baseTag) throws DatabaseException {
64         Resource tag = graph.newResource();
65         graph.claim(tag, l0.SubrelationOf, baseTag);
66         graph.claim(tag, l0.InverseOf, tag);
67         return tag;
68     }
69
70     public void setLayerActive(Resource layer, boolean active) throws ManyObjectsForFunctionalRelationException, ServiceException {
71         graph.claimLiteral(layer, dia.IsActive, Boolean.valueOf(active));
72     }
73
74 }