1 package org.simantics.ui.selection;
\r
3 import java.util.Collection;
\r
4 import java.util.List;
\r
6 import org.eclipse.core.commands.ExecutionEvent;
\r
7 import org.eclipse.jface.viewers.ISelection;
\r
8 import org.eclipse.ui.handlers.HandlerUtil;
\r
9 import org.simantics.Simantics;
\r
10 import org.simantics.db.ReadGraph;
\r
11 import org.simantics.db.RequestProcessor;
\r
12 import org.simantics.db.Resource;
\r
13 import org.simantics.db.common.primitiverequest.IsInstanceOf;
\r
14 import org.simantics.db.exception.DatabaseException;
\r
15 import org.simantics.db.layer0.request.VariableURI;
\r
16 import org.simantics.db.layer0.variable.Variable;
\r
17 import org.simantics.utils.ui.ISelectionUtils;
\r
19 public class WorkbenchSelectionUtils {
\r
21 public static Resource getPossibleResource(ExecutionEvent event) throws DatabaseException {
\r
22 ISelection selection = HandlerUtil.getCurrentSelection(event);
\r
23 return getPossibleResource(selection);
\r
26 public static Variable getPossibleVariable(ExecutionEvent event) throws DatabaseException {
\r
27 ISelection selection = HandlerUtil.getCurrentSelection(event);
\r
28 return getPossibleVariable(selection);
\r
31 public static List<WorkbenchSelectionElement> getWorkbenchSelectionElements(ISelection selection) {
\r
32 return ISelectionUtils.filterSelection(selection, WorkbenchSelectionElement.class);
\r
35 public static String getPossibleJSON(Object selection) throws DatabaseException {
\r
36 if(selection instanceof WorkbenchSelectionElement) return getPossibleJSON((WorkbenchSelectionElement)selection);
\r
37 WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
\r
38 if(element == null) return null;
\r
39 return getPossibleJSON(element);
\r
42 public static Resource getPossibleResource(Object selection) throws DatabaseException {
\r
43 if(selection instanceof WorkbenchSelectionElement) return getPossibleResource((WorkbenchSelectionElement)selection);
\r
44 WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
\r
45 if(element == null) return null;
\r
46 return getPossibleResource(element);
\r
49 public static Variable getPossibleVariable(Object selection) throws DatabaseException {
\r
50 if(selection instanceof WorkbenchSelectionElement) return getPossibleVariable((WorkbenchSelectionElement)selection);
\r
51 WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
\r
52 if(element == null) return null;
\r
53 return getPossibleVariable(element);
\r
56 public static Variable getPossibleVariable(Object[] selection) throws DatabaseException {
\r
57 if(selection.length != 1) return null;
\r
58 if(!(selection[0] instanceof WorkbenchSelectionElement)) return null;
\r
59 return getPossibleVariable((WorkbenchSelectionElement)selection[0]);
\r
62 public static <T> T getPossibleElement(Object[] selection, WorkbenchSelectionContentType<T> contentType) throws DatabaseException {
\r
63 if(selection.length != 1) return null;
\r
64 if(!(selection[0] instanceof WorkbenchSelectionElement)) return null;
\r
65 WorkbenchSelectionElement wse = (WorkbenchSelectionElement)selection[0];
\r
66 return wse.getContent(contentType);
\r
69 // public static <T> T getPossibleExplorerInput(Object selection) throws DatabaseException {
\r
70 // if(selection instanceof WorkbenchSelectionElement) return getPossibleExplorerInput((WorkbenchSelectionElement)selection);
\r
71 // WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
\r
72 // if(element == null) return null;
\r
73 // return getPossibleExplorerInput(element);
\r
76 // public static <T> T getPossibleExplorerColumn(Object selection) throws DatabaseException {
\r
77 // if(selection instanceof WorkbenchSelectionElement) return getPossibleExplorerColumn((WorkbenchSelectionElement)selection);
\r
78 // WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
\r
79 // if(element == null) return null;
\r
80 // return getPossibleExplorerColumn(element);
\r
83 public static String getPossibleJSON(WorkbenchSelectionElement wse) throws DatabaseException {
\r
84 return getPossibleJSON(Simantics.getSession(), wse);
\r
87 public static Resource getPossibleResource(WorkbenchSelectionElement wse) throws DatabaseException {
\r
88 return getPossibleResource(Simantics.getSession(), wse);
\r
91 public static Variable getPossibleVariable(WorkbenchSelectionElement wse) throws DatabaseException {
\r
92 return getPossibleVariable(Simantics.getSession(), wse);
\r
95 public static Variable getPossibleVariableSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
\r
96 return getPossibleVariable(graph, wse);
\r
99 public static Resource getPossibleResourceSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
\r
100 return getPossibleResource(graph, wse);
\r
103 public static String getPossibleJSON(RequestProcessor processor, Object input) throws DatabaseException {
\r
104 Variable var = getPossibleVariable(processor, input);
\r
106 String uri = processor.syncRequest(new VariableURI(var));
\r
107 return "{ \"type\":\"Variable\", \"uri\" : \"" + uri + "\" }";
\r
109 Resource res = getPossibleResource(processor, input);
\r
111 return "{ type:\"Resource\" }";
\r
113 return "{ type:\"Unknown\" }";
\r
117 public static Resource getPossibleResource(RequestProcessor processor, Object input) throws DatabaseException {
\r
118 return getPossibleResource(processor, input, null);
\r
121 public static Resource getPossibleResource(RequestProcessor processor, Object input, Resource type) throws DatabaseException {
\r
122 if(input instanceof Collection && !((Collection)input).isEmpty()) {
\r
123 Object element = ((Collection)input).iterator().next();
\r
124 if(element instanceof Resource)
\r
125 return (Resource)element;
\r
127 Resource resource = getPossibleElement(input, new AnyResource(processor));
\r
128 if(resource == null) return resource;
\r
130 if(processor.sync(new IsInstanceOf(resource, type))) return resource;
\r
136 public static Variable getPossibleVariable(RequestProcessor processor, Object input) throws DatabaseException {
\r
137 return getPossibleElement(input, new AnyVariable(processor));
\r
140 // @SuppressWarnings("unchecked")
\r
141 // public static <T> T getPossibleExplorerInput(WorkbenchSelectionElement input) throws DatabaseException {
\r
142 // return ((T)getPossibleElement(input, new ExplorerInputContentType()));
\r
145 public static <T> T getPossibleElement(Object input, WorkbenchSelectionContentType<T> contentType) {
\r
146 Object single = getPossibleSingleElement(input);
\r
147 if(single == null) return null;
\r
148 if(single instanceof WorkbenchSelectionElement) {
\r
149 WorkbenchSelectionElement element = (WorkbenchSelectionElement)single;
\r
150 return (T)element.getContent(contentType);
\r
155 public static WorkbenchSelectionElement getPossibleSelectionElement(Object input) {
\r
156 if(input instanceof WorkbenchSelectionElement) return (WorkbenchSelectionElement)input;
\r
157 if(input instanceof Collection) {
\r
158 Collection<?> c = (Collection<?>)input;
\r
159 if(c.size() == 1) {
\r
160 Object o = c.iterator().next();
\r
161 if(o instanceof WorkbenchSelectionElement) return (WorkbenchSelectionElement)o;
\r
167 // Internal helpers
\r
169 private static Object getPossibleSingleElement(Object input) {
\r
170 if(input instanceof WorkbenchSelectionElement) return input;
\r
171 if(input instanceof Collection) {
\r
172 Collection<?> c = (Collection<?>)input;
\r
173 if(c.size() == 1) return c.iterator().next();
\r