1 package org.simantics.ui.selection;
3 import java.util.Collection;
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;
19 public class WorkbenchSelectionUtils {
21 public static Resource getPossibleResource(ExecutionEvent event) throws DatabaseException {
22 ISelection selection = HandlerUtil.getCurrentSelection(event);
23 return getPossibleResource(selection);
26 public static Variable getPossibleVariable(ExecutionEvent event) throws DatabaseException {
27 ISelection selection = HandlerUtil.getCurrentSelection(event);
28 return getPossibleVariable(selection);
31 public static List<WorkbenchSelectionElement> getWorkbenchSelectionElements(ISelection selection) {
32 return ISelectionUtils.filterSelection(selection, WorkbenchSelectionElement.class);
35 public static String getPossibleJSON(Object selection) throws DatabaseException {
36 WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
37 return element != null ? getPossibleJSON(element) : null;
40 public static Resource getPossibleResource(Object selection) throws DatabaseException {
41 WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
42 return element != null ? getPossibleResource(element) : null;
45 public static Variable getPossibleVariable(Object selection) throws DatabaseException {
46 WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
47 return element != null ? getPossibleVariable(element) : null;
50 public static Resource getPossibleResourceFromSelection(RequestProcessor processor, Object selection) throws DatabaseException {
51 WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
52 return element != null ? getPossibleResource(processor, element) : null;
55 public static Variable getPossibleVariableFromSelection(RequestProcessor processor, Object selection) throws DatabaseException {
56 WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
57 return element != null ? getPossibleVariable(processor, element) : null;
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]);
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);
73 public static String getPossibleJSON(WorkbenchSelectionElement wse) throws DatabaseException {
74 return getPossibleJSON(Simantics.getSession(), wse);
77 public static Resource getPossibleResource(WorkbenchSelectionElement wse) throws DatabaseException {
78 return getPossibleResource(Simantics.getSession(), wse);
81 public static Variable getPossibleVariable(WorkbenchSelectionElement wse) throws DatabaseException {
82 return getPossibleVariable(Simantics.getSession(), wse);
85 public static Variable getPossibleVariableSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
86 return getPossibleVariable(graph, wse);
89 public static Resource getPossibleResourceSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
90 return getPossibleResource(graph, wse);
93 public static String getPossibleJSON(RequestProcessor processor, Object input) throws DatabaseException {
94 Variable var = getPossibleVariable(processor, input);
96 String uri = processor.syncRequest(new VariableURI(var));
97 return "{ \"type\":\"Variable\", \"uri\" : \"" + uri + "\" }";
99 Resource res = getPossibleResource(processor, input);
101 return "{ type:\"Resource\" }";
103 return "{ type:\"Unknown\" }";
107 public static Resource getPossibleResource(RequestProcessor processor, Object input) throws DatabaseException {
108 return getPossibleResource(processor, input, null);
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;
117 Resource resource = getPossibleElement(input, new AnyResource(processor));
118 if(resource == null) return resource;
120 if(processor.sync(new IsInstanceOf(resource, type))) return resource;
126 public static Variable getPossibleVariable(RequestProcessor processor, Object input) throws DatabaseException {
127 return getPossibleElement(input, new AnyVariable(processor));
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);
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;
145 Object o = c.iterator().next();
146 if(o instanceof WorkbenchSelectionElement) return (WorkbenchSelectionElement)o;
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();
163 private static WorkbenchSelectionElement getPossibleWorkbenchSelectionElement(Object selection) {
164 return getPossibleObject(selection, WorkbenchSelectionElement.class);
167 @SuppressWarnings("unchecked")
168 private static <T> T getPossibleObject(Object selection, Class<T> clazz) {
169 return clazz.isInstance(selection)
171 : ISelectionUtils.filterSingleSelection(selection, clazz);