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