1 /*******************************************************************************
\r
2 * Copyright (c) 2010, 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.properties.widgets.modules;
\r
14 import org.simantics.databoard.Bindings;
\r
15 import org.simantics.db.ReadGraph;
\r
16 import org.simantics.db.Resource;
\r
17 import org.simantics.db.WriteGraph;
\r
18 import org.simantics.db.common.request.WriteRequest;
\r
19 import org.simantics.db.exception.DatabaseException;
\r
20 import org.simantics.db.request.Read;
\r
21 import org.simantics.layer0.Layer0;
\r
22 import org.simantics.sysdyn.SysdynResource;
\r
23 import org.simantics.ui.SimanticsUI;
\r
25 public class ReferenceRow {
\r
27 Resource variable, module, dependency;
\r
30 public ReferenceRow(Resource module, Resource dependency, Resource variable) {
\r
31 this.module = module;
\r
32 this.variable = variable;
\r
33 this.dependency = dependency;
\r
37 public Resource getModule() {
\r
41 public Resource getVariable() {
\r
42 return this.variable;
\r
45 public Resource getDependency() {
\r
46 return this.dependency;
\r
49 public String getName() {
\r
52 name = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
55 public String perform(ReadGraph graph) throws DatabaseException {
\r
56 return (String)graph.getRelatedValue(getVariable(), Layer0.getInstance(graph).HasName);
\r
59 } catch (DatabaseException e) {
\r
60 e.printStackTrace();
\r
65 public String getValue() {
\r
66 if(dependency == null) return "";
\r
68 String value = null;
\r
70 value = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
73 public String perform(ReadGraph graph) throws DatabaseException {
\r
74 Layer0 l0 = Layer0.getInstance(graph);
\r
75 SysdynResource sr = SysdynResource.getInstance(graph);
\r
76 Resource valueResource = graph.getPossibleObject(dependency, sr.Variable_HasTail);
\r
77 if(!graph.isInstanceOf(valueResource, sr.Variable))
\r
78 valueResource = graph.getPossibleObject(dependency, sr.Variable_HasHead);
\r
80 if(graph.isInstanceOf(valueResource, sr.Shadow))
\r
81 valueResource = graph.getPossibleObject(valueResource, sr.Shadow_original);
\r
83 if(valueResource == null || !graph.isInstanceOf(valueResource, sr.Variable))
\r
86 return (String)graph.getRelatedValue(valueResource, l0.HasName, Bindings.STRING);
\r
89 } catch (DatabaseException e) {
\r
90 e.printStackTrace();
\r
95 public void setRefersTo(final Resource dependency) {
\r
96 if(dependency != null && dependency.equals(this.dependency)) return;
\r
97 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
99 public void perform(WriteGraph graph) throws DatabaseException {
\r
100 SysdynResource sr = SysdynResource.getInstance(graph);
\r
101 if(getDependency() != null && graph.hasStatement(getDependency(), sr.Dependency_refersTo))
\r
102 graph.deny(getDependency(), sr.Dependency_refersTo);
\r
103 if(dependency != null && graph.hasStatement(dependency, sr.Dependency_refersTo))
\r
104 graph.deny(dependency, sr.Dependency_refersTo);
\r
105 setDependency(null);
\r
106 if(dependency != null) {
\r
107 setDependency(dependency);
\r
108 graph.claim(getDependency(), SysdynResource.getInstance(graph).Dependency_refersTo, getVariable());
\r
114 private void setDependency(Resource dependency) {
\r
115 this.dependency = dependency;
\r