comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/DoubleArrayPanel.java @ 42:ba7df4a24ae0

Added a new widget to enter w/Q values in single and range mode. flys-client/trunk@1483 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 16 Mar 2011 15:52:13 +0000
parents
children a3d235c63195
comparison
equal deleted inserted replaced
41:87a44f8e25cc 42:ba7df4a24ae0
1 package de.intevation.flys.client.client.ui;
2
3 import java.util.Map;
4
5 import com.google.gwt.core.client.GWT;
6 import com.google.gwt.i18n.client.NumberFormat;
7
8 import com.smartgwt.client.types.TitleOrientation;
9 import com.smartgwt.client.widgets.form.DynamicForm;
10 import com.smartgwt.client.widgets.form.fields.FormItem;
11 import com.smartgwt.client.widgets.form.fields.StaticTextItem;
12 import com.smartgwt.client.widgets.form.fields.TextItem;
13 import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
14
15 import de.intevation.flys.client.client.FLYSMessages;
16
17 public class DoubleArrayPanel
18 extends DynamicForm
19 {
20 /** The message class that provides i18n strings.*/
21 protected FLYSMessages MESSAGES = GWT.create(FLYSMessages.class);
22
23
24 /** The constant input field name.*/
25 public static final String FIELD_NAME = "doublearray";
26
27
28 /**
29 * Creates a new form with a single input field that displays an array of
30 * double values.
31 *
32 * @param name The name of the TextItem.
33 * @param title The title of the TextItem.
34 * @param values The double values that should be displayed initially.
35 * @param handler The BlurHandler that is used to valide the input.
36 */
37 public DoubleArrayPanel(
38 String title,
39 double[] values,
40 BlurHandler handler)
41 {
42 TextItem ti = new TextItem(FIELD_NAME);
43 StaticTextItem sti = new StaticTextItem("staticarray");
44
45 ti.setShowTitle(false);
46 sti.setShowTitle(false);
47 sti.setValue(title);
48
49 ti.addBlurHandler(handler);
50
51 setFields(ti, sti);
52 setTitleOrientation(TitleOrientation.RIGHT);
53 setNumCols(2);
54
55 if (values == null) {
56 return;
57 }
58
59 NumberFormat f = NumberFormat.getDecimalFormat();
60
61 StringBuilder text = new StringBuilder();
62 boolean firstItem = true;
63
64 for (double val: values) {
65 if (!firstItem) {
66 text.append(" ");
67 }
68
69 text.append(f.format(val));
70
71 firstItem = false;
72 }
73
74 ti.setValue(text.toString());
75 }
76
77
78 /**
79 * This method validates the entered text in the location input field. If
80 * there are values that doesn't represent a valid location, an error is
81 * displayed.
82 *
83 * @param item The FormItem.
84 */
85 protected boolean validateForm(FormItem item) {
86 boolean valid = true;
87 String value = (String) item.getValue();
88
89 if (value == null) {
90 return valid;
91 }
92
93 String[] parts = value.split(" ");
94
95 if (parts == null) {
96 return valid;
97 }
98
99 NumberFormat f = NumberFormat.getDecimalFormat();
100 Map errors = getErrors();
101
102 try {
103 for (String part: parts) {
104 double location = f.parse(part);
105 }
106
107 errors.remove(item.getFieldName());
108 }
109 catch (NumberFormatException nfe) {
110 errors.put(item.getFieldName(), MESSAGES.wrongFormat());
111
112 valid = false;
113 }
114
115 setErrors(errors, true);
116
117 return valid;
118 }
119
120
121 /**
122 * This method returns the double array that has been entered in
123 * <i>item</i>.
124 *
125 * @param item The item that contains the desired values.
126 *
127 * @return the values as double array.
128 */
129 public double[] getInputValues(FormItem item) {
130 String value = (String) item.getValue();
131
132 if (value == null) {
133 return null;
134 }
135
136 String[] parts = value.split(" ");
137
138 if (parts == null) {
139 return null;
140 }
141
142 double[] values = new double[parts.length];
143
144 NumberFormat f = NumberFormat.getDecimalFormat();
145
146 int i = 0;
147 for (String part: parts) {
148 try {
149 values[i++] = f.parse(part);
150 }
151 catch (NumberFormatException nfe) {
152 // do nothing
153 }
154 }
155
156 return values;
157 }
158 }
159 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org