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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.common.property;
\r
14 import java.util.Collection;
\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
23 * @author Tuukka Lehtonen
\r
25 public class PropertyUtil {
\r
28 * Use databoard instead of this?
\r
30 public static enum ValueType {
\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
60 public static String toString(ValueType 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
72 throw new IllegalArgumentException("unrecognized value type: "+ type);
\r
79 * Returns the child nodes of the specified IProperty, constructed with the
\r
80 * specified IPropertyFactory.
\r
83 * Child nodes only exist for {@link IArrayProperty} instances whose value
\r
84 * range size is > 1.
\r
89 * @return child nodes of the specified property
\r
91 public static <T extends IProperty> NodeContext[] getChildren(IProperty property) {
\r
92 return getChildren(property, false);
\r
96 * Returns the child nodes of the specified IProperty, constructed with the
\r
97 * specified IPropertyFactory.
\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
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
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
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
126 * @return {@link Boolean#TRUE} if the property is an {@link IArrayProperty}
\r
127 * and its value range size is > 1.
\r
129 public static Boolean hasChildren(IProperty property) {
\r
130 if (!(property instanceof IArrayProperty))
\r
131 return Boolean.FALSE;
\r
133 IArrayProperty array = (IArrayProperty) property;
\r
134 ValueRange range = array.getRange();
\r
135 return Boolean.valueOf(range.size() < 2);
\r
140 * @param rangeStart
\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
150 * @param rangeStart
\r
152 * @param pow10OfItemsPerLevel
\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