]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
e6685123261d078002c52df646d93e0e75dd31af
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2012 Association for Decentralized Information Management in\r
3  * 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.sysdyn.ui.properties.widgets.modules;\r
13 \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
24 \r
25 public class ReferenceRow {\r
26 \r
27     Resource variable, module, dependency;\r
28     String name;\r
29 \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
34     }\r
35 \r
36 \r
37     public Resource getModule() {\r
38         return this.module;\r
39     }\r
40 \r
41     public Resource getVariable() {\r
42         return this.variable;\r
43     }\r
44 \r
45     public Resource getDependency() {\r
46         return this.dependency;\r
47     }\r
48 \r
49     public String getName() {\r
50         String name = null;\r
51         try {\r
52             name = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
53 \r
54                 @Override\r
55                 public String perform(ReadGraph graph) throws DatabaseException {\r
56                     return (String)graph.getRelatedValue(getVariable(), Layer0.getInstance(graph).HasName);\r
57                 }\r
58             });\r
59         } catch (DatabaseException e) {\r
60             e.printStackTrace();\r
61         }\r
62         return name;\r
63     }\r
64 \r
65     public String getValue() {\r
66         if(dependency == null) return "";\r
67 \r
68         String value = null;\r
69         try {\r
70             value = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
71 \r
72                 @Override\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
79                     \r
80                     if(graph.isInstanceOf(valueResource, sr.Shadow))\r
81                         valueResource = graph.getPossibleObject(valueResource, sr.Shadow_original);\r
82                     \r
83                     if(valueResource == null || !graph.isInstanceOf(valueResource, sr.Variable))\r
84                         return "";\r
85                     \r
86                     return (String)graph.getRelatedValue(valueResource, l0.HasName, Bindings.STRING);\r
87                 }\r
88             });\r
89         } catch (DatabaseException e) {\r
90             e.printStackTrace();\r
91         }\r
92         return value;\r
93     }\r
94 \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
98             @Override\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
109                 }\r
110             }\r
111         });\r
112     }\r
113 \r
114     private void setDependency(Resource dependency) {\r
115         this.dependency = dependency;\r
116     }\r
117 }\r
118 \r