]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator2.java
Double array properties
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / property / DoubleArrayPropertyManipulator2.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 public class DoubleArrayPropertyManipulator2 implements PropertyManipulator {
15         
16         ValueProvider provider;
17         Object input;
18         
19         boolean editMode;
20         double[] editValue = null;
21         
22         public DoubleArrayPropertyManipulator2(ValueProvider provider, Object input) {
23                 this.provider = provider;
24                 this.input = input;
25         }
26         
27         @Override
28     public int getValueCount() {
29         return getValue().length+1;
30     }
31         
32         @Override
33     public String getDescription(int i) {
34         return "Value " + i;
35     }
36         
37         private double[] getValue() {
38         try {
39             return (double[])provider.getValue(input);
40         } catch (Exception e) {
41             return new double[0];
42         }
43     }
44         
45         @Override
46         public String getValue(int i) {
47                 if (editMode) {
48                     if (i < editValue.length)
49                         return Double.toString(editValue[i]);
50                         return "";
51                 }
52                 try {
53                     double[] val = getValue();
54                     if (val.length == i)
55                         return "New";
56             if (val.length < i)
57                 return null;
58             return Double.toString(val[i]);
59                 } catch (Exception e) {
60                         return null;
61                 }
62         }
63         
64         @Override
65         public String setValue(String value, int i) {
66                 try {
67                         double[] val = editValue;
68                         if (value.length() == 0 && i == val.length -1) {
69                             double[] newVal = new double[val.length-1];
70                 System.arraycopy(val, 0, newVal, 0, val.length-1);
71                 val = newVal;
72                         } else if (i < val.length)
73                             val[i] = Double.parseDouble(value);
74                         else if (i == val.length) {
75                             double[] newVal = new double[val.length+1];
76                             System.arraycopy(val, 0, newVal, 0, val.length);
77                             val = newVal;
78                         }
79                         provider.setValue(input, val);
80                 } catch (Exception e) {
81                         return e.getMessage();
82                 }
83                 return null;
84         }
85
86         @Override
87         public boolean getEditMode() {
88                 return editMode;
89         }
90         
91         @Override
92         public void setEditMode(boolean b) {
93                 editMode = b;
94                 if (editMode) {
95                         try {
96                                 editValue = getValue();
97                         } catch (Exception e) {
98                                 
99                         }
100                 }
101                 
102         }
103 }