/******************************************************************************* * 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 javax.vecmath.Vector3d; import org.simantics.g3d.math.MathTools; public class VectorPropertyManipulator implements PropertyManipulator { ValueProvider provider; protected Object input; boolean editMode; Vector3d editValue = null; public VectorPropertyManipulator(ValueProvider provider, Object input) { this.provider = provider; this.input = input; } @Override public int getValueCount() { return 3; } @Override public String getDescription(int i) { if (i == 0) return "X"; if (i == 1) return "Y"; if (i == 2) return "Z"; return null; } @Override public String getValue(int i) { try { Vector3d v = _getValue(); if (v == null) return null; if (i == 0) return Double.toString(MathTools.round(v.x, 10)); if (i == 1) return Double.toString(MathTools.round(v.y, 10)); if (i == 2) return Double.toString(MathTools.round(v.z, 10)); return null; } catch (Exception e) { return null; } } @Override public String setValue(String value, int i) { try { Double d = Double.parseDouble(value); Vector3d v = _getValue(); v = new Vector3d(v.x, v.y, v.z); if (i == 0) v.x = d; if (i == 1) v.y = d; if (i == 2) v.z = d; editValue = v; setValue(v); } catch (Exception e) { return e.getMessage(); } return null; } protected void setValue(Vector3d v) throws Exception { provider.setValue(input, v); } private Vector3d _getValue() throws Exception{ if (editMode) return editValue; return (Vector3d) provider.getValue(input); } @Override public boolean getEditMode() { return editMode; } @Override public void setEditMode(boolean b) { editMode = b; if (editMode) { try { editValue = (Vector3d) provider.getValue(input); } catch (Exception e) { } } } }