package org.simantics.g3d.property; import java.util.List; import org.simantics.utils.datastructures.Arrays; public class ComboPropertyManipulator implements PropertyManipulator{ ComboValueProvider provider; Object input; boolean editMode; public ComboPropertyManipulator(ValueProvider provider, Object input) { this.provider = (ComboValueProvider)provider; this.input = input; } @Override public int getValueCount() { return 1; } @Override public String getDescription(int i) { if (i == 0) return "Value"; return null; } @Override public String getValue(int i) { try { Object rawValue = provider.getValue(input); if (rawValue == null) { return null; } else if (rawValue instanceof Integer) { Integer value = (Integer)rawValue; return getItems()[value]; } else if (rawValue instanceof Enum) { return rawValue.toString(); } else { return null; } } catch (Exception e) { return null; } } @Override public String setValue(String value, int i) { try { int intValue = Integer.parseInt(value); Class valueType = provider.getValueType(); if (Enum.class.isAssignableFrom(valueType)) { provider.setValue(input, valueType.getEnumConstants()[intValue]); } else { provider.setValue(input, intValue); } } catch (Exception e) { return e.getMessage(); } return null; } @Override public boolean getEditMode() { return editMode; } @Override public void setEditMode(boolean b) { editMode = b; } public String[] getItems() { try { List vals = provider.getValues(input); String arr[] = new String[vals.size()]; for (int i = 0; i < vals.size(); i++) { arr[i] = vals.get(i).toString(); } return arr; } catch (Exception e) { return new String[0]; } } public Integer getValueIndex() { try { Object value = provider.getValue(input); Class valueType = provider.getValueType(); if (valueType.isEnum()) { return Arrays.indexOf(valueType.getEnumConstants(), value); } else { return (Integer) value; } } catch (Exception e) { return null; } } public Integer indexOf(String value) { String[] items = getItems(); for (int i = 0; i < items.length; i++) if (items[i].equals(value)) return i; return -1; } }