-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.utils.ui;
+
+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
+ */
+public class AdaptionUtils {
+
+ @SuppressWarnings("unchecked")
+ public static <T> T adaptToSingle(Object o, Class<T> clazz) {
+ if (clazz.isInstance(o)) {
+ return (T)o;
+ } else 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 (o instanceof Container<?>) {
+ Object obj = ((Container<?>) o).get();
+ if (obj == o)
+ return null;
+ return adaptToSingle(obj, clazz);
+ }
+ return null;
+ }
+
+ /**
+ * Adapts given object to collection of objects of given class.
+ * @param o
+ * @param clazz
+ * @return collection of objects of given class.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> Collection<T> adaptToCollection(Object o, Class<T> 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 <T> Collection<T> adaptToCollection(Object arr[], Class<T> clazz) {
+ Collection<T> result = new ArrayList<T>();
+ for (Object o : arr) {
+ Collection<T> tColl = adaptToCollection(o, clazz);
+ for (T t : tColl)
+ if (t != null && !result.contains(t))
+ result.add(t);
+ }
+ return result;
+ }
+
+}