]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / selection / WorkbenchSelectionUtils.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java b/bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java
new file mode 100644 (file)
index 0000000..e30b0e0
--- /dev/null
@@ -0,0 +1,179 @@
+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