]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/property/PropertyUtil.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / property / PropertyUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.common.property;
13
14 import java.util.Collection;
15
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;
21
22 /**
23  * @author Tuukka Lehtonen
24  */
25 public class PropertyUtil {
26
27     /**
28      * Use databoard instead of this?
29      */
30     public static enum ValueType {
31         NoValue,
32         BooleanValue,
33         ByteValue,
34         IntegerValue,
35         LongValue,
36         FloatValue,
37         DoubleValue,
38         StringValue,
39         UnrecognizedValue;
40
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;
58         }
59
60         public static String toString(ValueType type) {
61             switch (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";
71                 default:
72                     throw new IllegalArgumentException("unrecognized value type: "+ type);
73             }
74         }
75     }
76
77
78     /**
79      * Returns the child nodes of the specified IProperty, constructed with the
80      * specified IPropertyFactory.
81      * 
82      * <p>
83      * Child nodes only exist for {@link IArrayProperty} instances whose value
84      * range size is > 1.
85      * </p>
86      * 
87      * @param <T>
88      * @param property
89      * @return child nodes of the specified property
90      */
91     public static <T extends IProperty> NodeContext[] getChildren(IProperty property) {
92         return getChildren(property, false);
93     }
94
95     /**
96      * Returns the child nodes of the specified IProperty, constructed with the
97      * specified IPropertyFactory.
98      * 
99      * <p>
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.
103      * </p>
104      * 
105      * @param <T>
106      * @param property
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
110      */
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;
115
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);
122     }
123
124     /**
125      * @param property
126      * @return {@link Boolean#TRUE} if the property is an {@link IArrayProperty}
127      *         and its value range size is > 1.
128      */
129     public static Boolean hasChildren(IProperty property) {
130         if (!(property instanceof IArrayProperty))
131             return Boolean.FALSE;
132
133         IArrayProperty array = (IArrayProperty) property;
134         ValueRange range = array.getRange();
135         return Boolean.valueOf(range.size() < 2);
136     }
137
138     /**
139      * @param prop
140      * @param rangeStart
141      * @param rangeSize
142      * @return
143      */
144     public static <T extends Sliceable<T>> Collection<T> subnodify(Sliceable<T> prop, int rangeStart, int rangeSize) {
145         return SliceUtil.subnodify(prop, rangeStart, rangeSize);
146     }
147
148     /**
149      * @param prop
150      * @param rangeStart
151      * @param rangeSize
152      * @param pow10OfItemsPerLevel
153      * @return
154      */
155     public static <T> Collection<T> subnodify(Object p, int rangeStart, int rangeSize, int pow10OfItemsPerLevel) {
156         return SliceUtil.subnodify(p, rangeStart, rangeSize, pow10OfItemsPerLevel);
157     }
158
159 }