X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fselection%2FWorkbenchSelectionUtils.java;h=ecdda45c24ba654de669a56bf052a9d8255d4e1c;hb=refs%2Fchanges%2F15%2F415%2F4;hp=765a37c2a8e60c8c4f009bf513a659044a4a8863;hpb=34b35ae67bf4940f653690099759bc01699a56ab;p=simantics%2Fplatform.git 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 index 765a37c2a..ecdda45c2 100644 --- a/bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java +++ b/bundles/org.simantics.ui/src/org/simantics/ui/selection/WorkbenchSelectionUtils.java @@ -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 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 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 getPossibleExplorerInput(WorkbenchSelectionElement input) throws DatabaseException { -// return ((T)getPossibleElement(input, new ExplorerInputContentType())); -// } - public static T getPossibleElement(Object input, WorkbenchSelectionContentType 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 getPossibleObject(Object selection, Class clazz) { + return clazz.isInstance(selection) + ? (T) selection + : ISelectionUtils.filterSingleSelection(selection, clazz); + } + }