/******************************************************************************* * Copyright (c) 2012, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.g3d.property; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import org.simantics.utils.datastructures.Arrays; public class MethodComboValueProvider implements ComboValueProvider { Method getter; Method setter; Method values; public MethodComboValueProvider(Method getter, Method setter, Method values) { this.getter = getter; this.setter = setter; this.values = values; } @Override public Object getValue(Object obj) throws Exception{ if (getValueType().isEnum()) { Object value = getter.invoke(obj); Object[] vs = getValueType().getEnumConstants(); return (Integer) Arrays.indexOf(vs, value); } else { return getter.invoke(obj); } } @Override public void setValue(Object obj, Object value) throws Exception { setter.invoke(obj,value); } @SuppressWarnings("unchecked") public List getValues(Object obj) throws Exception { if (values != null) { return (List)values.invoke(obj); } else if (getValueType().isEnum()) { Object[] enumConstants = getValueType().getEnumConstants(); List result = new ArrayList<>(enumConstants.length); for (Object c : enumConstants) result.add(c); return result; } else { throw new IllegalStateException("No value list defined for " + getter.toString()); } } @Override public Class getValueType() { return getter.getReturnType(); } }