]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/property/VectorPropertyManipulator.java
Double array properties
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / property / VectorPropertyManipulator.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g3d.property;
13
14 import javax.vecmath.Vector3d;
15
16 public class VectorPropertyManipulator implements PropertyManipulator {
17         
18         ValueProvider provider;
19         protected Object input;
20         
21         boolean editMode;
22         Vector3d editValue = null;
23         
24         public VectorPropertyManipulator(ValueProvider provider, Object input) {
25                 this.provider = provider;
26                 this.input = input;
27         }
28         
29         @Override
30         public int getValueCount() {
31                 return 3;
32         }
33         
34         @Override
35         public String getDescription(int i) {
36                 if (i == 0)
37                         return "X";
38                 if (i == 1)
39                         return "Y";
40                 if (i == 2)
41                         return "Z";
42                 return null;
43         }
44         
45         @Override
46         public String getValue(int i) {
47                 try {
48                         Vector3d v = _getValue();
49                         if (v == null)
50                                 return null;
51                         if (i == 0)
52                                 return Double.toString(v.x);
53                         if (i == 1)
54                                 return Double.toString(v.y);
55                         if (i == 2)
56                                 return Double.toString(v.z);
57                         return null;
58                 } catch (Exception e) {
59                         return null;
60                 }
61         }
62         
63         @Override
64         public String setValue(String value, int i) {
65                 try {
66                         Double d = Double.parseDouble(value);
67                         Vector3d v = _getValue();
68                         v = new Vector3d(v.x, v.y, v.z);
69                         if (i == 0)
70                                 v.x = d;
71                         if (i == 1)
72                                 v.y = d;
73                         if (i == 2)
74                                 v.z = d;
75                         editValue = v;
76                         setValue(v);
77                 } catch (Exception e) {
78                         return e.getMessage();
79                 }
80                 return null;
81         }
82         
83         protected void setValue(Vector3d v) throws Exception {
84                 provider.setValue(input, v);
85         }
86         
87         private Vector3d _getValue() throws Exception{
88                 if (editMode)
89                         return editValue;
90                 return (Vector3d) provider.getValue(input);
91         }
92         
93         @Override
94         public boolean getEditMode() {
95                 return editMode;
96         }
97         
98         @Override
99         public void setEditMode(boolean b) {
100                 editMode = b;
101                 if (editMode) {
102                         try {
103                                 editValue = (Vector3d) provider.getValue(input);
104                         } catch (Exception e) {
105                                 
106                         }
107                 }
108                 
109         }
110
111 }