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