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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils.ui;
\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 public class AdaptionUtils {
\r
28 @SuppressWarnings("unchecked")
\r
29 public static <T> T adaptToSingle(Object o, Class<T> clazz) {
\r
30 if (clazz.isInstance(o)) {
\r
32 } else if (o instanceof IStructuredSelection) {
\r
33 IStructuredSelection iss = (IStructuredSelection) o;
\r
34 if (iss.size() != 1)
\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
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
51 return adaptToSingle(obj, clazz);
\r
57 * Adapts given object to collection of objects of given class.
\r
60 * @return collection of objects of given class.
\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
78 return Collections.EMPTY_LIST;
\r
79 return adaptToCollection(obj, clazz);
\r
81 return Collections.EMPTY_LIST;
\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
89 if (t != null && !result.contains(t))
\r