]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java
Added alternatives taking RequestProcessor to WorkbenchSelectionUtils.
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / selection / WorkbenchSelectionUtils.java
index 765a37c2a8e60c8c4f009bf513a659044a4a8863..ecdda45c24ba654de669a56bf052a9d8255d4e1c 100644 (file)
@@ -33,24 +33,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 +70,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);
        }
@@ -137,11 +127,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 +159,16 @@ 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);
+    }
+
 }