1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
14 import java.lang.reflect.Array;
15 import java.util.Collection;
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;
38 * A utility on top of the generic {@link PropertyUtil} for working with graph
39 * database properties.
41 * @author Tuukka Lehtonen
43 public class GraphPropertyUtil {
50 * @throws DatabaseException
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
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();
68 typeString = dt.toSingleLineString();
70 return literalTypeName + " : " + typeString;
71 } catch (RuntimeBindingConstructionException e) {
73 } catch (AdaptException e) {
75 } catch (DatabaseException e) {
78 // Fallback to OLD logic for some kind of backwards compatibility
79 if (!graph.isInstanceOf(resource, l0.Literal))
80 return ValueType.toString(ValueType.NoValue);
82 Object value = graph.getValue(resource);
83 return ValueType.toString(ValueType.convert(value));
84 } catch (DoesNotContainValueException ee) {
85 return ValueType.toString(ValueType.NoValue);
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);
94 return propertyFactory.create(null);
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
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));
109 boolean isEnum = IsEnumeratedValue.isEnumeratedValue(graph, resource);
111 //System.out.println("isEnum: " + GraphUtils.getReadableName(graph, resource));
112 return propertyFactory.create(null);
114 Layer0 l0 = Layer0.getInstance(graph);
115 boolean isLiteral = graph.isInstanceOf(resource, l0.Literal);
117 //System.out.println("isValue: " + GraphUtils.getReadableName(graph, resource));
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);
125 return propertyFactory.create(ValueRange.make(0, size));
127 return propertyFactory.create(ValueRange.make(0, 0));
129 return propertyFactory.create(null);
130 } catch (DoesNotContainValueException e) {
133 // Check if is list..
134 boolean isList = graph.isInstanceOf(resource, l0.OrderedSet);
135 //System.out.println("IS LIST ? "+isList);
137 int size = OrderedSetUtils.toList(graph, resource).size();
138 //System.out.println("list size "+size);
139 return propertyFactory.create(ValueRange.make(0, size));
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)
150 if (property instanceof IArrayProperty) {
151 IArrayProperty array = (IArrayProperty) property;
152 ValueRange range = array.getRange();
154 Collection<IArrayProperty> children = PropertyUtil.subnodify(array, 0, range.size());
155 return NodeContextUtil.toContextsWithInput(children);
158 // Plain properties do not have children.
159 return NodeContext.NONE;
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.
169 public static Boolean tryValueHasChildren(ReadGraph graph, Resource r) throws DatabaseException {
170 Layer0 l0 = Layer0.getInstance(graph);
171 if (graph.isInstanceOf(r, l0.Literal)) {
173 Object o = graph.getValue(r);
174 return Boolean.valueOf(Array.getLength(o) > 1);
175 } catch (DoesNotContainValueException e) {
176 return Boolean.FALSE;