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