]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.loader/src/org/simantics/scenegraph/loader/ScenegraphVariable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph.loader / src / org / simantics / scenegraph / loader / ScenegraphVariable.java
1 package org.simantics.scenegraph.loader;\r
2 \r
3 import gnu.trove.map.hash.THashMap;\r
4 \r
5 import java.util.Collection;\r
6 import java.util.Collections;\r
7 import java.util.Map;\r
8 \r
9 import org.simantics.databoard.Bindings;\r
10 import org.simantics.databoard.binding.mutable.Variant;\r
11 import org.simantics.databoard.util.ObjectUtils;\r
12 import org.simantics.db.ReadGraph;\r
13 import org.simantics.db.Resource;\r
14 import org.simantics.db.exception.DatabaseException;\r
15 import org.simantics.db.layer0.variable.ConstantPropertyVariable;\r
16 import org.simantics.db.layer0.variable.StandardGraphChildVariable;\r
17 import org.simantics.db.layer0.variable.Variable;\r
18 import org.simantics.db.layer0.variable.Variables;\r
19 import org.simantics.scenegraph.INode;\r
20 \r
21 public class ScenegraphVariable extends StandardGraphChildVariable {\r
22 \r
23         final private SceneGraphContext context;\r
24         final private Map<String, Variant> originalProperties;\r
25         final private Map<String, Variable> properties;\r
26         \r
27         public ScenegraphVariable(Variable parent, Resource resource, final Resource runtime, final INode root) {\r
28                 this(parent, resource, runtime, root, Collections.<String, Variant>emptyMap());\r
29         }\r
30 \r
31         public static class SceneGraphContextImpl implements SceneGraphContext {\r
32 \r
33                 final private ScenegraphVariable parent;\r
34                 final private Resource runtime;\r
35                 final private INode root;\r
36                 \r
37                 public SceneGraphContextImpl(ScenegraphVariable parent, Resource runtime, INode root) {\r
38                         assert(root != null);\r
39                         this.parent = parent;\r
40                         this.runtime = runtime;\r
41                         this.root = root;\r
42                 }\r
43 \r
44                 @Override\r
45                 public INode getRoot() {\r
46                         return root;\r
47                 }\r
48                 \r
49                 @Override\r
50                 public Resource getRuntime() {\r
51                         return runtime;\r
52                 }\r
53                 \r
54                 @Override\r
55                 public Variable getRuntimeVariable() {\r
56                         return new ScenegraphVariable(parent, runtime, runtime, root, parent.originalProperties);\r
57                 }\r
58                 \r
59                 @Override\r
60                 public int hashCode() {\r
61                         \r
62                         return runtime.hashCode() ^ ObjectUtils.hashCode(root);\r
63                         \r
64                 }\r
65                 \r
66                 @Override\r
67                 public boolean equals(Object obj) {\r
68                         \r
69                         if(this == obj) return true;\r
70                         \r
71                         SceneGraphContextImpl other = (SceneGraphContextImpl)obj;\r
72                         \r
73                         if(!runtime.equals(other.runtime)) return false;\r
74                         if(!ObjectUtils.objectEquals(root, other.root)) return false;\r
75                         \r
76                         return true;\r
77                         \r
78                 }\r
79                 \r
80                 \r
81         }\r
82         \r
83         public ScenegraphVariable(Variable parent, Resource resource, final Resource runtime, final INode root, final Map<String, Variant> properties) {\r
84                 \r
85                 super(parent, null, resource);\r
86 \r
87                 if(runtime != null) {\r
88                         this.context = new SceneGraphContextImpl(this, runtime, root);\r
89                 } else {\r
90                         this.context = null;\r
91                 }\r
92                 this.originalProperties = properties;\r
93                 if(properties.isEmpty()) this.properties = Collections.<String, Variable>emptyMap();\r
94                 else {\r
95                         this.properties = new THashMap<String, Variable>();\r
96                         for(Map.Entry<String, Variant> p : properties.entrySet()) {\r
97                                 this.properties.put(p.getKey(), new ConstantPropertyVariable(parent, p.getKey(), p.getValue().getValue(), p.getValue().getBinding()));\r
98                         }\r
99                 }\r
100                 \r
101         }\r
102 \r
103         @Override\r
104         public String getName(ReadGraph graph) throws DatabaseException {\r
105                 if(parent instanceof ScenegraphVariable) {\r
106                         return super.getName(graph);\r
107                 } else {\r
108                         return "" + System.identityHashCode(this);\r
109                 }\r
110         }\r
111         \r
112         @Override\r
113         public Variable getNameVariable(ReadGraph graph) throws DatabaseException {\r
114                 if(parent instanceof ScenegraphVariable) {\r
115                         return super.getNameVariable(graph);\r
116                 } else {\r
117                         return new ConstantPropertyVariable(parent, Variables.NAME, "" + System.identityHashCode(this), Bindings.STRING);\r
118                 }\r
119         }\r
120 \r
121         protected <T> T tryAdapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
122                 if(SceneGraphContext.class == clazz) return (T)context;\r
123                 else if(INode.class == clazz) return (T)context.getRoot();\r
124                 return null;\r
125         }\r
126 \r
127         @Override\r
128         public <T> T adapt(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
129                 T t = tryAdapt(graph, clazz);\r
130                 return t != null ? t : super.adapt(graph, clazz);\r
131         }\r
132 \r
133         @Override\r
134         public <T> T adaptPossible(ReadGraph graph, Class<T> clazz) throws DatabaseException {\r
135                 T t = tryAdapt(graph, clazz);\r
136                 return t != null ? t : super.adaptPossible(graph, clazz);\r
137         }\r
138 \r
139         @Override\r
140         public boolean equals(Object obj) {\r
141                 \r
142                 // First check \r
143                 if(!super.equals(obj)) return false;\r
144                 \r
145                 ScenegraphVariable other = (ScenegraphVariable)obj;\r
146                 SceneGraphContext otherContext = other.context;\r
147 \r
148                 return ObjectUtils.objectEquals(context, otherContext);\r
149                 \r
150         }\r
151 \r
152         @Override\r
153         public int hashCode() {\r
154                 int s = super.hashCode();\r
155                 if(context != null) s ^= context.getRuntime().hashCode();\r
156                 return s;\r
157         }\r
158         \r
159         @Override\r
160         public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {\r
161                 return ScenegraphLoaderUtils.computeChildren(graph, this);\r
162         }\r
163         \r
164         @Override\r
165         public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {\r
166                 for(Variable child : getChildren(graph)) {\r
167                         if(child.getName(graph).equals(name)) return child;\r
168                 }\r
169                 return null;\r
170         }\r
171 \r
172         @Override\r
173         public void collectExtraProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {\r
174                 properties.putAll(this.properties);\r
175         }\r
176         \r
177         @Override\r
178         public Variable getPossibleExtraProperty(ReadGraph graph, String name) throws DatabaseException {\r
179                 return properties.get(name);\r
180         }\r
181         \r
182 }\r