]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.common.property;\r
13 \r
14 import java.util.Collection;\r
15 \r
16 import org.simantics.browsing.ui.NodeContext;\r
17 import org.simantics.browsing.ui.common.NodeContextUtil;\r
18 import org.simantics.utils.datastructures.slice.SliceUtil;\r
19 import org.simantics.utils.datastructures.slice.Sliceable;\r
20 import org.simantics.utils.datastructures.slice.ValueRange;\r
21 \r
22 /**\r
23  * @author Tuukka Lehtonen\r
24  */\r
25 public class PropertyUtil {\r
26 \r
27     /**\r
28      * Use databoard instead of this?\r
29      */\r
30     public static enum ValueType {\r
31         NoValue,\r
32         BooleanValue,\r
33         ByteValue,\r
34         IntegerValue,\r
35         LongValue,\r
36         FloatValue,\r
37         DoubleValue,\r
38         StringValue,\r
39         UnrecognizedValue;\r
40 \r
41         public static ValueType convert(Object obj) {\r
42             if (obj == null) return NoValue;\r
43             if (obj.getClass() == Boolean.class) return BooleanValue;\r
44             if (obj.getClass() == boolean[].class) return BooleanValue;\r
45             if (obj.getClass() == Byte.class) return ByteValue;\r
46             if (obj.getClass() == byte[].class) return ByteValue;\r
47             if (obj.getClass() == Integer.class) return IntegerValue;\r
48             if (obj.getClass() == int[].class) return IntegerValue;\r
49             if (obj.getClass() == Long.class) return LongValue;\r
50             if (obj.getClass() == long[].class) return LongValue;\r
51             if (obj.getClass() == Float.class) return FloatValue;\r
52             if (obj.getClass() == float[].class) return FloatValue;\r
53             if (obj.getClass() == Double.class) return DoubleValue;\r
54             if (obj.getClass() == double[].class) return DoubleValue;\r
55             if (obj.getClass() == String.class) return StringValue;\r
56             if (obj.getClass() == String[].class) return StringValue;\r
57             return UnrecognizedValue;\r
58         }\r
59 \r
60         public static String toString(ValueType type) {\r
61             switch (type) {\r
62                 case NoValue: return "no value";\r
63                 case BooleanValue: return "boolean";\r
64                 case ByteValue: return "byte";\r
65                 case DoubleValue: return "double";\r
66                 case FloatValue: return "float";\r
67                 case IntegerValue: return "int32";\r
68                 case LongValue: return "int64";\r
69                 case StringValue: return "string";\r
70                 case UnrecognizedValue: return "unknown value";\r
71                 default:\r
72                     throw new IllegalArgumentException("unrecognized value type: "+ type);\r
73             }\r
74         }\r
75     }\r
76 \r
77 \r
78     /**\r
79      * Returns the child nodes of the specified IProperty, constructed with the\r
80      * specified IPropertyFactory.\r
81      * \r
82      * <p>\r
83      * Child nodes only exist for {@link IArrayProperty} instances whose value\r
84      * range size is > 1.\r
85      * </p>\r
86      * \r
87      * @param <T>\r
88      * @param property\r
89      * @return child nodes of the specified property\r
90      */\r
91     public static <T extends IProperty> NodeContext[] getChildren(IProperty property) {\r
92         return getChildren(property, false);\r
93     }\r
94 \r
95     /**\r
96      * Returns the child nodes of the specified IProperty, constructed with the\r
97      * specified IPropertyFactory.\r
98      * \r
99      * <p>\r
100      * Child nodes only exist for {@link IArrayProperty} instances whose value\r
101      * range size is > 0. You can whether you want to create child nodes for\r
102      * ranges of size 1 or not.\r
103      * </p>\r
104      * \r
105      * @param <T>\r
106      * @param property\r
107      * @param createOneChild <code>true</code> to create a sub-node even if the\r
108      *        array property range is of size 1\r
109      * @return child nodes of the specified property\r
110      */\r
111     public static <T extends IProperty> NodeContext[] getChildren(IProperty property, boolean createOneChild) {\r
112         // Plain properties do not have children.\r
113         if (!(property instanceof IArrayProperty))\r
114             return NodeContext.NONE;\r
115 \r
116         IArrayProperty array = (IArrayProperty) property;\r
117         ValueRange range = array.getRange();\r
118         if (range.size() < 1 || (!createOneChild && range.size() < 2))\r
119             return NodeContext.NONE;\r
120         Collection<IArrayProperty> children = subnodify(array, range.start(), range.size());\r
121         return NodeContextUtil.toContextsWithInput(children);\r
122     }\r
123 \r
124     /**\r
125      * @param property\r
126      * @return {@link Boolean#TRUE} if the property is an {@link IArrayProperty}\r
127      *         and its value range size is > 1.\r
128      */\r
129     public static Boolean hasChildren(IProperty property) {\r
130         if (!(property instanceof IArrayProperty))\r
131             return Boolean.FALSE;\r
132 \r
133         IArrayProperty array = (IArrayProperty) property;\r
134         ValueRange range = array.getRange();\r
135         return Boolean.valueOf(range.size() < 2);\r
136     }\r
137 \r
138     /**\r
139      * @param prop\r
140      * @param rangeStart\r
141      * @param rangeSize\r
142      * @return\r
143      */\r
144     public static <T extends Sliceable<T>> Collection<T> subnodify(Sliceable<T> prop, int rangeStart, int rangeSize) {\r
145         return SliceUtil.subnodify(prop, rangeStart, rangeSize);\r
146     }\r
147 \r
148     /**\r
149      * @param prop\r
150      * @param rangeStart\r
151      * @param rangeSize\r
152      * @param pow10OfItemsPerLevel\r
153      * @return\r
154      */\r
155     public static <T> Collection<T> subnodify(Object p, int rangeStart, int rangeSize, int pow10OfItemsPerLevel) {\r
156         return SliceUtil.subnodify(p, rangeStart, rangeSize, pow10OfItemsPerLevel);\r
157     }\r
158 \r
159 }\r