]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/utils/ResourceAdaptionUtils.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / utils / ResourceAdaptionUtils.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.ui.utils;
13
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
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;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class ResourceAdaptionUtils {
32
33     /**
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}.
37      * 
38      * @param o any object
39      * @return a Resource or <code>null</code> if not adaptable to
40      *         {@link Resource}
41      */
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);
50             return r;
51         }
52         return null;
53     }
54
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);
64             if(var != null)
65                 return var.getPossibleRepresents(graph);
66         }
67         return null;
68     }
69     
70     private static Resource[] toResources(IStructuredSelection iss) {
71         if (iss == null || iss.isEmpty())
72             return Resource.NONE;
73
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);
78             if (r != null)
79                 list.add(r);
80         }
81         return list.toArray(Resource.NONE);
82     }
83
84     /**
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)}.
89      * 
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.
93      */
94     public static Resource toSingleResource(Object o) {
95         if (o instanceof IStructuredSelection) {
96             IStructuredSelection iss = (IStructuredSelection) o;
97             if (iss.size() != 1)
98                 return null;
99             Object element = iss.getFirstElement();
100             return adaptToResource(element);
101         }
102         return adaptToResource(o);
103     }
104
105     public static Resource toSingleResource(ReadGraph graph, Object o) throws DatabaseException {
106         if (o instanceof IStructuredSelection) {
107             IStructuredSelection iss = (IStructuredSelection) o;
108             if (iss.size() != 1)
109                 return null;
110             Object element = iss.getFirstElement();
111             return adaptToResource(graph, element);
112         }
113         return adaptToResource(graph, o);
114     }
115     
116     /**
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.
120      * 
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.
124      */
125     public static Resource[] toResources(Object o) {
126         if (o instanceof IStructuredSelection) {
127             return toResources((IStructuredSelection) o);
128         }
129         Resource r = adaptToResource(o);
130         return (r == null) ? Resource.NONE : new Resource[] { r };
131     }
132
133     ///////////////////////////////////////////////////////////////////////////
134
135     /**
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}.
139      * 
140      * @param o any object
141      * @return a Resource or <code>null</code> if not adaptable to
142      *         {@link Resource}
143      */
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);
151         }
152         if (o instanceof Resource[]) {
153             return new ResourceArray((Resource[]) o);
154         }
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);
165         }
166         return null;
167     }
168
169     /**
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}.
173      * 
174      * @param o any object
175      * @return a Resource or <code>null</code> if not adaptable to
176      *         {@link ResourceArray[]}
177      */
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;
185         }
186         ResourceArray ra = adaptToResourceArray(o);
187         return ra != null ? new ResourceArray[] { ra } : null;
188     }
189
190     private static ResourceArray[] toResourceArrays(IStructuredSelection iss) {
191         if (iss == null || iss.isEmpty())
192             return ResourceArray.NONE;
193
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);
198             if (r != null)
199                 for (ResourceArray ra : r)
200                     list.add(ra);
201         }
202         return list.toArray(ResourceArray.NONE);
203     }
204
205     /**
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)}.
210      * 
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.
214      */
215     public static ResourceArray toSingleResourceArray(Object o) {
216         if (o instanceof IStructuredSelection) {
217             IStructuredSelection iss = (IStructuredSelection) o;
218             if (iss.size() != 1)
219                 return null;
220             Object element = iss.getFirstElement();
221             return adaptToResourceArray(element);
222         }
223         return adaptToResourceArray(o);
224     }
225
226     /**
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.
231      * 
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.
235      */
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);
242                 if (r != null)
243                     for (ResourceArray ra : r)
244                         out.add(ra);
245             }
246             return out.toArray(ResourceArray.NONE);
247         }
248         if (o instanceof IStructuredSelection) {
249             return toResourceArrays((IStructuredSelection) o);
250         }
251         ResourceArray r = adaptToResourceArray(o);
252         return (r == null) ? ResourceArray.NONE : new ResourceArray[] { r };
253     }
254
255     public static Resource toSingleWorkbenchResource() {
256         return toSingleResource(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection());
257     }
258
259 }