/******************************************************************************* * 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.util.Arrays; public class DoubleArrayPropertyManipulator implements PropertyManipulator { ValueProvider provider; Object input; boolean editMode; double[] editValue = null; public DoubleArrayPropertyManipulator(ValueProvider provider, Object input) { this.provider = provider; this.input = input; } @Override public int getValueCount() { return 1; } @Override public String getDescription(int i) { return "Value"; } private double[] getValue() { try { return (double[])provider.getValue(input); } catch (Exception e) { return new double[0]; } } @Override public String getValue(int i) { if (editMode) { return Arrays.toString(editValue); } try { double[] val = getValue(); if (val == null) return null; return Arrays.toString(val); } catch (Exception e) { return null; } } @Override public String setValue(String value, int i) { try { value = value.trim(); if (value.startsWith("[") && value.endsWith("]")) { value = value.substring(1,value.length()-1); String vals[] = value.split(","); double val[] = new double[vals.length]; for (int j = 0; j < vals.length; j++) { val[j] = Double.parseDouble(vals[j]); } provider.setValue(input, val); } else { return "Input must be values between [] characters separated with ,"; } } 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) { } } } }