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