comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/DoubleArrayPanel.java @ 5834:f507086aa94b

Repaired internal references.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:31:32 +0200
parents flys-client/src/main/java/de/intevation/flys/client/client/ui/DoubleArrayPanel.java@6f23afa5323e
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
1 package de.intevation.flys.client.client.ui;
2
3 import com.google.gwt.core.client.GWT;
4 import com.google.gwt.i18n.client.NumberFormat;
5
6 import com.smartgwt.client.types.TitleOrientation;
7 import com.smartgwt.client.widgets.form.DynamicForm;
8 import com.smartgwt.client.widgets.form.fields.FormItem;
9 import com.smartgwt.client.widgets.form.fields.StaticTextItem;
10 import com.smartgwt.client.widgets.form.fields.TextItem;
11 import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
12 import com.smartgwt.client.widgets.form.fields.events.FocusHandler;
13
14 import de.intevation.flys.client.client.FLYSConstants;
15
16 import java.util.Map;
17
18 public class DoubleArrayPanel
19 extends DynamicForm
20 {
21 /** The message class that provides i18n strings. */
22 protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);
23
24 protected TextItem ti;
25
26 private String title;
27
28 /** The constant input field name. */
29 public static final String FIELD_NAME = "doublearray";
30
31
32 public DoubleArrayPanel(
33 String title,
34 double[] values,
35 BlurHandler handler)
36 {
37 this(title, values, handler, null, TitleOrientation.RIGHT);
38 }
39
40
41 /**
42 * Creates a new form with a single input field that displays an array of
43 * double values.
44 *
45 * @param name The name of the TextItem.
46 * @param title The title of the TextItem.
47 * @param values The double values that should be displayed initially.
48 * @param blurHandler The BlurHandler that is used to valide the input.
49 * @param focusHandler The FocueHandler that is used to valide the input.
50 */
51 public DoubleArrayPanel(
52 String title,
53 double[] values,
54 BlurHandler blurHandler,
55 FocusHandler focusHandler,
56 TitleOrientation titleOrientation)
57 {
58 this.title = title;
59 ti = new TextItem(FIELD_NAME);
60 StaticTextItem sti = new StaticTextItem("staticarray");
61
62 ti.setShowTitle(false);
63 sti.setShowTitle(false);
64 sti.setValue(title);
65
66 ti.addBlurHandler(blurHandler);
67 if (focusHandler != null) {
68 ti.addFocusHandler(focusHandler);
69 }
70
71 if (titleOrientation == TitleOrientation.RIGHT) {
72 setFields(ti, sti);
73 }
74 else {
75 setFields(sti, ti);
76 }
77
78 setTitleOrientation(titleOrientation);
79 setNumCols(2);
80
81 if (values == null) {
82 return;
83 }
84
85 NumberFormat f = NumberFormat.getDecimalFormat();
86
87 StringBuilder text = new StringBuilder();
88 boolean firstItem = true;
89
90 for (double val: values) {
91 if (!firstItem) {
92 text.append(" ");
93 }
94
95 text.append(f.format(val));
96
97 firstItem = false;
98 }
99
100 ti.setValue(text.toString());
101 }
102
103
104 /**
105 * This method takes the double array to set the values to the textbox.
106 *
107 * @param values The double values.
108 */
109 public void setValues(double[] values) {
110 NumberFormat f = NumberFormat.getDecimalFormat();
111
112 if(values == null || values.length == 0) {
113 ti.clearValue();
114 return;
115 }
116 StringBuilder text = new StringBuilder();
117 boolean firstItem = true;
118 if (values != null) {
119 for (double val: values) {
120 if (!firstItem) {
121 text.append(" ");
122 }
123
124 text.append(f.format(val));
125
126 firstItem = false;
127 }
128 }
129
130 ti.clearValue();
131 ti.setValue(text.toString());
132 }
133
134
135 /**
136 * This method appends a double value to the current list of values.
137 *
138 * @param value A new value.
139 */
140 public void addValue(double value) {
141 NumberFormat f = NumberFormat.getDecimalFormat();
142
143 String current = ti.getValueAsString();
144
145 if (current == null || current.length() == 0) {
146 current = f.format(value);
147 }
148 else {
149 current += " " + f.format(value);
150 }
151
152 ti.setValue(current);
153 }
154
155
156 protected boolean validateForm() {
157 return validateForm(ti);
158 }
159
160
161 /**
162 * This method validates the entered text in the location input field. If
163 * there are values that doesn't represent a valid location, an error is
164 * displayed.
165 *
166 * @param item The FormItem.
167 */
168 @SuppressWarnings("unchecked")
169 protected boolean validateForm(FormItem item) {
170 if (item instanceof StaticTextItem) {
171 return true;
172 }
173
174 boolean valid = true;
175 String value = (String) item.getValue();
176
177 if (value == null) {
178 return valid;
179 }
180
181 String[] parts = value.split("\\s+");
182
183 if (parts == null) {
184 return valid;
185 }
186
187 NumberFormat nf = NumberFormat.getDecimalFormat();
188 @SuppressWarnings("rawtypes")
189 Map errors = getErrors();
190
191 try {
192 for (String part: parts) {
193
194 if (part.length() == 0) {
195 continue;
196 }
197
198 nf.parse(part);
199 }
200
201 errors.remove(item.getFieldName());
202 }
203 catch (NumberFormatException nfe) {
204 errors.put(item.getFieldName(), MESSAGES.wrongFormat());
205
206 valid = false;
207 }
208
209 setErrors(errors, true);
210
211 return valid;
212 }
213
214
215 /**
216 * This method returns the double array that has been entered in
217 * <i>item</i>.
218 *
219 * @param item The item that contains the desired values.
220 *
221 * @return the values as double array.
222 */
223 public double[] getInputValues(FormItem item) {
224 String value = (String) item.getValue();
225
226 if (value == null) {
227 return null;
228 }
229
230 String[] parts = value.split("\\s+");
231
232 if (parts == null) {
233 return null;
234 }
235
236 NumberFormat f = NumberFormat.getDecimalFormat();
237
238 double[] values = new double[parts.length];
239
240 int i = 0;
241 OUTER: for (String part: parts) {
242 if (part.length() == 0) {
243 continue;
244 }
245
246 try {
247 double x = f.parse(part);
248 for (int j = 0; j < i; ++j) {
249 if (values[j] == x) {
250 continue OUTER;
251 }
252 }
253 values[i++] = x;
254 }
255 catch (NumberFormatException nfe) {
256 // do nothing
257 }
258 }
259
260 double [] out = new double[i];
261 System.arraycopy(values, 0, out, 0, i);
262
263 return out;
264 }
265
266
267 /**
268 * Returns the double values of this panel.
269 *
270 * @return the double values of this panel.
271 */
272 public double[] getInputValues() {
273 return getInputValues(ti);
274 }
275
276 public String getItemTitle() {
277 return this.title;
278 }
279 }
280 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org