X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Ftools%2FAdaptationUtils.java;fp=org.simantics.g3d%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Ftools%2FAdaptationUtils.java;h=5902b0ab8884bdf8af5a1cb88d2b520d24bc4d4a;hb=87b3241ec277ba3d8e414b26186a032c9cdcaeed;hp=0000000000000000000000000000000000000000;hpb=1f0bcd66274375f2278d2e6c486cb28257a5f7b2;p=simantics%2F3d.git diff --git a/org.simantics.g3d/src/org/simantics/g3d/tools/AdaptationUtils.java b/org.simantics.g3d/src/org/simantics/g3d/tools/AdaptationUtils.java new file mode 100644 index 00000000..5902b0ab --- /dev/null +++ b/org.simantics.g3d/src/org/simantics/g3d/tools/AdaptationUtils.java @@ -0,0 +1,89 @@ +package org.simantics.g3d.tools; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.simantics.utils.Container; + +/** + * @author Antti Villberg + * @author Marko Luukkainen + * + * TODO: merge to os.ui.utils. + */ +public class AdaptationUtils { + /** + * + * @param o + * @param clazz + * @return object of given class or null + */ + @SuppressWarnings("unchecked") + public static T adaptToSingle(Object o, Class clazz) { + if (o instanceof IStructuredSelection) { + IStructuredSelection iss = (IStructuredSelection) o; + if (iss.size() != 1) + return null; + Object element = iss.getFirstElement(); + return adaptToSingle(element, clazz); + } else if (o instanceof Collection) { + Collection c = (Collection) o; + if (c.size() != 1) + return null; + Object element = c.iterator().next(); + return adaptToSingle(element, clazz); + } else if (o instanceof IAdaptable) { + IAdaptable a = (IAdaptable) o; + return (T)a.getAdapter(clazz); + } else if (clazz.isInstance(o)) { + return (T)o; + } else if (o instanceof Container) { + Object obj = ((Container) o).get(); + if (obj == o) + return null; + return adaptToSingle(obj, clazz); + } + return null; + } + /** + * + * @param o + * @param clazz + * @return collection of objects of given class. + */ + @SuppressWarnings("unchecked") + public static Collection adaptToCollection(Object o, Class clazz) { + if (clazz.isInstance(o)) { + return Collections.singletonList((T)o); + } else if (o instanceof IStructuredSelection) { + IStructuredSelection iss = (IStructuredSelection) o; + return adaptToCollection(iss.toArray(), clazz); + } else if (o instanceof Collection) { + Collection c = (Collection) o; + return adaptToCollection(c.toArray(), clazz); + } else if (o instanceof IAdaptable) { + IAdaptable a = (IAdaptable) o; + return Collections.singletonList((T)a.getAdapter(clazz)); + } else if (o instanceof Container) { + Object obj = ((Container) o).get(); + if (obj == o) + return Collections.EMPTY_LIST; + return adaptToCollection(obj, clazz); + } + return Collections.EMPTY_LIST; + } + + public static Collection adaptToCollection(Object arr[], Class clazz) { + Collection result = new ArrayList(); + for (Object o : arr) { + Collection tColl = adaptToCollection(o, clazz); + for (T t : tColl) + if (t != null && !result.contains(t)) + result.add(t); + } + return result; + } +}