]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/property/PropertyUtil.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / property / PropertyUtil.java
diff --git a/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/property/PropertyUtil.java b/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/property/PropertyUtil.java
new file mode 100644 (file)
index 0000000..3950c80
--- /dev/null
@@ -0,0 +1,159 @@
+/*******************************************************************************\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.common.property;\r
+\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.utils.datastructures.slice.SliceUtil;\r
+import org.simantics.utils.datastructures.slice.Sliceable;\r
+import org.simantics.utils.datastructures.slice.ValueRange;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class PropertyUtil {\r
+\r
+    /**\r
+     * Use databoard instead of this?\r
+     */\r
+    public static enum ValueType {\r
+        NoValue,\r
+        BooleanValue,\r
+        ByteValue,\r
+        IntegerValue,\r
+        LongValue,\r
+        FloatValue,\r
+        DoubleValue,\r
+        StringValue,\r
+        UnrecognizedValue;\r
+\r
+        public static ValueType convert(Object obj) {\r
+            if (obj == null) return NoValue;\r
+            if (obj.getClass() == Boolean.class) return BooleanValue;\r
+            if (obj.getClass() == boolean[].class) return BooleanValue;\r
+            if (obj.getClass() == Byte.class) return ByteValue;\r
+            if (obj.getClass() == byte[].class) return ByteValue;\r
+            if (obj.getClass() == Integer.class) return IntegerValue;\r
+            if (obj.getClass() == int[].class) return IntegerValue;\r
+            if (obj.getClass() == Long.class) return LongValue;\r
+            if (obj.getClass() == long[].class) return LongValue;\r
+            if (obj.getClass() == Float.class) return FloatValue;\r
+            if (obj.getClass() == float[].class) return FloatValue;\r
+            if (obj.getClass() == Double.class) return DoubleValue;\r
+            if (obj.getClass() == double[].class) return DoubleValue;\r
+            if (obj.getClass() == String.class) return StringValue;\r
+            if (obj.getClass() == String[].class) return StringValue;\r
+            return UnrecognizedValue;\r
+        }\r
+\r
+        public static String toString(ValueType type) {\r
+            switch (type) {\r
+                case NoValue: return "no value";\r
+                case BooleanValue: return "boolean";\r
+                case ByteValue: return "byte";\r
+                case DoubleValue: return "double";\r
+                case FloatValue: return "float";\r
+                case IntegerValue: return "int32";\r
+                case LongValue: return "int64";\r
+                case StringValue: return "string";\r
+                case UnrecognizedValue: return "unknown value";\r
+                default:\r
+                    throw new IllegalArgumentException("unrecognized value type: "+ type);\r
+            }\r
+        }\r
+    }\r
+\r
+\r
+    /**\r
+     * Returns the child nodes of the specified IProperty, constructed with the\r
+     * specified IPropertyFactory.\r
+     * \r
+     * <p>\r
+     * Child nodes only exist for {@link IArrayProperty} instances whose value\r
+     * range size is > 1.\r
+     * </p>\r
+     * \r
+     * @param <T>\r
+     * @param property\r
+     * @return child nodes of the specified property\r
+     */\r
+    public static <T extends IProperty> NodeContext[] getChildren(IProperty property) {\r
+        return getChildren(property, false);\r
+    }\r
+\r
+    /**\r
+     * Returns the child nodes of the specified IProperty, constructed with the\r
+     * specified IPropertyFactory.\r
+     * \r
+     * <p>\r
+     * Child nodes only exist for {@link IArrayProperty} instances whose value\r
+     * range size is > 0. You can whether you want to create child nodes for\r
+     * ranges of size 1 or not.\r
+     * </p>\r
+     * \r
+     * @param <T>\r
+     * @param property\r
+     * @param createOneChild <code>true</code> to create a sub-node even if the\r
+     *        array property range is of size 1\r
+     * @return child nodes of the specified property\r
+     */\r
+    public static <T extends IProperty> NodeContext[] getChildren(IProperty property, boolean createOneChild) {\r
+        // Plain properties do not have children.\r
+        if (!(property instanceof IArrayProperty))\r
+            return NodeContext.NONE;\r
+\r
+        IArrayProperty array = (IArrayProperty) property;\r
+        ValueRange range = array.getRange();\r
+        if (range.size() < 1 || (!createOneChild && range.size() < 2))\r
+            return NodeContext.NONE;\r
+        Collection<IArrayProperty> children = subnodify(array, range.start(), range.size());\r
+        return NodeContextUtil.toContextsWithInput(children);\r
+    }\r
+\r
+    /**\r
+     * @param property\r
+     * @return {@link Boolean#TRUE} if the property is an {@link IArrayProperty}\r
+     *         and its value range size is > 1.\r
+     */\r
+    public static Boolean hasChildren(IProperty property) {\r
+        if (!(property instanceof IArrayProperty))\r
+            return Boolean.FALSE;\r
+\r
+        IArrayProperty array = (IArrayProperty) property;\r
+        ValueRange range = array.getRange();\r
+        return Boolean.valueOf(range.size() < 2);\r
+    }\r
+\r
+    /**\r
+     * @param prop\r
+     * @param rangeStart\r
+     * @param rangeSize\r
+     * @return\r
+     */\r
+    public static <T extends Sliceable<T>> Collection<T> subnodify(Sliceable<T> prop, int rangeStart, int rangeSize) {\r
+        return SliceUtil.subnodify(prop, rangeStart, rangeSize);\r
+    }\r
+\r
+    /**\r
+     * @param prop\r
+     * @param rangeStart\r
+     * @param rangeSize\r
+     * @param pow10OfItemsPerLevel\r
+     * @return\r
+     */\r
+    public static <T> Collection<T> subnodify(Object p, int rangeStart, int rangeSize, int pow10OfItemsPerLevel) {\r
+        return SliceUtil.subnodify(p, rangeStart, rangeSize, pow10OfItemsPerLevel);\r
+    }\r
+\r
+}\r