]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/tools/AdaptationUtils.java
White space clean-up
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / tools / AdaptationUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * 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.g3d.tools;\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  * TODO: merge to os.ui.utils.\r
27  */\r
28 public class AdaptationUtils {\r
29         /**\r
30          * \r
31          * @param o\r
32          * @param clazz\r
33          * @return object of given class or null\r
34          */\r
35         @SuppressWarnings("unchecked")\r
36         public static <T> T adaptToSingle(Object o, Class<T> clazz) {\r
37                 if (o instanceof IStructuredSelection) {\r
38                         IStructuredSelection iss = (IStructuredSelection) o;\r
39                         if (iss.size() != 1)\r
40                                 return null;\r
41                         Object element = iss.getFirstElement();\r
42                         return adaptToSingle(element, clazz);\r
43                 } else if (o instanceof Collection<?>) {\r
44                         Collection<?> c = (Collection<?>) o;\r
45                         if (c.size() != 1)\r
46                                 return null;\r
47                         Object element = c.iterator().next();\r
48                         return adaptToSingle(element, clazz);\r
49                 } else if (o instanceof IAdaptable) {\r
50                         IAdaptable a = (IAdaptable) o;\r
51                         return (T)a.getAdapter(clazz);\r
52                 } else if (clazz.isInstance(o)) {\r
53                         return (T)o;\r
54                 } else if (o instanceof Container<?>) {\r
55                         Object obj = ((Container<?>) o).get();\r
56                         if (obj == o)\r
57                                 return null;\r
58                         return adaptToSingle(obj, clazz);\r
59                 }\r
60                 return null;\r
61         }\r
62         /**\r
63          * \r
64          * @param o\r
65          * @param clazz\r
66          * @return collection of objects of given class. \r
67          */\r
68         @SuppressWarnings("unchecked")\r
69         public static <T> Collection<T> adaptToCollection(Object o, Class<T> clazz) {\r
70                 if (clazz.isInstance(o)) {\r
71                         return Collections.singletonList((T)o);\r
72                 } else if (o instanceof IStructuredSelection) {\r
73                         IStructuredSelection iss = (IStructuredSelection) o;\r
74                         return adaptToCollection(iss.toArray(), clazz);\r
75                 } else if (o instanceof Collection<?>) {\r
76                         Collection<?> c = (Collection<?>) o;\r
77                         return adaptToCollection(c.toArray(), clazz);\r
78                 } else if (o instanceof IAdaptable) {\r
79                         IAdaptable a = (IAdaptable) o;\r
80                         return Collections.singletonList((T)a.getAdapter(clazz));\r
81                 } else if (o instanceof Container<?>) {\r
82                         Object obj = ((Container<?>) o).get();\r
83                         if (obj == o)\r
84                                 return Collections.EMPTY_LIST;\r
85                         return adaptToCollection(obj, clazz);\r
86                 }\r
87                 return Collections.EMPTY_LIST;\r
88         }\r
89         \r
90         public static <T> Collection<T> adaptToCollection(Object arr[], Class<T> clazz) {\r
91                 Collection<T> result = new ArrayList<T>();\r
92                 for (Object o : arr) {\r
93                         Collection<T> tColl = adaptToCollection(o, clazz); \r
94                         for (T t : tColl)\r
95                                 if (t != null && !result.contains(t))\r
96                                         result.add(t);\r
97                 }\r
98                 return result;\r
99         }\r
100 }\r