]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/variables/ComponentConnectionDescriptor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / variables / ComponentConnectionDescriptor.java
1 package org.simantics.structural2.variables;\r
2 \r
3 import org.simantics.db.ReadGraph;\r
4 import org.simantics.db.Resource;\r
5 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
6 import org.simantics.db.common.request.UnaryRead;\r
7 import org.simantics.db.exception.DatabaseException;\r
8 import org.simantics.db.layer0.variable.Variable;\r
9 \r
10 public class ComponentConnectionDescriptor extends AbstractVariableConnectionPointDescriptor {\r
11         \r
12         public Variable component;\r
13         public Resource cp;\r
14         \r
15         public ComponentConnectionDescriptor(Variable component, Resource cp) {\r
16                 this.component = component;\r
17                 this.cp = cp;\r
18         }\r
19         @Override\r
20         public int hashCode() {\r
21                 return component.hashCode() + 31*cp.hashCode();\r
22         }\r
23         @Override\r
24         public boolean equals(Object object) {\r
25         if (this == object)\r
26             return true;\r
27         else if (object == null)\r
28             return false;\r
29         else if (!(object instanceof ComponentConnectionDescriptor))\r
30             return false;\r
31         ComponentConnectionDescriptor r = (ComponentConnectionDescriptor)object;\r
32         if(!r.cp.equals(cp)) return false;\r
33         if(!r.component.equals(component)) return false;\r
34         return true;\r
35         }\r
36 \r
37         static class ComputeVariable extends UnaryRead<ComponentConnectionDescriptor, Variable> {\r
38 \r
39                 public ComputeVariable(ComponentConnectionDescriptor desc) {\r
40                         super(desc);\r
41                 }\r
42 \r
43                 @Override\r
44                 public Variable perform(ReadGraph graph) throws DatabaseException {\r
45                         return parameter.component.getProperty(graph, parameter.cp);\r
46                 }\r
47                 \r
48         }\r
49         @Override\r
50         public Variable getVariable(ReadGraph graph) throws DatabaseException {\r
51                 return graph.syncRequest(new ComputeVariable(this), TransientCacheAsyncListener.<Variable>instance());\r
52         }\r
53         public String getURI(ReadGraph graph) throws DatabaseException {\r
54                 return getVariable(graph).getURI(graph);\r
55         }\r
56         @Override\r
57         public Resource getConnectionPointResource(ReadGraph graph) throws DatabaseException {\r
58                 return getVariable(graph).getPossiblePredicateResource(graph);\r
59         }\r
60         \r
61         @Override\r
62         public boolean isFlattenedFrom(ReadGraph graph, Variable possiblyStructuralCp) throws DatabaseException {\r
63 \r
64                 Variable parent = possiblyStructuralCp.getParent(graph);\r
65                 \r
66                 String descURI = getURI(graph);\r
67                 String parentURI = parent.getURI(graph);\r
68                 if(descURI.startsWith(parentURI)) {\r
69                         // Children are in substructure\r
70                         if('/' == descURI.charAt(parentURI.length())) return true;\r
71                         // Otherwise require exact match\r
72                         return descURI.endsWith(possiblyStructuralCp.getName(graph));\r
73                 } else {\r
74                         return false;\r
75                 }\r
76                 \r
77         }\r
78         \r
79 }