]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/property/DoubleArrayPropertyManipulator2.java
Allow repeated single clicks to activate property editing
[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 == null)
55                         return null;
56                     if (val.length == i)
57                         return "New";
58             if (val.length < i)
59                 return null;
60             return Double.toString(val[i]);
61                 } catch (Exception e) {
62                         return null;
63                 }
64         }
65         
66         @Override
67         public String setValue(String value, int i) {
68                 try {
69                         double[] val = editValue;
70                         if (value.length() == 0 && i == val.length -1) {
71                             double[] newVal = new double[val.length-1];
72                 System.arraycopy(val, 0, newVal, 0, val.length-1);
73                 val = newVal;
74                         } else if (i < val.length)
75                             val[i] = Double.parseDouble(value);
76                         else if (i == val.length) {
77                             double[] newVal = new double[val.length+1];
78                             System.arraycopy(val, 0, newVal, 0, val.length);
79                             val = newVal;
80                         }
81                         provider.setValue(input, val);
82                 } catch (Exception e) {
83                         return e.getMessage();
84                 }
85                 return null;
86         }
87
88         @Override
89         public boolean getEditMode() {
90                 return editMode;
91         }
92         
93         @Override
94         public void setEditMode(boolean b) {
95                 editMode = b;
96                 if (editMode) {
97                         try {
98                                 editValue = getValue();
99                         } catch (Exception e) {
100                                 
101                         }
102                 }
103                 
104         }
105 }