]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/AdaptionUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / AdaptionUtils.java
diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/AdaptionUtils.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/AdaptionUtils.java
new file mode 100644 (file)
index 0000000..4318b34
--- /dev/null
@@ -0,0 +1,95 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.ui;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+\r
+import org.eclipse.core.runtime.IAdaptable;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.simantics.utils.Container;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ * @author Marko Luukkainen\r
+ */\r
+public class AdaptionUtils {\r
+\r
+    @SuppressWarnings("unchecked")\r
+    public static <T> T adaptToSingle(Object o, Class<T> clazz) {\r
+        if (clazz.isInstance(o)) {\r
+             return (T)o;\r
+        } else if (o instanceof IStructuredSelection) {\r
+            IStructuredSelection iss = (IStructuredSelection) o;\r
+            if (iss.size() != 1)\r
+                return null;\r
+            Object element = iss.getFirstElement();\r
+            return adaptToSingle(element, clazz);\r
+        } else if (o instanceof Collection<?>) {\r
+            Collection<?> c = (Collection<?>) o;\r
+            if (c.size() != 1)\r
+                return null;\r
+            Object element = c.iterator().next();\r
+            return adaptToSingle(element, clazz);\r
+        } else if (o instanceof IAdaptable) {\r
+            IAdaptable a = (IAdaptable) o;\r
+            return (T)a.getAdapter(clazz);\r
+        } else if (o instanceof Container<?>) {\r
+            Object obj = ((Container<?>) o).get();\r
+            if (obj == o)\r
+                return null;\r
+            return adaptToSingle(obj, clazz);\r
+        }\r
+        return null;\r
+    }\r
+    \r
+    /**\r
+        * Adapts given object to collection of objects of given class.\r
+        * @param o\r
+        * @param clazz\r
+        * @return collection of objects of given class. \r
+        */\r
+       @SuppressWarnings("unchecked")\r
+       public static <T> Collection<T> adaptToCollection(Object o, Class<T> clazz) {\r
+               if (clazz.isInstance(o)) {\r
+            return Collections.singletonList((T)o);\r
+               } else if (o instanceof IStructuredSelection) {\r
+            IStructuredSelection iss = (IStructuredSelection) o;\r
+            return adaptToCollection(iss.toArray(), clazz);\r
+        } else if (o instanceof Collection<?>) {\r
+            Collection<?> c = (Collection<?>) o;\r
+            return adaptToCollection(c.toArray(), clazz);\r
+        } else if (o instanceof IAdaptable) {\r
+            IAdaptable a = (IAdaptable) o;\r
+            return Collections.singletonList((T)a.getAdapter(clazz));\r
+        } else if (o instanceof Container<?>) {\r
+            Object obj = ((Container<?>) o).get();\r
+            if (obj == o)\r
+                return Collections.EMPTY_LIST;\r
+            return adaptToCollection(obj, clazz);\r
+        }\r
+        return Collections.EMPTY_LIST;\r
+    }\r
+       \r
+       public static <T> Collection<T> adaptToCollection(Object arr[], Class<T> clazz) {\r
+               Collection<T> result = new ArrayList<T>();\r
+               for (Object o : arr) {\r
+                       Collection<T> tColl = adaptToCollection(o, clazz); \r
+                       for (T t : tColl)\r
+                               if (t != null && !result.contains(t))\r
+                                       result.add(t);\r
+               }\r
+               return result;\r
+       }\r
+\r
+}\r