1 /*******************************************************************************
\r
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g3d.tools;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.Collections;
\r
18 import org.eclipse.core.runtime.IAdaptable;
\r
19 import org.eclipse.jface.viewers.IStructuredSelection;
\r
20 import org.simantics.utils.Container;
\r
23 * @author Antti Villberg
\r
24 * @author Marko Luukkainen
\r
26 * TODO: merge to os.ui.utils.
\r
28 public class AdaptationUtils {
\r
33 * @return object of given class or null
\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
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
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
54 } else if (o instanceof Container<?>) {
\r
55 Object obj = ((Container<?>) o).get();
\r
58 return adaptToSingle(obj, clazz);
\r
66 * @return collection of objects of given class.
\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
84 return Collections.EMPTY_LIST;
\r
85 return adaptToCollection(obj, clazz);
\r
87 return Collections.EMPTY_LIST;
\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
95 if (t != null && !result.contains(t))
\r