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 {
34 public GraphLayer createLayer(WriteGraph graph, String layerName, boolean active) throws DatabaseException {
35 Layer0 L0 = Layer0.getInstance(graph);
36 DiagramResource DIA = DiagramResource.getInstance(graph);
38 Resource layer = graph.newResource();
39 graph.claim(layer, L0.InstanceOf, null, DIA.Layer);
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);
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);
52 graph.claim(layer, L0.HasName, name);
53 graph.claim(visibleTag, L0.HasName, name);
54 graph.claim(focusableTag, L0.HasName, name);
56 setLayerActive(graph, DIA, layer, active);
58 Map<String, Resource> properties = new HashMap<>();
59 properties.put(GraphLayer.PROP_FOCUSABLE, focusableTag);
60 properties.put(GraphLayer.PROP_VISIBLE, visibleTag);
62 return new GraphLayer(layerName, layer, properties);
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);
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));
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);
84 Map<String, Resource> properties = new HashMap<>();
85 properties.put(GraphLayer.PROP_FOCUSABLE, focusable);
86 properties.put(GraphLayer.PROP_VISIBLE, visible);
88 return new GraphLayer(name, layer, properties);