]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java
Provide classifications and datatype for Variable-based drag sources
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / selection / WorkbenchSelectionUtils.java
index e30b0e08b6b20240fe8aa149255d8ea290daf5ea..e3e6eeafe9270432ab1a1a1dd984d8f8ea3d2306 100644 (file)
-package org.simantics.ui.selection;\r
-\r
-import java.util.Collection;\r
-import java.util.List;\r
-\r
-import org.eclipse.core.commands.ExecutionEvent;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.ui.handlers.HandlerUtil;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.RequestProcessor;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.primitiverequest.IsInstanceOf;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.request.VariableURI;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-\r
-public class WorkbenchSelectionUtils {\r
-\r
-       public static Resource getPossibleResource(ExecutionEvent event) throws DatabaseException {\r
-               ISelection selection = HandlerUtil.getCurrentSelection(event);\r
-               return getPossibleResource(selection);\r
-       }\r
-       \r
-       public static Variable getPossibleVariable(ExecutionEvent event) throws DatabaseException {\r
-               ISelection selection = HandlerUtil.getCurrentSelection(event);\r
-               return getPossibleVariable(selection);\r
-       }\r
-       \r
-       public static List<WorkbenchSelectionElement> getWorkbenchSelectionElements(ISelection selection) {\r
-           return ISelectionUtils.filterSelection(selection, WorkbenchSelectionElement.class);\r
-       }\r
-\r
-       public static String getPossibleJSON(Object selection) throws DatabaseException {\r
-               if(selection instanceof WorkbenchSelectionElement) return getPossibleJSON((WorkbenchSelectionElement)selection);\r
-               WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);\r
-               if(element == null) return null;\r
-               return getPossibleJSON(element);\r
-       }\r
-\r
-       public static Resource getPossibleResource(Object selection) throws DatabaseException {\r
-               if(selection instanceof WorkbenchSelectionElement) return getPossibleResource((WorkbenchSelectionElement)selection);\r
-               WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);\r
-               if(element == null) return null;\r
-               return getPossibleResource(element);\r
-       }\r
-\r
-       public static Variable getPossibleVariable(Object selection) throws DatabaseException {\r
-               if(selection instanceof WorkbenchSelectionElement) return getPossibleVariable((WorkbenchSelectionElement)selection);\r
-               WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);\r
-               if(element == null) return null;\r
-               return getPossibleVariable(element);\r
-       }\r
-\r
-       public static Variable getPossibleVariable(Object[] selection) throws DatabaseException {\r
-               if(selection.length != 1) return null;\r
-               if(!(selection[0] instanceof WorkbenchSelectionElement)) return null;\r
-               return getPossibleVariable((WorkbenchSelectionElement)selection[0]);\r
-       }\r
-\r
-       public static <T> T getPossibleElement(Object[] selection, WorkbenchSelectionContentType<T> contentType) throws DatabaseException {\r
-               if(selection.length != 1) return null;\r
-               if(!(selection[0] instanceof WorkbenchSelectionElement)) return null;\r
-               WorkbenchSelectionElement wse = (WorkbenchSelectionElement)selection[0];\r
-               return wse.getContent(contentType);\r
-       }\r
-\r
-//     public static <T> T getPossibleExplorerInput(Object selection) throws DatabaseException {\r
-//             if(selection instanceof WorkbenchSelectionElement) return getPossibleExplorerInput((WorkbenchSelectionElement)selection);\r
-//             WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);\r
-//             if(element == null) return null;\r
-//             return getPossibleExplorerInput(element);\r
-//     }\r
-\r
-//     public static <T> T getPossibleExplorerColumn(Object selection) throws DatabaseException {\r
-//             if(selection instanceof WorkbenchSelectionElement) return getPossibleExplorerColumn((WorkbenchSelectionElement)selection);\r
-//             WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);\r
-//             if(element == null) return null;\r
-//             return getPossibleExplorerColumn(element);\r
-//     }\r
-       \r
-       public static String getPossibleJSON(WorkbenchSelectionElement wse) throws DatabaseException {\r
-               return getPossibleJSON(Simantics.getSession(), wse);\r
-       }\r
-\r
-       public static Resource getPossibleResource(WorkbenchSelectionElement wse) throws DatabaseException {\r
-               return getPossibleResource(Simantics.getSession(), wse);\r
-       }\r
-\r
-       public static Variable getPossibleVariable(WorkbenchSelectionElement wse) throws DatabaseException {\r
-               return getPossibleVariable(Simantics.getSession(), wse);\r
-       }\r
-\r
-       public static Variable getPossibleVariableSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {\r
-               return getPossibleVariable(graph, wse);\r
-       }\r
-\r
-       public static Resource getPossibleResourceSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {\r
-               return getPossibleResource(graph, wse);\r
-       }\r
-\r
-       public static String getPossibleJSON(RequestProcessor processor, Object input) throws DatabaseException {\r
-               Variable var = getPossibleVariable(processor, input);\r
-               if(var != null) {\r
-                       String uri = processor.syncRequest(new VariableURI(var));\r
-                       return "{ \"type\":\"Variable\", \"uri\" : \"" + uri + "\" }";\r
-               }\r
-               Resource res = getPossibleResource(processor, input);\r
-               if(res != null) {\r
-                       return "{ type:\"Resource\" }";\r
-               }\r
-               return "{ type:\"Unknown\" }";\r
-               \r
-       }\r
-\r
-       public static Resource getPossibleResource(RequestProcessor processor, Object input) throws DatabaseException {\r
-               return getPossibleResource(processor, input, null);\r
-       }\r
-\r
-       public static Resource getPossibleResource(RequestProcessor processor, Object input, Resource type) throws DatabaseException {\r
-           if(input instanceof Collection && !((Collection)input).isEmpty()) {\r
-               Object element = ((Collection)input).iterator().next();\r
-               if(element instanceof Resource)\r
-                   return (Resource)element;\r
-           }\r
-               Resource resource = getPossibleElement(input, new AnyResource(processor));\r
-               if(resource == null) return resource;\r
-               if(type != null) {\r
-                       if(processor.sync(new IsInstanceOf(resource, type))) return resource;\r
-                       else return null;\r
-               }\r
-               return resource;\r
-       }\r
-\r
-       public static Variable getPossibleVariable(RequestProcessor processor, Object input) throws DatabaseException {\r
-               return getPossibleElement(input, new AnyVariable(processor));\r
-       }\r
-\r
-//     @SuppressWarnings("unchecked")\r
-//     public static <T> T getPossibleExplorerInput(WorkbenchSelectionElement input) throws DatabaseException {\r
-//             return ((T)getPossibleElement(input, new ExplorerInputContentType()));\r
-//     }\r
-\r
-       public static <T> T getPossibleElement(Object input, WorkbenchSelectionContentType<T> contentType) {\r
-               Object single = getPossibleSingleElement(input);\r
-               if(single == null) return null;\r
-               if(single instanceof WorkbenchSelectionElement) {\r
-                       WorkbenchSelectionElement element = (WorkbenchSelectionElement)single;\r
-                       return (T)element.getContent(contentType);\r
-               }\r
-               return null;\r
-       }\r
-       \r
-       public static WorkbenchSelectionElement getPossibleSelectionElement(Object input) {\r
-       if(input instanceof WorkbenchSelectionElement) return (WorkbenchSelectionElement)input;\r
-       if(input instanceof Collection) {\r
-               Collection<?> c = (Collection<?>)input;\r
-               if(c.size() == 1) {\r
-                       Object o = c.iterator().next();\r
-                       if(o instanceof WorkbenchSelectionElement) return (WorkbenchSelectionElement)o;\r
-               }\r
-       }\r
-       return null;\r
-       }\r
-\r
-       // Internal helpers\r
-       \r
-    private static Object getPossibleSingleElement(Object input) {\r
-       if(input instanceof WorkbenchSelectionElement) return input;\r
-       if(input instanceof Collection) {\r
-               Collection<?> c = (Collection<?>)input;\r
-               if(c.size() == 1) return c.iterator().next();\r
-       } \r
-       return null;\r
-    }  \r
-    \r
-       \r
-}\r
+package org.simantics.ui.selection;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.simantics.Simantics;
+import org.simantics.databoard.type.Datatype;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.common.primitiverequest.IsInstanceOf;
+import org.simantics.db.common.primitiverequest.Supertypes;
+import org.simantics.db.common.primitiverequest.Types;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.PossibleGUID;
+import org.simantics.db.layer0.request.PossibleURI;
+import org.simantics.db.layer0.request.PossibleVariableGUID;
+import org.simantics.db.layer0.request.PossibleVariableRepresents;
+import org.simantics.db.layer0.request.VariableRead;
+import org.simantics.db.layer0.request.VariableURI;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.utils.ui.ISelectionUtils;
+
+public class WorkbenchSelectionUtils {
+
+       public static Resource getPossibleResource(ExecutionEvent event) throws DatabaseException {
+               ISelection selection = HandlerUtil.getCurrentSelection(event);
+               return getPossibleResource(selection);
+       }
+       
+       public static Variable getPossibleVariable(ExecutionEvent event) throws DatabaseException {
+               ISelection selection = HandlerUtil.getCurrentSelection(event);
+               return getPossibleVariable(selection);
+       }
+       
+       public static List<WorkbenchSelectionElement> getWorkbenchSelectionElements(ISelection selection) {
+           return ISelectionUtils.filterSelection(selection, WorkbenchSelectionElement.class);
+       }
+
+       public static String getPossibleJSON(Object selection) throws DatabaseException {
+               WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
+               return element != null ? getPossibleJSON(element) : null;
+       }
+
+       public static Resource getPossibleResource(Object selection) throws DatabaseException {
+               WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
+               return element != null ? getPossibleResource(element) : null;
+       }
+
+       public static Variable getPossibleVariable(Object selection) throws DatabaseException {
+               WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
+               return element != null ? getPossibleVariable(element) : null;
+       }
+
+       public static Resource getPossibleResourceFromSelection(RequestProcessor processor, Object selection) throws DatabaseException {
+               WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
+               return element != null ? getPossibleResource(processor, element) : null;
+       }
+
+       public static Variable getPossibleVariableFromSelection(RequestProcessor processor, Object selection) throws DatabaseException {
+               WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
+               return element != null ? getPossibleVariable(processor, element) : null;
+       }
+
+       public static Variable getPossibleVariable(Object[] selection) throws DatabaseException {
+               if(selection.length != 1) return null;
+               if(!(selection[0] instanceof WorkbenchSelectionElement)) return null;
+               return getPossibleVariable((WorkbenchSelectionElement)selection[0]);
+       }
+
+       public static <T> T getPossibleElement(Object[] selection, WorkbenchSelectionContentType<T> contentType) throws DatabaseException {
+               if(selection.length != 1) return null;
+               if(!(selection[0] instanceof WorkbenchSelectionElement)) return null;
+               WorkbenchSelectionElement wse = (WorkbenchSelectionElement)selection[0];
+               return wse.getContent(contentType);
+       }
+
+       public static String getPossibleJSON(WorkbenchSelectionElement wse) throws DatabaseException {
+               return getPossibleJSON(Simantics.getSession(), wse);
+       }
+
+       public static Resource getPossibleResource(WorkbenchSelectionElement wse) throws DatabaseException {
+               return getPossibleResource(Simantics.getSession(), wse);
+       }
+
+       public static Variable getPossibleVariable(WorkbenchSelectionElement wse) throws DatabaseException {
+               return getPossibleVariable(Simantics.getSession(), wse);
+       }
+
+       public static Variable getPossibleVariableSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
+               return getPossibleVariable(graph, wse);
+       }
+
+       public static Resource getPossibleResourceSCL(ReadGraph graph, WorkbenchSelectionElement wse) throws DatabaseException {
+               return getPossibleResource(graph, wse);
+       }
+
+       public static String getPossibleJSON(RequestProcessor processor, Object input) throws DatabaseException {
+               Variable var = getPossibleVariable(processor, input);
+               Resource res = getPossibleResource(processor, input);
+               String typesStr = getTypeResourceString(processor, res, var);
+               if(var != null) {
+                       String uri = processor.syncRequest(new VariableURI(var));
+                       String guid = processor.syncRequest(new PossibleVariableGUID(var));
+                       
+                       Set<String> classifications = processor.syncRequest(new VariableRead<Set<String>>(var) {
+                               @Override
+                               public Set<String> perform(ReadGraph graph) throws DatabaseException {
+                                       return var.getClassifications(graph);
+                               }
+                       });
+                       String classificationsStr = toJSONStringArray(new ArrayList<>(classifications));
+                       
+                       Datatype datatype = processor.syncRequest(new VariableRead<Datatype>(var) {
+                               @Override
+                               public Datatype perform(ReadGraph graph) throws DatabaseException {
+                                       return var.getPossibleDatatype(graph);
+                               }
+                       });
+                       
+                       return toJSONObjectString(
+                                       "type", "\"Variable\"",
+                                       "uri", safeQuotedString(uri),
+                                       "guid", safeQuotedString(guid),
+                                       "resourceId", res == null ? "" : Long.toString(res.getResourceId()),
+                                       "typeResources", typesStr,
+                                       "classifications", classificationsStr,
+                                       "datatype", safeQuotedString(datatype != null ? datatype.getClass().getName() : null));
+               }
+               if(res != null) {
+                   String uri = processor.syncRequest(new PossibleURI(res));
+                   String guid = processor.syncRequest(new PossibleGUID(res));
+                       return toJSONObjectString(
+                                       "type", "\"Resource\"",
+                                       "uri", safeQuotedString(uri),
+                                       "guid", safeQuotedString(guid),
+                                       "resourceId", Long.toString(res.getResourceId()),
+                                       "typeResources", typesStr);
+               }
+               return "{ \"type\": \"Unknown\" }";
+       }
+
+       public static Resource getPossibleResource(RequestProcessor processor, Object input) throws DatabaseException {
+               return getPossibleResource(processor, input, null);
+       }
+
+       public static Resource getPossibleResource(RequestProcessor processor, Object input, Resource type) throws DatabaseException {
+           if(input instanceof Collection && !((Collection<?>)input).isEmpty()) {
+               Object element = ((Collection<?>)input).iterator().next();
+               if(element instanceof Resource)
+                   return (Resource)element;
+           }
+               Resource resource = getPossibleElement(input, new AnyResource(processor));
+               if(resource == null) return resource;
+               if(type != null) {
+                       if(processor.sync(new IsInstanceOf(resource, type))) return resource;
+                       else return null;
+               }
+               return resource;
+       }
+
+       public static Variable getPossibleVariable(RequestProcessor processor, Object input) throws DatabaseException {
+               return getPossibleElement(input, new AnyVariable(processor));
+       }
+
+       public static <T> T getPossibleElement(Object input, WorkbenchSelectionContentType<T> contentType) {
+               Object single = getPossibleSingleElement(input);
+               if(single == null) return null;
+               if(single instanceof WorkbenchSelectionElement) {
+                       WorkbenchSelectionElement element = (WorkbenchSelectionElement)single;
+                       return (T)element.getContent(contentType);
+               }
+               return null;
+       }
+       
+       public static WorkbenchSelectionElement getPossibleSelectionElement(Object input) {
+       if(input instanceof WorkbenchSelectionElement) return (WorkbenchSelectionElement)input;
+       if(input instanceof Collection) {
+               Collection<?> c = (Collection<?>)input;
+               if(c.size() == 1) {
+                       Object o = c.iterator().next();
+                       if(o instanceof WorkbenchSelectionElement) return (WorkbenchSelectionElement)o;
+               }
+       }
+       return null;
+       }
+
+       // Internal helpers
+       
+    private static Object getPossibleSingleElement(Object input) {
+       if(input instanceof WorkbenchSelectionElement) return input;
+       if(input instanceof Collection) {
+               Collection<?> c = (Collection<?>)input;
+               if(c.size() == 1) return c.iterator().next();
+       } 
+       return null;
+    }  
+
+    private static WorkbenchSelectionElement getPossibleWorkbenchSelectionElement(Object selection) {
+        return getPossibleObject(selection, WorkbenchSelectionElement.class);
+    }
+
+    @SuppressWarnings("unchecked")
+    private static <T> T getPossibleObject(Object selection, Class<T> clazz) {
+        return clazz.isInstance(selection)
+                ? (T) selection
+                : ISelectionUtils.filterSingleSelection(selection, clazz);
+    }
+
+    private static class PossibleVariableType extends VariableRead<Resource> {
+
+        public PossibleVariableType(Variable var) {
+            super(var);
+        }
+
+        @Override
+        public Resource perform(ReadGraph graph) throws DatabaseException {
+            return variable.getPossibleType(graph);
+        }
+
+    }
+
+       private static String toJSONObjectString(String... keyValuePairs) {
+               int len = keyValuePairs.length;
+               assert (len & 1) == 0;
+               StringBuilder sb = new StringBuilder(128);
+               sb.append("{ ");
+               int entryCount = 0;
+               for (int i = 0; i < len; i += 2) {
+                       String value = keyValuePairs[i+1];
+                       if (value != null && !value.isEmpty()) {
+                               if (entryCount > 0)
+                                       sb.append(", ");
+                               sb.append("\"").append(keyValuePairs[i]).append("\": ").append(value);
+                               ++entryCount;
+                       }
+               }
+               sb.append(" }");
+               return sb.toString();
+       }
+
+       private static String escapeQuotes(String s) {
+               return s.indexOf('"') >= 0 ? s.replaceAll("\"", "\\\\\"") : s;
+       }
+
+       private static String safeQuotedString(String s) {
+               return s != null && !s.isEmpty() ? "\"" + escapeQuotes(s) + "\"" : "";
+       }
+
+       private static String toJSONStringArray(List<String> strings) throws DatabaseException {
+               // Sort the type strings to produce stable results
+               if (strings.isEmpty())
+                       return "";
+               return strings.stream()
+                               .map(WorkbenchSelectionUtils::escapeQuotes)
+                               .sorted()
+                               .collect(Collectors.joining("\", \"", "[\"", "\"]"));
+       }
+
+       private static String getTypeResourceString(RequestProcessor processor, Resource r, Variable v) throws DatabaseException {
+               return toJSONStringArray(
+                               toPossibleURIs(processor,
+                                               getTypes(processor, r, v)));
+       }
+
+       private static Set<Resource> getTypes(RequestProcessor processor, Resource r, Variable v) throws DatabaseException {
+               if (r == null && v != null)
+                       r = processor.syncRequest(new PossibleVariableRepresents(v));
+               if (r != null)
+                       return processor.syncRequest(new Types(r));
+               r = v != null ? processor.syncRequest(new PossibleVariableType(v)) : null;
+               return r != null ? processor.syncRequest(new Supertypes(r)) : Collections.emptySet();
+       }
+
+       private static List<String> toPossibleURIs(RequestProcessor processor, Collection<Resource> resources) throws DatabaseException {
+               if (resources.isEmpty())
+                       return Collections.emptyList();
+               List<String> result = new ArrayList<>(resources.size());
+               for (Resource r : resources) {
+                       String uri = processor.syncRequest(new PossibleURI(r));
+                       if (uri != null)
+                               result.add(uri);
+               }
+               return result;
+       }
+
+}