]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/selection/VariableWorkbenchSelectionElement.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / selection / VariableWorkbenchSelectionElement.java
1 package org.simantics.ui.selection;\r
2 \r
3 import org.simantics.db.ReadGraph;\r
4 import org.simantics.db.Resource;\r
5 import org.simantics.db.common.request.UnaryRead;\r
6 import org.simantics.db.common.utils.Logger;\r
7 import org.simantics.db.exception.DatabaseException;\r
8 import org.simantics.db.layer0.variable.Variable;\r
9 \r
10 public class VariableWorkbenchSelectionElement implements WorkbenchSelectionElement {\r
11         \r
12         final private Variable variable;\r
13         \r
14         public VariableWorkbenchSelectionElement(Variable variable) {\r
15                 this.variable = variable;\r
16         }\r
17         \r
18     @SuppressWarnings("unchecked")\r
19     @Override\r
20     public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {\r
21         if(contentType instanceof AnyResource) {\r
22                 AnyResource type = (AnyResource)contentType;\r
23             try {\r
24                 return (T) type.processor.sync(new UnaryRead<Variable, Resource>(variable) {\r
25                     @Override\r
26                     public Resource perform(ReadGraph graph) throws DatabaseException {\r
27                         return parameter.getRepresents(graph);\r
28                     }\r
29                 });\r
30             } catch (DatabaseException e) {\r
31                 Logger.defaultLogError(e);\r
32             }\r
33         }\r
34         else if(contentType instanceof AnyVariable) {\r
35                 return (T)variable;\r
36         } \r
37         return null;\r
38     }\r
39         \r
40         @Override\r
41         public int hashCode() {\r
42                 return variable.hashCode();\r
43         }\r
44         \r
45         @Override\r
46         public boolean equals(Object object) {\r
47         if (this == object)\r
48             return true;\r
49         else if (object == null)\r
50             return false;\r
51         else if (!(object instanceof VariableWorkbenchSelectionElement))\r
52             return false;\r
53         VariableWorkbenchSelectionElement vwse = (VariableWorkbenchSelectionElement)object;\r
54         return variable.equals(vwse.variable);\r
55         }\r
56     \r
57 \r
58 }\r