X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=org.simantics.g3d%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Ftools%2FAdaptationUtils.java;h=f6c7f275b8db31f15f1a9aca3775eeb8d08e4d6e;hb=53d55c24c779745f188bdb18d32f71d20acb61b2;hp=5902b0ab8884bdf8af5a1cb88d2b520d24bc4d4a;hpb=87b3241ec277ba3d8e414b26186a032c9cdcaeed;p=simantics%2F3d.git diff --git a/org.simantics.g3d/src/org/simantics/g3d/tools/AdaptationUtils.java b/org.simantics.g3d/src/org/simantics/g3d/tools/AdaptationUtils.java index 5902b0ab..f6c7f275 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/tools/AdaptationUtils.java +++ b/org.simantics.g3d/src/org/simantics/g3d/tools/AdaptationUtils.java @@ -1,3 +1,14 @@ +/******************************************************************************* + * Copyright (c) 2012, 2013 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.g3d.tools; import java.util.ArrayList; @@ -22,32 +33,32 @@ public class AdaptationUtils { * @return object of given class or null */ @SuppressWarnings("unchecked") - public static T adaptToSingle(Object o, Class clazz) { - 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 (clazz.isInstance(o)) { - return (T)o; - } else if (o instanceof Container) { - Object obj = ((Container) o).get(); - if (obj == o) - return null; - return adaptToSingle(obj, clazz); - } - return null; - } + public static T adaptToSingle(Object o, Class clazz) { + 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 (clazz.isInstance(o)) { + return (T)o; + } else if (o instanceof Container) { + Object obj = ((Container) o).get(); + if (obj == o) + return null; + return adaptToSingle(obj, clazz); + } + return null; + } /** * * @param o @@ -57,24 +68,24 @@ public class AdaptationUtils { @SuppressWarnings("unchecked") public static Collection adaptToCollection(Object o, Class clazz) { if (clazz.isInstance(o)) { - return Collections.singletonList((T)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; - } + 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 Collection adaptToCollection(Object arr[], Class clazz) { Collection result = new ArrayList();