1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.common.property;
14 import java.util.Collection;
16 import org.simantics.browsing.ui.NodeContext;
17 import org.simantics.browsing.ui.common.NodeContextUtil;
18 import org.simantics.utils.datastructures.slice.SliceUtil;
19 import org.simantics.utils.datastructures.slice.Sliceable;
20 import org.simantics.utils.datastructures.slice.ValueRange;
23 * @author Tuukka Lehtonen
25 public class PropertyUtil {
28 * Use databoard instead of this?
30 public static enum ValueType {
41 public static ValueType convert(Object obj) {
42 if (obj == null) return NoValue;
43 if (obj.getClass() == Boolean.class) return BooleanValue;
44 if (obj.getClass() == boolean[].class) return BooleanValue;
45 if (obj.getClass() == Byte.class) return ByteValue;
46 if (obj.getClass() == byte[].class) return ByteValue;
47 if (obj.getClass() == Integer.class) return IntegerValue;
48 if (obj.getClass() == int[].class) return IntegerValue;
49 if (obj.getClass() == Long.class) return LongValue;
50 if (obj.getClass() == long[].class) return LongValue;
51 if (obj.getClass() == Float.class) return FloatValue;
52 if (obj.getClass() == float[].class) return FloatValue;
53 if (obj.getClass() == Double.class) return DoubleValue;
54 if (obj.getClass() == double[].class) return DoubleValue;
55 if (obj.getClass() == String.class) return StringValue;
56 if (obj.getClass() == String[].class) return StringValue;
57 return UnrecognizedValue;
60 public static String toString(ValueType type) {
62 case NoValue: return "no value";
63 case BooleanValue: return "boolean";
64 case ByteValue: return "byte";
65 case DoubleValue: return "double";
66 case FloatValue: return "float";
67 case IntegerValue: return "int32";
68 case LongValue: return "int64";
69 case StringValue: return "string";
70 case UnrecognizedValue: return "unknown value";
72 throw new IllegalArgumentException("unrecognized value type: "+ type);
79 * Returns the child nodes of the specified IProperty, constructed with the
80 * specified IPropertyFactory.
83 * Child nodes only exist for {@link IArrayProperty} instances whose value
89 * @return child nodes of the specified property
91 public static <T extends IProperty> NodeContext[] getChildren(IProperty property) {
92 return getChildren(property, false);
96 * Returns the child nodes of the specified IProperty, constructed with the
97 * specified IPropertyFactory.
100 * Child nodes only exist for {@link IArrayProperty} instances whose value
101 * range size is > 0. You can whether you want to create child nodes for
102 * ranges of size 1 or not.
107 * @param createOneChild <code>true</code> to create a sub-node even if the
108 * array property range is of size 1
109 * @return child nodes of the specified property
111 public static <T extends IProperty> NodeContext[] getChildren(IProperty property, boolean createOneChild) {
112 // Plain properties do not have children.
113 if (!(property instanceof IArrayProperty))
114 return NodeContext.NONE;
116 IArrayProperty array = (IArrayProperty) property;
117 ValueRange range = array.getRange();
118 if (range.size() < 1 || (!createOneChild && range.size() < 2))
119 return NodeContext.NONE;
120 Collection<IArrayProperty> children = subnodify(array, range.start(), range.size());
121 return NodeContextUtil.toContextsWithInput(children);
126 * @return {@link Boolean#TRUE} if the property is an {@link IArrayProperty}
127 * and its value range size is > 1.
129 public static Boolean hasChildren(IProperty property) {
130 if (!(property instanceof IArrayProperty))
131 return Boolean.FALSE;
133 IArrayProperty array = (IArrayProperty) property;
134 ValueRange range = array.getRange();
135 return Boolean.valueOf(range.size() < 2);
144 public static <T extends Sliceable<T>> Collection<T> subnodify(Sliceable<T> prop, int rangeStart, int rangeSize) {
145 return SliceUtil.subnodify(prop, rangeStart, rangeSize);
152 * @param pow10OfItemsPerLevel
155 public static <T> Collection<T> subnodify(Object p, int rangeStart, int rangeSize, int pow10OfItemsPerLevel) {
156 return SliceUtil.subnodify(p, rangeStart, rangeSize, pow10OfItemsPerLevel);