]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/property/MethodComboValueProvider.java
Reflection-based handling of Enum property editing.
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / property / MethodComboValueProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * 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.g3d.property;
13
14 import java.lang.reflect.Method;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.simantics.utils.datastructures.Arrays;
19
20 public class MethodComboValueProvider implements ComboValueProvider {
21
22         Method getter; 
23         Method setter;
24         Method values;
25         
26         public MethodComboValueProvider(Method getter, Method setter, Method values) {
27                 this.getter = getter;
28                 this.setter = setter;
29                 this.values = values;
30         }
31         
32         @Override
33         public Object getValue(Object obj) throws Exception{
34                 if (getValueType().isEnum()) {
35                         Object value = getter.invoke(obj);
36                         Object[] vs = getValueType().getEnumConstants();
37                         return (Integer) Arrays.indexOf(vs, value);
38                 }
39                 else {
40                         return getter.invoke(obj);
41                 }
42         }
43         @Override
44         public void setValue(Object obj, Object value) throws Exception {
45                 setter.invoke(obj,value);
46         }
47         
48         @SuppressWarnings("unchecked")
49         public List<Object> getValues(Object obj) throws Exception {
50                 if (values != null) {
51                         return (List<Object>)values.invoke(obj);
52                 }
53                 else if (getValueType().isEnum()) {
54                         Object[] enumConstants = getValueType().getEnumConstants();
55                         List<Object> result = new ArrayList<>(enumConstants.length);
56                         for (Object c : enumConstants)
57                                 result.add(c);
58                         return result;
59                 }
60                 else {
61                         throw new IllegalStateException("No value list defined for " + getter.toString());
62                 }
63         }
64
65         @Override
66         public Class<?> getValueType() {
67                 return getter.getReturnType();
68         }
69 }