/******************************************************************************* * 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 org.simantics.g3d.math.MathTools; public class DoubleArrayPropertyManipulator2 implements PropertyManipulator { ValueProvider provider; Object input; boolean editMode; double[] editValue = null; public DoubleArrayPropertyManipulator2(ValueProvider provider, Object input) { this.provider = provider; this.input = input; } @Override public int getValueCount() { return getValue().length+1; } @Override public String getDescription(int i) { return "Value " + i; } private double[] getValue() { try { return (double[])provider.getValue(input); } catch (Exception e) { return new double[0]; } } @Override public String getValue(int i) { if (editMode) { if (i < editValue.length) return Double.toString(editValue[i]); return ""; } try { double[] val = getValue(); if (val == null) return null; if (val.length == i) return "New"; if (val.length < i) return null; return Double.toString(MathTools.round(val[i], 10)); } catch (Exception e) { return null; } } @Override public String setValue(String value, int i) { try { double[] val = editValue; if (value.length() == 0 && i == val.length -1) { double[] newVal = new double[val.length-1]; System.arraycopy(val, 0, newVal, 0, val.length-1); val = newVal; } else if (i < val.length) val[i] = Double.parseDouble(value); else if (i == val.length) { double[] newVal = new double[val.length+1]; System.arraycopy(val, 0, newVal, 0, val.length); val = newVal; } provider.setValue(input, val); } catch (Exception e) { return e.getMessage(); } return null; } @Override public boolean getEditMode() { return editMode; } @Override public void setEditMode(boolean b) { editMode = b; if (editMode) { try { editValue = getValue(); } catch (Exception e) { } } } }