]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/AdaptionUtils.java
Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / AdaptionUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.ui;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.Collections;\r
17 \r
18 import org.eclipse.core.runtime.IAdaptable;\r
19 import org.eclipse.jface.viewers.IStructuredSelection;\r
20 import org.simantics.utils.Container;\r
21 \r
22 /**\r
23  * @author Antti Villberg\r
24  * @author Marko Luukkainen\r
25  */\r
26 public class AdaptionUtils {\r
27 \r
28     @SuppressWarnings("unchecked")\r
29     public static <T> T adaptToSingle(Object o, Class<T> clazz) {\r
30          if (clazz.isInstance(o)) {\r
31              return (T)o;\r
32          } else if (o instanceof IStructuredSelection) {\r
33             IStructuredSelection iss = (IStructuredSelection) o;\r
34             if (iss.size() != 1)\r
35                 return null;\r
36             Object element = iss.getFirstElement();\r
37             return adaptToSingle(element, clazz);\r
38         } else if (o instanceof Collection<?>) {\r
39             Collection<?> c = (Collection<?>) o;\r
40             if (c.size() != 1)\r
41                 return null;\r
42             Object element = c.iterator().next();\r
43             return adaptToSingle(element, clazz);\r
44         } else if (o instanceof IAdaptable) {\r
45             IAdaptable a = (IAdaptable) o;\r
46             return (T)a.getAdapter(clazz);\r
47         } else if (o instanceof Container<?>) {\r
48             Object obj = ((Container<?>) o).get();\r
49             if (obj == o)\r
50                 return null;\r
51             return adaptToSingle(obj, clazz);\r
52         }\r
53         return null;\r
54     }\r
55     \r
56     /**\r
57          * Adapts given object to collection of objects of given class.\r
58          * @param o\r
59          * @param clazz\r
60          * @return collection of objects of given class. \r
61          */\r
62         @SuppressWarnings("unchecked")\r
63         public static <T> Collection<T> adaptToCollection(Object o, Class<T> clazz) {\r
64                 if (clazz.isInstance(o)) {\r
65             return Collections.singletonList((T)o);\r
66                 } else if (o instanceof IStructuredSelection) {\r
67             IStructuredSelection iss = (IStructuredSelection) o;\r
68             return adaptToCollection(iss.toArray(), clazz);\r
69         } else if (o instanceof Collection<?>) {\r
70             Collection<?> c = (Collection<?>) o;\r
71             return adaptToCollection(c.toArray(), clazz);\r
72         } else if (o instanceof IAdaptable) {\r
73             IAdaptable a = (IAdaptable) o;\r
74             return Collections.singletonList((T)a.getAdapter(clazz));\r
75         } else if (o instanceof Container<?>) {\r
76             Object obj = ((Container<?>) o).get();\r
77             if (obj == o)\r
78                 return Collections.EMPTY_LIST;\r
79             return adaptToCollection(obj, clazz);\r
80         }\r
81         return Collections.EMPTY_LIST;\r
82     }\r
83         \r
84         public static <T> Collection<T> adaptToCollection(Object arr[], Class<T> clazz) {\r
85                 Collection<T> result = new ArrayList<T>();\r
86                 for (Object o : arr) {\r
87                         Collection<T> tColl = adaptToCollection(o, clazz); \r
88                         for (T t : tColl)\r
89                                 if (t != null && !result.contains(t))\r
90                                         result.add(t);\r
91                 }\r
92                 return result;\r
93         }\r
94 \r
95 }\r