]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GraphPropertyUtil.java
368beaf4a317b98f6b556626c63342063350c880
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / GraphPropertyUtil.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.browsing.ui.graph.impl;\r
13 \r
14 import java.lang.reflect.Array;\r
15 import java.util.Collection;\r
16 \r
17 import org.simantics.browsing.ui.NodeContext;\r
18 import org.simantics.browsing.ui.common.NodeContextUtil;\r
19 import org.simantics.browsing.ui.common.property.IArrayProperty;\r
20 import org.simantics.browsing.ui.common.property.IProperty;\r
21 import org.simantics.browsing.ui.common.property.IPropertyFactory;\r
22 import org.simantics.browsing.ui.common.property.PropertyUtil;\r
23 import org.simantics.browsing.ui.common.property.PropertyUtil.ValueType;\r
24 import org.simantics.databoard.Bindings;\r
25 import org.simantics.databoard.adapter.AdaptException;\r
26 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
27 import org.simantics.databoard.type.ArrayType;\r
28 import org.simantics.databoard.type.Datatype;\r
29 import org.simantics.db.ReadGraph;\r
30 import org.simantics.db.Resource;\r
31 import org.simantics.db.common.utils.OrderedSetUtils;\r
32 import org.simantics.db.exception.DatabaseException;\r
33 import org.simantics.db.exception.DoesNotContainValueException;\r
34 import org.simantics.layer0.Layer0;\r
35 import org.simantics.utils.datastructures.slice.ValueRange;\r
36 \r
37 /**\r
38  * A utility on top of the generic {@link PropertyUtil} for working with graph\r
39  * database properties.\r
40  * \r
41  * @author Tuukka Lehtonen\r
42  */\r
43 public class GraphPropertyUtil {\r
44 \r
45     /**\r
46      * @param graph\r
47      * @param resource\r
48      * @param sizeHint\r
49      * @return\r
50      * @throws DatabaseException\r
51      */\r
52     public static String tryGetValueTypeString(ReadGraph graph, Resource resource, int sizeHint) throws DatabaseException {\r
53         Layer0 l0 = Layer0.getInstance(graph);\r
54         if (graph.isInstanceOf(resource, l0.OrderedSet))\r
55             return "list"; // FIXME\r
56 \r
57         try {\r
58             Resource literalType = graph.getSingleType(resource, l0.Literal);\r
59             String literalTypeName = graph.getRelatedValue(literalType, l0.HasName, Bindings.STRING);\r
60             Datatype dt = graph.getDataType(resource);\r
61             String typeString = null;\r
62             if (dt instanceof ArrayType) {\r
63                 ArrayType at = (ArrayType) dt;\r
64                 at = (ArrayType) Bindings.getBindingUnchecked(Datatype.class).clone(at);\r
65                 at.setLength("" + sizeHint);\r
66                 typeString = at.toSingleLineString();\r
67             } else {\r
68                 typeString = dt.toSingleLineString();\r
69             }\r
70             return literalTypeName + " : " + typeString;\r
71         } catch (RuntimeBindingConstructionException e) {\r
72             // Shouldn't happen\r
73         } catch (AdaptException e) {\r
74             // Shouldn't happen\r
75         } catch (DatabaseException e) {\r
76         }\r
77 \r
78         // Fallback to OLD logic for some kind of backwards compatibility \r
79         if (!graph.isInstanceOf(resource, l0.Literal))\r
80             return ValueType.toString(ValueType.NoValue);\r
81         try {\r
82             Object value = graph.getValue(resource);\r
83             return ValueType.toString(ValueType.convert(value));\r
84         } catch (DoesNotContainValueException ee) {\r
85             return ValueType.toString(ValueType.NoValue);\r
86         }\r
87     }\r
88 \r
89     public static <T extends IProperty> T createProperty(ReadGraph graph, Resource resource, IPropertyFactory<T> propertyFactory) throws DatabaseException {\r
90 //      System.out.println("createProperty: " + GraphUtils.getReadableName(graph, resource));\r
91         T t = tryCreateProperty(graph, resource, propertyFactory);\r
92         if (t != null)\r
93             return t;\r
94         return propertyFactory.create(null);\r
95     }\r
96 \r
97     /**\r
98      * @param <T>\r
99      * @param graph\r
100      * @param resource\r
101      * @param propertyFactory\r
102      * @return <code>null</code> if the specified resource is neither an\r
103      *         enumerated value or an L0.Value instance\r
104      * @throws DatabaseException\r
105      */\r
106     public static <T extends IProperty> T tryCreateProperty(ReadGraph graph, Resource resource, IPropertyFactory<T> propertyFactory) throws DatabaseException {\r
107 //        System.out.println("tryCreateProperty: " + GraphUtils.getReadableName(graph, resource));\r
108 \r
109         boolean isEnum = IsEnumeratedValue.isEnumeratedValue(graph, resource);\r
110         if (isEnum) {\r
111             //System.out.println("isEnum: " + GraphUtils.getReadableName(graph, resource));\r
112             return propertyFactory.create(null);\r
113         }\r
114         Layer0 l0 = Layer0.getInstance(graph);\r
115         boolean isLiteral = graph.isInstanceOf(resource, l0.Literal);\r
116         if (isLiteral) {\r
117             //System.out.println("isValue: " + GraphUtils.getReadableName(graph, resource));\r
118             try {\r
119                 Object value = graph.getValue(resource);\r
120                 //System.out.println("  VALUE: " + value);\r
121                 if(value.getClass().isArray()) {\r
122                     //System.out.println("    IS ARRAY");\r
123                     int size = Array.getLength(value);\r
124                     if (size > 1)\r
125                         return propertyFactory.create(ValueRange.make(0, size));\r
126                     if (size == 0)\r
127                         return propertyFactory.create(ValueRange.make(0, 0));\r
128                 }\r
129                 return propertyFactory.create(null);\r
130             } catch (DoesNotContainValueException e) {\r
131             }\r
132         } else {\r
133             // Check if is list..\r
134             boolean isList = graph.isInstanceOf(resource, l0.OrderedSet);\r
135             //System.out.println("IS LIST ? "+isList);\r
136             if(isList) {\r
137                 int size = OrderedSetUtils.toList(graph, resource).size();\r
138                 //System.out.println("list size "+size);\r
139                 return propertyFactory.create(ValueRange.make(0, size));\r
140             }\r
141         }\r
142         return null;\r
143     }\r
144 \r
145     public static <T extends IProperty> NodeContext[] tryValueGetChildren(ReadGraph graph, Resource resource, IPropertyFactory<T> propertyFactory) throws DatabaseException {\r
146         T property = tryCreateProperty(graph, resource, propertyFactory);\r
147         if (property == null)\r
148             return null;\r
149 \r
150         if (property instanceof IArrayProperty) {\r
151             IArrayProperty array = (IArrayProperty) property;\r
152             ValueRange range = array.getRange();\r
153 \r
154             Collection<IArrayProperty> children = PropertyUtil.subnodify(array, 0, range.size());\r
155             return NodeContextUtil.toContextsWithInput(children);\r
156         }\r
157 \r
158         // Plain properties do not have children.\r
159         return NodeContext.NONE;\r
160     }\r
161 \r
162     /**\r
163      * @param graph\r
164      * @param r\r
165      * @return <code>null</code> if this is not a value,\r
166      *         <code>Boolean.FALSE</code> if this is a scalar value and\r
167      *         <code>Boolean.TRUE</code> if this is a vector value.\r
168      */\r
169     public static Boolean tryValueHasChildren(ReadGraph graph, Resource r) throws DatabaseException {\r
170         Layer0 l0 = Layer0.getInstance(graph);\r
171         if (graph.isInstanceOf(r, l0.Literal)) {\r
172             try {\r
173                 Object o = graph.getValue(r);\r
174                 return Boolean.valueOf(Array.getLength(o) > 1);\r
175             } catch (DoesNotContainValueException e) {\r
176                 return Boolean.FALSE;\r
177             }\r
178         }\r
179         return null;\r
180     }\r
181 \r
182 }\r