]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java
Added resourceId and GUID to diagram DnD content
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / selection / WorkbenchSelectionUtils.java
index 765a37c2a8e60c8c4f009bf513a659044a4a8863..111ac8a3a2722b89b7945343830d059e2c3d82f5 100644 (file)
@@ -1,7 +1,11 @@
 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;
@@ -11,7 +15,14 @@ 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.PossibleVariableGUID;
+import org.simantics.db.layer0.request.PossibleURI;
+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;
@@ -33,24 +44,28 @@ public class WorkbenchSelectionUtils {
        }
 
        public static String getPossibleJSON(Object selection) throws DatabaseException {
-               if(selection instanceof WorkbenchSelectionElement) return getPossibleJSON((WorkbenchSelectionElement)selection);
-               WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
-               if(element == null) return null;
-               return getPossibleJSON(element);
+               WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
+               return element != null ? getPossibleJSON(element) : null;
        }
 
        public static Resource getPossibleResource(Object selection) throws DatabaseException {
-               if(selection instanceof WorkbenchSelectionElement) return getPossibleResource((WorkbenchSelectionElement)selection);
-               WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
-               if(element == null) return null;
-               return getPossibleResource(element);
+               WorkbenchSelectionElement element = getPossibleWorkbenchSelectionElement(selection);
+               return element != null ? getPossibleResource(element) : null;
        }
 
        public static Variable getPossibleVariable(Object selection) throws DatabaseException {
-               if(selection instanceof WorkbenchSelectionElement) return getPossibleVariable((WorkbenchSelectionElement)selection);
-               WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
-               if(element == null) return null;
-               return getPossibleVariable(element);
+               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 {
@@ -66,20 +81,6 @@ public class WorkbenchSelectionUtils {
                return wse.getContent(contentType);
        }
 
-//     public static <T> T getPossibleExplorerInput(Object selection) throws DatabaseException {
-//             if(selection instanceof WorkbenchSelectionElement) return getPossibleExplorerInput((WorkbenchSelectionElement)selection);
-//             WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
-//             if(element == null) return null;
-//             return getPossibleExplorerInput(element);
-//     }
-
-//     public static <T> T getPossibleExplorerColumn(Object selection) throws DatabaseException {
-//             if(selection instanceof WorkbenchSelectionElement) return getPossibleExplorerColumn((WorkbenchSelectionElement)selection);
-//             WorkbenchSelectionElement element = ISelectionUtils.filterSingleSelection(selection, WorkbenchSelectionElement.class);
-//             if(element == null) return null;
-//             return getPossibleExplorerColumn(element);
-//     }
-       
        public static String getPossibleJSON(WorkbenchSelectionElement wse) throws DatabaseException {
                return getPossibleJSON(Simantics.getSession(), wse);
        }
@@ -102,16 +103,29 @@ public class WorkbenchSelectionUtils {
 
        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));
-                       return "{ \"type\":\"Variable\", \"uri\" : \"" + uri + "\" }";
+                       String guid = processor.syncRequest(new PossibleVariableGUID(var));
+                       return toJSONObjectString(
+                                       "type", "\"Variable\"",
+                                       "uri", safeQuotedString(uri),
+                                       "guid", safeQuotedString(guid),
+                                       "resourceId", res == null ? "" : Long.toString(res.getResourceId()),
+                                       "typeResources", typesStr);
                }
-               Resource res = getPossibleResource(processor, input);
                if(res != null) {
-                       return "{ type:\"Resource\" }";
+                   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\" }";
-               
+               return "{ \"type\": \"Unknown\" }";
        }
 
        public static Resource getPossibleResource(RequestProcessor processor, Object input) throws DatabaseException {
@@ -137,11 +151,6 @@ public class WorkbenchSelectionUtils {
                return getPossibleElement(input, new AnyVariable(processor));
        }
 
-//     @SuppressWarnings("unchecked")
-//     public static <T> T getPossibleExplorerInput(WorkbenchSelectionElement input) throws DatabaseException {
-//             return ((T)getPossibleElement(input, new ExplorerInputContentType()));
-//     }
-
        public static <T> T getPossibleElement(Object input, WorkbenchSelectionContentType<T> contentType) {
                Object single = getPossibleSingleElement(input);
                if(single == null) return null;
@@ -174,6 +183,93 @@ public class WorkbenchSelectionUtils {
        } 
        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;
+       }
+
 }