1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.synchronization.graph.layer;
14 import java.util.HashMap;
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;
27 * Utilities for manipulating diagram layers in the graph.
29 * @author Tuukka Lehtonen
31 public final class GraphLayerUtil implements IGraphLayerUtil {
33 public GraphLayerUtil(Resource layer) {
37 public GraphLayer createLayer(WriteGraph graph, String layerName, boolean active) throws DatabaseException {
38 Layer0 L0 = Layer0.getInstance(graph);
39 DiagramResource DIA = DiagramResource.getInstance(graph);
41 Resource layer = graph.newResource();
42 graph.claim(layer, L0.InstanceOf, null, DIA.Layer);
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);
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);
55 graph.claim(layer, L0.HasName, name);
56 graph.claim(visibleTag, L0.HasName, name);
57 graph.claim(focusableTag, L0.HasName, name);
59 setLayerActive(graph, DIA, layer, active);
61 Map<String, Resource> properties = new HashMap<>();
62 properties.put(GraphLayer.PROP_FOCUSABLE, focusableTag);
63 properties.put(GraphLayer.PROP_VISIBLE, visibleTag);
65 return new GraphLayer(layerName, layer, properties);
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);
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));
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);
87 Map<String, Resource> properties = new HashMap<>();
88 properties.put(GraphLayer.PROP_FOCUSABLE, focusable);
89 properties.put(GraphLayer.PROP_VISIBLE, visible);
91 return new GraphLayer(name, layer, properties);