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