1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
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.g3d.property;
14 import java.lang.reflect.Method;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.simantics.utils.datastructures.Arrays;
20 public class MethodComboValueProvider implements ComboValueProvider {
26 public MethodComboValueProvider(Method getter, Method setter, Method values) {
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);
40 return getter.invoke(obj);
44 public void setValue(Object obj, Object value) throws Exception {
45 setter.invoke(obj,value);
48 @SuppressWarnings("unchecked")
49 public List<Object> getValues(Object obj) throws Exception {
51 return (List<Object>)values.invoke(obj);
53 else if (getValueType().isEnum()) {
54 Object[] enumConstants = getValueType().getEnumConstants();
55 List<Object> result = new ArrayList<>(enumConstants.length);
56 for (Object c : enumConstants)
61 throw new IllegalStateException("No value list defined for " + getter.toString());
66 public Class<?> getValueType() {
67 return getter.getReturnType();