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 if(selection instanceof WorkbenchSelectionElement) return getPossibleJSON((WorkbenchSelectionElement)selection);
37 WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
38 if(element == null) return null;
39 return getPossibleJSON(element);
42 public static Resource getPossibleResource(Object selection) throws DatabaseException {
43 if(selection instanceof WorkbenchSelectionElement) return getPossibleResource((WorkbenchSelectionElement)selection);
44 WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
45 if(element == null) return null;
46 return getPossibleResource(element);
49 public static Variable getPossibleVariable(Object selection) throws DatabaseException {
50 if(selection instanceof WorkbenchSelectionElement) return getPossibleVariable((WorkbenchSelectionElement)selection);
51 WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
52 if(element == null) return null;
53 return getPossibleVariable(element);
56 public static Variable getPossibleVariable(Object[] selection) throws DatabaseException {
57 if(selection.length != 1) return null;
58 if(!(selection[0] instanceof WorkbenchSelectionElement)) return null;
59 return getPossibleVariable((WorkbenchSelectionElement)selection[0]);
62 public static <T> T getPossibleElement(Object[] selection, WorkbenchSelectionContentType<T> contentType) throws DatabaseException {
63 if(selection.length != 1) return null;
64 if(!(selection[0] instanceof WorkbenchSelectionElement)) return null;
65 WorkbenchSelectionElement wse = (WorkbenchSelectionElement)selection[0];
66 return wse.getContent(contentType);
69 // public static <T> T getPossibleExplorerInput(Object selection) throws DatabaseException {
70 // if(selection instanceof WorkbenchSelectionElement) return getPossibleExplorerInput((WorkbenchSelectionElement)selection);
71 // WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
72 // if(element == null) return null;
73 // return getPossibleExplorerInput(element);
76 // public static <T> T getPossibleExplorerColumn(Object selection) throws DatabaseException {
77 // if(selection instanceof WorkbenchSelectionElement) return getPossibleExplorerColumn((WorkbenchSelectionElement)selection);
78 // WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
79 // if(element == null) return null;
80 // return getPossibleExplorerColumn(element);
83 public static String getPossibleJSON(WorkbenchSelectionElement wse) throws DatabaseException {
84 return getPossibleJSON(Simantics.getSession(), wse);
87 public static Resource getPossibleResource(WorkbenchSelectionElement wse) throws DatabaseException {
88 return getPossibleResource(Simantics.getSession(), wse);
91 public static Variable getPossibleVariable(WorkbenchSelectionElement wse) throws DatabaseException {
92 return getPossibleVariable(Simantics.getSession(), wse);
95 public static Variable getPossibleVariableSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
96 return getPossibleVariable(graph, wse);
99 public static Resource getPossibleResourceSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
100 return getPossibleResource(graph, wse);
103 public static String getPossibleJSON(RequestProcessor processor, Object input) throws DatabaseException {
104 Variable var = getPossibleVariable(processor, input);
106 String uri = processor.syncRequest(new VariableURI(var));
107 return "{ \"type\":\"Variable\", \"uri\" : \"" + uri + "\" }";
109 Resource res = getPossibleResource(processor, input);
111 return "{ type:\"Resource\" }";
113 return "{ type:\"Unknown\" }";
117 public static Resource getPossibleResource(RequestProcessor processor, Object input) throws DatabaseException {
118 return getPossibleResource(processor, input, null);
121 public static Resource getPossibleResource(RequestProcessor processor, Object input, Resource type) throws DatabaseException {
122 if(input instanceof Collection && !((Collection)input).isEmpty()) {
123 Object element = ((Collection)input).iterator().next();
124 if(element instanceof Resource)
125 return (Resource)element;
127 Resource resource = getPossibleElement(input, new AnyResource(processor));
128 if(resource == null) return resource;
130 if(processor.sync(new IsInstanceOf(resource, type))) return resource;
136 public static Variable getPossibleVariable(RequestProcessor processor, Object input) throws DatabaseException {
137 return getPossibleElement(input, new AnyVariable(processor));
140 // @SuppressWarnings("unchecked")
141 // public static <T> T getPossibleExplorerInput(WorkbenchSelectionElement input) throws DatabaseException {
142 // return ((T)getPossibleElement(input, new ExplorerInputContentType()));
145 public static <T> T getPossibleElement(Object input, WorkbenchSelectionContentType<T> contentType) {
146 Object single = getPossibleSingleElement(input);
147 if(single == null) return null;
148 if(single instanceof WorkbenchSelectionElement) {
149 WorkbenchSelectionElement element = (WorkbenchSelectionElement)single;
150 return (T)element.getContent(contentType);
155 public static WorkbenchSelectionElement getPossibleSelectionElement(Object input) {
156 if(input instanceof WorkbenchSelectionElement) return (WorkbenchSelectionElement)input;
157 if(input instanceof Collection) {
158 Collection<?> c = (Collection<?>)input;
160 Object o = c.iterator().next();
161 if(o instanceof WorkbenchSelectionElement) return (WorkbenchSelectionElement)o;
169 private static Object getPossibleSingleElement(Object input) {
170 if(input instanceof WorkbenchSelectionElement) return input;
171 if(input instanceof Collection) {
172 Collection<?> c = (Collection<?>)input;
173 if(c.size() == 1) return c.iterator().next();