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.browsing.ui.graph.impl;
\r
14 import java.lang.reflect.Array;
\r
15 import java.util.Collection;
\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
38 * A utility on top of the generic {@link PropertyUtil} for working with graph
\r
39 * database properties.
\r
41 * @author Tuukka Lehtonen
\r
43 public class GraphPropertyUtil {
\r
50 * @throws DatabaseException
\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
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
68 typeString = dt.toSingleLineString();
\r
70 return literalTypeName + " : " + typeString;
\r
71 } catch (RuntimeBindingConstructionException e) {
\r
73 } catch (AdaptException e) {
\r
75 } catch (DatabaseException e) {
\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
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
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
94 return propertyFactory.create(null);
\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
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
109 boolean isEnum = IsEnumeratedValue.isEnumeratedValue(graph, resource);
\r
111 //System.out.println("isEnum: " + GraphUtils.getReadableName(graph, resource));
\r
112 return propertyFactory.create(null);
\r
114 Layer0 l0 = Layer0.getInstance(graph);
\r
115 boolean isLiteral = graph.isInstanceOf(resource, l0.Literal);
\r
117 //System.out.println("isValue: " + GraphUtils.getReadableName(graph, resource));
\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
125 return propertyFactory.create(ValueRange.make(0, size));
\r
127 return propertyFactory.create(ValueRange.make(0, 0));
\r
129 return propertyFactory.create(null);
\r
130 } catch (DoesNotContainValueException e) {
\r
133 // Check if is list..
\r
134 boolean isList = graph.isInstanceOf(resource, l0.OrderedSet);
\r
135 //System.out.println("IS LIST ? "+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
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
150 if (property instanceof IArrayProperty) {
\r
151 IArrayProperty array = (IArrayProperty) property;
\r
152 ValueRange range = array.getRange();
\r
154 Collection<IArrayProperty> children = PropertyUtil.subnodify(array, 0, range.size());
\r
155 return NodeContextUtil.toContextsWithInput(children);
\r
158 // Plain properties do not have children.
\r
159 return NodeContext.NONE;
\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
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
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