]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java
ecdda45c24ba654de669a56bf052a9d8255d4e1c
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / selection / WorkbenchSelectionUtils.java
1 package org.simantics.ui.selection;
2
3 import java.util.Collection;
4 import java.util.List;
5
6 import org.eclipse.core.commands.ExecutionEvent;
7 import org.eclipse.jface.viewers.ISelection;
8 import org.eclipse.ui.handlers.HandlerUtil;
9 import org.simantics.Simantics;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.RequestProcessor;
12 import org.simantics.db.Resource;
13 import org.simantics.db.common.primitiverequest.IsInstanceOf;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.request.VariableURI;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.utils.ui.ISelectionUtils;
18
19 public class WorkbenchSelectionUtils {
20
21         public static Resource getPossibleResource(ExecutionEvent event) throws DatabaseException {
22                 ISelection selection = HandlerUtil.getCurrentSelection(event);
23                 return getPossibleResource(selection);
24         }
25         
26         public static Variable getPossibleVariable(ExecutionEvent event) throws DatabaseException {
27                 ISelection selection = HandlerUtil.getCurrentSelection(event);
28                 return getPossibleVariable(selection);
29         }
30         
31         public static List<WorkbenchSelectionElement> getWorkbenchSelectionElements(ISelection selection) {
32             return ISelectionUtils.filterSelection(selection, WorkbenchSelectionElement.class);
33         }
34
35         public static String getPossibleJSON(Object selection) throws DatabaseException {
36                 WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
37                 return element != null ? getPossibleJSON(element) : null;
38         }
39
40         public static Resource getPossibleResource(Object selection) throws DatabaseException {
41                 WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
42                 return element != null ? getPossibleResource(element) : null;
43         }
44
45         public static Variable getPossibleVariable(Object selection) throws DatabaseException {
46                 WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
47                 return element != null ? getPossibleVariable(element) : null;
48         }
49
50         public static Resource getPossibleResourceFromSelection(RequestProcessor processor, Object selection) throws DatabaseException {
51                 WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
52                 return element != null ? getPossibleResource(processor, element) : null;
53         }
54
55         public static Variable getPossibleVariableFromSelection(RequestProcessor processor, Object selection) throws DatabaseException {
56                 WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
57                 return element != null ? getPossibleVariable(processor, element) : null;
58         }
59
60         public static Variable getPossibleVariable(Object[] selection) throws DatabaseException {
61                 if(selection.length != 1) return null;
62                 if(!(selection[0] instanceof WorkbenchSelectionElement)) return null;
63                 return getPossibleVariable((WorkbenchSelectionElement)selection[0]);
64         }
65
66         public static <T> T getPossibleElement(Object[] selection, WorkbenchSelectionContentType<T> contentType) throws DatabaseException {
67                 if(selection.length != 1) return null;
68                 if(!(selection[0] instanceof WorkbenchSelectionElement)) return null;
69                 WorkbenchSelectionElement wse = (WorkbenchSelectionElement)selection[0];
70                 return wse.getContent(contentType);
71         }
72
73         public static String getPossibleJSON(WorkbenchSelectionElement wse) throws DatabaseException {
74                 return getPossibleJSON(Simantics.getSession(), wse);
75         }
76
77         public static Resource getPossibleResource(WorkbenchSelectionElement wse) throws DatabaseException {
78                 return getPossibleResource(Simantics.getSession(), wse);
79         }
80
81         public static Variable getPossibleVariable(WorkbenchSelectionElement wse) throws DatabaseException {
82                 return getPossibleVariable(Simantics.getSession(), wse);
83         }
84
85         public static Variable getPossibleVariableSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
86                 return getPossibleVariable(graph, wse);
87         }
88
89         public static Resource getPossibleResourceSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
90                 return getPossibleResource(graph, wse);
91         }
92
93         public static String getPossibleJSON(RequestProcessor processor, Object input) throws DatabaseException {
94                 Variable var = getPossibleVariable(processor, input);
95                 if(var != null) {
96                         String uri = processor.syncRequest(new VariableURI(var));
97                         return "{ \"type\":\"Variable\", \"uri\" : \"" + uri + "\" }";
98                 }
99                 Resource res = getPossibleResource(processor, input);
100                 if(res != null) {
101                         return "{ type:\"Resource\" }";
102                 }
103                 return "{ type:\"Unknown\" }";
104                 
105         }
106
107         public static Resource getPossibleResource(RequestProcessor processor, Object input) throws DatabaseException {
108                 return getPossibleResource(processor, input, null);
109         }
110
111         public static Resource getPossibleResource(RequestProcessor processor, Object input, Resource type) throws DatabaseException {
112             if(input instanceof Collection && !((Collection<?>)input).isEmpty()) {
113                 Object element = ((Collection<?>)input).iterator().next();
114                 if(element instanceof Resource)
115                     return (Resource)element;
116             }
117                 Resource resource = getPossibleElement(input, new AnyResource(processor));
118                 if(resource == null) return resource;
119                 if(type != null) {
120                         if(processor.sync(new IsInstanceOf(resource, type))) return resource;
121                         else return null;
122                 }
123                 return resource;
124         }
125
126         public static Variable getPossibleVariable(RequestProcessor processor, Object input) throws DatabaseException {
127                 return getPossibleElement(input, new AnyVariable(processor));
128         }
129
130         public static <T> T getPossibleElement(Object input, WorkbenchSelectionContentType<T> contentType) {
131                 Object single = getPossibleSingleElement(input);
132                 if(single == null) return null;
133                 if(single instanceof WorkbenchSelectionElement) {
134                         WorkbenchSelectionElement element = (WorkbenchSelectionElement)single;
135                         return (T)element.getContent(contentType);
136                 }
137                 return null;
138         }
139         
140         public static WorkbenchSelectionElement getPossibleSelectionElement(Object input) {
141         if(input instanceof WorkbenchSelectionElement) return (WorkbenchSelectionElement)input;
142         if(input instanceof Collection) {
143                 Collection<?> c = (Collection<?>)input;
144                 if(c.size() == 1) {
145                         Object o = c.iterator().next();
146                         if(o instanceof WorkbenchSelectionElement) return (WorkbenchSelectionElement)o;
147                 }
148         }
149         return null;
150         }
151
152         // Internal helpers
153         
154     private static Object getPossibleSingleElement(Object input) {
155         if(input instanceof WorkbenchSelectionElement) return input;
156         if(input instanceof Collection) {
157                 Collection<?> c = (Collection<?>)input;
158                 if(c.size() == 1) return c.iterator().next();
159         } 
160         return null;
161     }   
162
163     private static WorkbenchSelectionElement getPossibleWorkbenchSelectionElement(Object selection) {
164         return getPossibleObject(selection, WorkbenchSelectionElement.class);
165     }
166
167     @SuppressWarnings("unchecked")
168     private static <T> T getPossibleObject(Object selection, Class<T> clazz) {
169         return clazz.isInstance(selection)
170                 ? (T) selection
171                 : ISelectionUtils.filterSingleSelection(selection, clazz);
172     }
173
174 }