1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.utils;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.ui.PlatformUI;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.common.ResourceArray;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.variable.Variable;
26 import org.simantics.utils.Container;
29 * @author Tuukka Lehtonen
31 public class ResourceAdaptionUtils {
34 * Tries to adapt an object into a {@link Resource}. This is successful if
35 * the object already is a {@link ResourceContainer} or it is adaptable into
36 * a {@link Resource} via {@link IAdaptable}.
39 * @return a Resource or <code>null</code> if not adaptable to
42 public static Resource adaptToResource(Object o) {
43 if (o instanceof Container<?>) {
44 Object obj = ((Container<?>) o).get();
45 if (obj instanceof Resource)
46 return (Resource) obj;
47 } else if (o instanceof IAdaptable) {
48 IAdaptable a = (IAdaptable) o;
49 Resource r = (Resource) a.getAdapter(Resource.class);
55 public static Resource adaptToResource(ReadGraph graph, Object o) throws DatabaseException {
56 Resource r = adaptToResource(o);
57 if(r != null) return r;
58 if(o instanceof Variable) {
59 Variable var = (Variable)o;
60 return var.getPossibleRepresents(graph);
61 } else if (o instanceof IAdaptable) {
62 IAdaptable a = (IAdaptable) o;
63 Variable var = (Variable) a.getAdapter(Variable.class);
65 return var.getPossibleRepresents(graph);
70 private static Resource[] toResources(IStructuredSelection iss) {
71 if (iss == null || iss.isEmpty())
74 ArrayList<Resource> list = new ArrayList<Resource>(iss.size());
75 for (Iterator<?> iterator = iss.iterator(); iterator.hasNext();) {
76 Object o = iterator.next();
77 Resource r = adaptToResource(o);
81 return list.toArray(Resource.NONE);
85 * Tries to adapt any Object into a {@link Resource}. Successful if the
86 * object is an IStructuredSelection of size one and its content is
87 * adaptable to a {@link Resource} according to {@link #adaptToResource(Object)},
88 * or the object itself is adaptable by {@link #adaptToResource(Object)}.
90 * @param s any selection
91 * @return a Resource, or <code>null</code> if not adaptable to Resource
92 * or selection size is not one.
94 public static Resource toSingleResource(Object o) {
95 if (o instanceof IStructuredSelection) {
96 IStructuredSelection iss = (IStructuredSelection) o;
99 Object element = iss.getFirstElement();
100 return adaptToResource(element);
102 return adaptToResource(o);
105 public static Resource toSingleResource(ReadGraph graph, Object o) throws DatabaseException {
106 if (o instanceof IStructuredSelection) {
107 IStructuredSelection iss = (IStructuredSelection) o;
110 Object element = iss.getFirstElement();
111 return adaptToResource(graph, element);
113 return adaptToResource(graph, o);
117 * Tries to adapt any Object into an array of {@link Resource}s. Only
118 * successful if {@link #adaptToResource(Object)} succeeds or the object is an
119 * IStructuredSelection that contains something adaptable to resources.
121 * @param s any selection
122 * @return every {@link Resource} that was adaptable or an array of size 0
123 * if nothing could be adapted.
125 public static Resource[] toResources(Object o) {
126 if (o instanceof IStructuredSelection) {
127 return toResources((IStructuredSelection) o);
129 Resource r = adaptToResource(o);
130 return (r == null) ? Resource.NONE : new Resource[] { r };
133 ///////////////////////////////////////////////////////////////////////////
136 * Tries to adapt an object into a {@link ResourceArray}. This is successful if
137 * the object already is a {@link ResourceContainer} or it is adaptable into
138 * a {@link ResourceArray} via {@link IAdaptable}.
140 * @param o any object
141 * @return a Resource or <code>null</code> if not adaptable to
144 private static ResourceArray adaptToResourceArray(Object o) {
145 if (o instanceof Container<?>) {
146 Object obj = ((Container<?>) o).get();
147 if (obj instanceof ResourceArray)
148 return (ResourceArray) obj;
149 if (obj instanceof Resource)
150 return new ResourceArray((Resource) obj);
152 if (o instanceof Resource[]) {
153 return new ResourceArray((Resource[]) o);
155 if (o instanceof ResourceArray) {
156 return (ResourceArray) o;
157 } else if (o instanceof IAdaptable) {
158 IAdaptable a = (IAdaptable) o;
159 ResourceArray r = (ResourceArray) a.getAdapter(ResourceArray.class);
160 if (r!=null) return r;
161 Resource[] r3 = (Resource[]) a.getAdapter(Resource[].class);
162 if (r3!=null) return new ResourceArray(r3);
163 Resource r2 = (Resource) a.getAdapter(Resource.class);
164 if (r2!=null) return new ResourceArray(r2);
170 * Tries to adapt an object into a {@link ResourceArray}. This is successful if
171 * the object already is a {@link ResourceContainer} or it is adaptable into
172 * a {@link ResourceArray} via {@link IAdaptable}.
174 * @param o any object
175 * @return a Resource or <code>null</code> if not adaptable to
176 * {@link ResourceArray[]}
178 private static ResourceArray[] adaptToResourceArrays(Object o) {
179 if (o instanceof ResourceArray[]) {
180 return (ResourceArray[]) o;
181 } else if (o instanceof IAdaptable) {
182 IAdaptable a = (IAdaptable) o;
183 ResourceArray[] ras = (ResourceArray[]) a.getAdapter(ResourceArray[].class);
184 if (ras!=null) return ras;
186 ResourceArray ra = adaptToResourceArray(o);
187 return ra != null ? new ResourceArray[] { ra } : null;
190 private static ResourceArray[] toResourceArrays(IStructuredSelection iss) {
191 if (iss == null || iss.isEmpty())
192 return ResourceArray.NONE;
194 ArrayList<ResourceArray> list = new ArrayList<ResourceArray>(iss.size());
195 for (Iterator<?> iterator = iss.iterator(); iterator.hasNext();) {
196 Object o = iterator.next();
197 ResourceArray[] r = adaptToResourceArrays(o);
199 for (ResourceArray ra : r)
202 return list.toArray(ResourceArray.NONE);
206 * Tries to adapt any Object into a {@link Resource}. Successful if the
207 * object is an IStructuredSelection of size one and its content is
208 * adaptable to a {@link Resource} according to {@link #adaptToResource(Object)},
209 * or the object itself is adaptable by {@link #adaptToResource(Object)}.
211 * @param s any selection
212 * @return a Resource, or <code>null</code> if not adaptable to Resource
213 * or selection size is not one.
215 public static ResourceArray toSingleResourceArray(Object o) {
216 if (o instanceof IStructuredSelection) {
217 IStructuredSelection iss = (IStructuredSelection) o;
220 Object element = iss.getFirstElement();
221 return adaptToResourceArray(element);
223 return adaptToResourceArray(o);
227 * Tries to adapt any Object into an array of {@link Resource}s. Only
228 * successful if the input object is directly adaptable to ResourceArray[],
229 * if {@link #adaptToResourceArray(Object)} succeeds or the object is an
230 * IStructuredSelection containing resources in some form.
232 * @param s any selection
233 * @return every {@link Resource} that was adaptable or an array of size 0
234 * if nothing could be adapted.
236 public static ResourceArray[] toResourceArrays(Object o) {
237 if (o instanceof List<?>) {
238 List<?> list = (List<?>)o;
239 ArrayList<ResourceArray> out = new ArrayList<ResourceArray>(list.size());
240 for (Object i : list) {
241 ResourceArray[] r = adaptToResourceArrays(i);
243 for (ResourceArray ra : r)
246 return out.toArray(ResourceArray.NONE);
248 if (o instanceof IStructuredSelection) {
249 return toResourceArrays((IStructuredSelection) o);
251 ResourceArray r = adaptToResourceArray(o);
252 return (r == null) ? ResourceArray.NONE : new ResourceArray[] { r };
255 public static Resource toSingleWorkbenchResource() {
256 return toSingleResource(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection());