--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.graph.impl;\r
+\r
+import java.lang.reflect.Array;\r
+import java.util.Collection;\r
+\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.common.NodeContextUtil;\r
+import org.simantics.browsing.ui.common.property.IArrayProperty;\r
+import org.simantics.browsing.ui.common.property.IProperty;\r
+import org.simantics.browsing.ui.common.property.IPropertyFactory;\r
+import org.simantics.browsing.ui.common.property.PropertyUtil;\r
+import org.simantics.browsing.ui.common.property.PropertyUtil.ValueType;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.adapter.AdaptException;\r
+import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.databoard.type.ArrayType;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.OrderedSetUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.DoesNotContainValueException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.datastructures.slice.ValueRange;\r
+\r
+/**\r
+ * A utility on top of the generic {@link PropertyUtil} for working with graph\r
+ * database properties.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class GraphPropertyUtil {\r
+\r
+ /**\r
+ * @param graph\r
+ * @param resource\r
+ * @param sizeHint\r
+ * @return\r
+ * @throws DatabaseException\r
+ */\r
+ public static String tryGetValueTypeString(ReadGraph graph, Resource resource, int sizeHint) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ if (graph.isInstanceOf(resource, l0.OrderedSet))\r
+ return "list"; // FIXME\r
+\r
+ try {\r
+ Resource literalType = graph.getSingleType(resource, l0.Literal);\r
+ String literalTypeName = graph.getRelatedValue(literalType, l0.HasName, Bindings.STRING);\r
+ Datatype dt = graph.getDataType(resource);\r
+ String typeString = null;\r
+ if (dt instanceof ArrayType) {\r
+ ArrayType at = (ArrayType) dt;\r
+ at = (ArrayType) Bindings.getBindingUnchecked(Datatype.class).clone(at);\r
+ at.setLength("" + sizeHint);\r
+ typeString = at.toSingleLineString();\r
+ } else {\r
+ typeString = dt.toSingleLineString();\r
+ }\r
+ return literalTypeName + " : " + typeString;\r
+ } catch (RuntimeBindingConstructionException e) {\r
+ // Shouldn't happen\r
+ } catch (AdaptException e) {\r
+ // Shouldn't happen\r
+ } catch (DatabaseException e) {\r
+ }\r
+\r
+ // Fallback to OLD logic for some kind of backwards compatibility \r
+ if (!graph.isInstanceOf(resource, l0.Literal))\r
+ return ValueType.toString(ValueType.NoValue);\r
+ try {\r
+ Object value = graph.getValue(resource);\r
+ return ValueType.toString(ValueType.convert(value));\r
+ } catch (DoesNotContainValueException ee) {\r
+ return ValueType.toString(ValueType.NoValue);\r
+ }\r
+ }\r
+\r
+ public static <T extends IProperty> T createProperty(ReadGraph graph, Resource resource, IPropertyFactory<T> propertyFactory) throws DatabaseException {\r
+// System.out.println("createProperty: " + GraphUtils.getReadableName(graph, resource));\r
+ T t = tryCreateProperty(graph, resource, propertyFactory);\r
+ if (t != null)\r
+ return t;\r
+ return propertyFactory.create(null);\r
+ }\r
+\r
+ /**\r
+ * @param <T>\r
+ * @param graph\r
+ * @param resource\r
+ * @param propertyFactory\r
+ * @return <code>null</code> if the specified resource is neither an\r
+ * enumerated value or an L0.Value instance\r
+ * @throws DatabaseException\r
+ */\r
+ public static <T extends IProperty> T tryCreateProperty(ReadGraph graph, Resource resource, IPropertyFactory<T> propertyFactory) throws DatabaseException {\r
+// System.out.println("tryCreateProperty: " + GraphUtils.getReadableName(graph, resource));\r
+\r
+ boolean isEnum = IsEnumeratedValue.isEnumeratedValue(graph, resource);\r
+ if (isEnum) {\r
+ //System.out.println("isEnum: " + GraphUtils.getReadableName(graph, resource));\r
+ return propertyFactory.create(null);\r
+ }\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ boolean isLiteral = graph.isInstanceOf(resource, l0.Literal);\r
+ if (isLiteral) {\r
+ //System.out.println("isValue: " + GraphUtils.getReadableName(graph, resource));\r
+ try {\r
+ Object value = graph.getValue(resource);\r
+ //System.out.println(" VALUE: " + value);\r
+ if(value.getClass().isArray()) {\r
+ //System.out.println(" IS ARRAY");\r
+ int size = Array.getLength(value);\r
+ if (size > 1)\r
+ return propertyFactory.create(ValueRange.make(0, size));\r
+ if (size == 0)\r
+ return propertyFactory.create(ValueRange.make(0, 0));\r
+ }\r
+ return propertyFactory.create(null);\r
+ } catch (DoesNotContainValueException e) {\r
+ }\r
+ } else {\r
+ // Check if is list..\r
+ boolean isList = graph.isInstanceOf(resource, l0.OrderedSet);\r
+ //System.out.println("IS LIST ? "+isList);\r
+ if(isList) {\r
+ int size = OrderedSetUtils.toList(graph, resource).size();\r
+ //System.out.println("list size "+size);\r
+ return propertyFactory.create(ValueRange.make(0, size));\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+ public static <T extends IProperty> NodeContext[] tryValueGetChildren(ReadGraph graph, Resource resource, IPropertyFactory<T> propertyFactory) throws DatabaseException {\r
+ T property = tryCreateProperty(graph, resource, propertyFactory);\r
+ if (property == null)\r
+ return null;\r
+\r
+ if (property instanceof IArrayProperty) {\r
+ IArrayProperty array = (IArrayProperty) property;\r
+ ValueRange range = array.getRange();\r
+\r
+ Collection<IArrayProperty> children = PropertyUtil.subnodify(array, 0, range.size());\r
+ return NodeContextUtil.toContextsWithInput(children);\r
+ }\r
+\r
+ // Plain properties do not have children.\r
+ return NodeContext.NONE;\r
+ }\r
+\r
+ /**\r
+ * @param graph\r
+ * @param r\r
+ * @return <code>null</code> if this is not a value,\r
+ * <code>Boolean.FALSE</code> if this is a scalar value and\r
+ * <code>Boolean.TRUE</code> if this is a vector value.\r
+ */\r
+ public static Boolean tryValueHasChildren(ReadGraph graph, Resource r) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ if (graph.isInstanceOf(r, l0.Literal)) {\r
+ try {\r
+ Object o = graph.getValue(r);\r
+ return Boolean.valueOf(Array.getLength(o) > 1);\r
+ } catch (DoesNotContainValueException e) {\r
+ return Boolean.FALSE;\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+}\r