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