comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/DoubleRangePanel.java @ 9270:7337034eb5d5

multiple whitespace input fix
author gernotbelger
date Thu, 19 Jul 2018 13:36:38 +0200
parents 316a9eeb0836
children
comparison
equal deleted inserted replaced
9269:83ebeb620b5a 9270:7337034eb5d5
6 * documentation coming with Dive4Elements River for details. 6 * documentation coming with Dive4Elements River for details.
7 */ 7 */
8 8
9 package org.dive4elements.river.client.client.ui; 9 package org.dive4elements.river.client.client.ui;
10 10
11 import java.util.Map;
12
13 import org.dive4elements.river.client.client.FLYSConstants;
14
11 import com.google.gwt.core.client.GWT; 15 import com.google.gwt.core.client.GWT;
12 import com.google.gwt.i18n.client.NumberFormat; 16 import com.google.gwt.i18n.client.NumberFormat;
13
14 import com.smartgwt.client.types.Alignment; 17 import com.smartgwt.client.types.Alignment;
15 import com.smartgwt.client.widgets.form.DynamicForm; 18 import com.smartgwt.client.widgets.form.DynamicForm;
16 import com.smartgwt.client.widgets.form.fields.FloatItem; 19 import com.smartgwt.client.widgets.form.fields.FloatItem;
17 import com.smartgwt.client.widgets.form.fields.FormItem; 20 import com.smartgwt.client.widgets.form.fields.FormItem;
18 import com.smartgwt.client.widgets.form.fields.StaticTextItem; 21 import com.smartgwt.client.widgets.form.fields.StaticTextItem;
19 import com.smartgwt.client.widgets.form.fields.events.BlurHandler; 22 import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
20 23
21 import org.dive4elements.river.client.client.FLYSConstants;
22
23 import java.util.Map;
24
25
26 /** 24 /**
27 * This class creates a DynamicForm with three input fields. 25 * This class creates a DynamicForm with three input fields.
28 * 26 *
29 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 27 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
30 */ 28 */
31 public class DoubleRangePanel 29 public class DoubleRangePanel extends DynamicForm {
32 extends DynamicForm 30 /** The message class that provides i18n strings. */
33 {
34 /** The message class that provides i18n strings.*/
35 protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); 31 protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);
36 32
37 33 /** The constant name of the input field to enter the start of a distance. */
38 /** The constant name of the input field to enter the start of a distance.*/
39 public static final String FIELD_FROM = "from"; 34 public static final String FIELD_FROM = "from";
40 35
41 /** The constant name of the input field to enter the end of a distance.*/ 36 /** The constant name of the input field to enter the end of a distance. */
42 public static final String FIELD_TO = "to"; 37 public static final String FIELD_TO = "to";
43 38
44 /** The constant name of the input field to enter the step width of a 39 /**
45 * distance.*/ 40 * The constant name of the input field to enter the step width of a
41 * distance.
42 */
46 public static final String FIELD_WIDTH = "step"; 43 public static final String FIELD_WIDTH = "step";
47 44
48 /** The textboxes */ 45 /** The textboxes */
49 protected FloatItem fromItem; 46 protected FloatItem fromItem;
50 protected FloatItem toItem; 47 protected FloatItem toItem;
51 protected FloatItem stepItem; 48 protected FloatItem stepItem;
52 49
53
54 public DoubleRangePanel() { 50 public DoubleRangePanel() {
55 } 51 }
56 52
57 public FloatItem getToItem() { 53 public FloatItem getToItem() {
58 return toItem; 54 return this.toItem;
59 } 55 }
60
61 56
62 /** 57 /**
63 * Creates a new form with a single input field that displays an array of 58 * Creates a new form with a single input field that displays an array of
64 * double values. 59 * double values.
65 * 60 *
66 * @param name The name of the TextItem. 61 * @param name
67 * @param title The title of the TextItem. 62 * The name of the TextItem.
68 * @param values The double values that should be displayed initially. 63 * @param title
69 * @param handler The BlurHandler that is used to valide the input. 64 * The title of the TextItem.
70 */ 65 * @param values
71 public DoubleRangePanel( 66 * The double values that should be displayed initially.
72 String titleFrom, String titleTo, String titleStep, 67 * @param handler
73 double from, double to, double step, 68 * The BlurHandler that is used to valide the input.
74 int width, 69 */
75 BlurHandler handler) 70 public DoubleRangePanel(final String titleFrom, final String titleTo, final String titleStep, final double from, final double to, final double step,
76 { 71 final int width, final BlurHandler handler) {
77 this( 72 this(titleFrom, titleTo, titleStep, from, to, step, width, handler, "right");
78 titleFrom, titleTo, titleStep, 73 }
79 from, to, step, 74
80 width, 75 public DoubleRangePanel(final String titleFrom, final String titleTo, final String titleStep, final double from, final double to, final double step,
81 handler, 76 final int width, final BlurHandler handler, final String labelOrientation) {
82 "right"); 77 this.fromItem = new FloatItem(FIELD_FROM);
83 } 78 this.toItem = new FloatItem(FIELD_TO);
84 79 this.stepItem = new FloatItem(FIELD_WIDTH);
85 80
86 public DoubleRangePanel( 81 this.fromItem.addBlurHandler(handler);
87 String titleFrom, String titleTo, String titleStep, 82 this.toItem.addBlurHandler(handler);
88 double from, double to, double step, 83 this.stepItem.addBlurHandler(handler);
89 int width, 84
90 BlurHandler handler, 85 final NumberFormat f = NumberFormat.getDecimalFormat();
91 String labelOrientation) 86
92 { 87 this.fromItem.setValue(f.format(from));
93 fromItem = new FloatItem(FIELD_FROM); 88 this.toItem.setValue(f.format(to));
94 toItem = new FloatItem(FIELD_TO); 89 this.stepItem.setValue(f.format(step));
95 stepItem = new FloatItem(FIELD_WIDTH); 90
96 91 final StaticTextItem fromText = new StaticTextItem("staticFrom");
97 fromItem.addBlurHandler(handler);
98 toItem.addBlurHandler(handler);
99 stepItem.addBlurHandler(handler);
100
101 NumberFormat f = NumberFormat.getDecimalFormat();
102
103 fromItem.setValue(f.format(from));
104 toItem.setValue(f.format(to));
105 stepItem.setValue(f.format(step));
106
107 StaticTextItem fromText = new StaticTextItem("staticFrom");
108 fromText.setValue(titleFrom); 92 fromText.setValue(titleFrom);
109 fromText.setShowTitle(false); 93 fromText.setShowTitle(false);
110 fromItem.setShowTitle(false); 94 this.fromItem.setShowTitle(false);
111 95
112 StaticTextItem toText = new StaticTextItem("staticTo"); 96 final StaticTextItem toText = new StaticTextItem("staticTo");
113 toText.setValue(titleTo); 97 toText.setValue(titleTo);
114 toText.setShowTitle(false); 98 toText.setShowTitle(false);
115 toItem.setShowTitle(false); 99 this.toItem.setShowTitle(false);
116 100
117 StaticTextItem stepText = new StaticTextItem("staticStep"); 101 final StaticTextItem stepText = new StaticTextItem("staticStep");
118 stepText.setValue(titleStep); 102 stepText.setValue(titleStep);
119 stepText.setShowTitle(false); 103 stepText.setShowTitle(false);
120 stepItem.setShowTitle(false); 104 this.stepItem.setShowTitle(false);
121 105
122 int itemWidth = width / 6; 106 final int itemWidth = width / 6;
123 fromItem.setWidth(itemWidth); 107 this.fromItem.setWidth(itemWidth);
124 fromText.setWidth(itemWidth); 108 fromText.setWidth(itemWidth);
125 toItem.setWidth(itemWidth); 109 this.toItem.setWidth(itemWidth);
126 toText.setWidth(itemWidth); 110 toText.setWidth(itemWidth);
127 stepItem.setWidth(itemWidth); 111 this.stepItem.setWidth(itemWidth);
128 stepText.setWidth(itemWidth); 112 stepText.setWidth(itemWidth);
129 113
130 if (labelOrientation.equals("right")) { 114 if (labelOrientation.equals("right")) {
131 setFields(fromItem, fromText, toItem, toText, stepItem, stepText); 115 setFields(this.fromItem, fromText, this.toItem, toText, this.stepItem, stepText);
132 } 116 } else {
133 else { 117 setFields(fromText, this.fromItem, toText, this.toItem, stepText, this.stepItem);
134 setFields(fromText, fromItem, toText, toItem, stepText, stepItem);
135 } 118 }
136 119
137 setFixedColWidths(false); 120 setFixedColWidths(false);
138 setNumCols(6); 121 setNumCols(6);
139 setWidth(width); 122 setWidth(width);
140 setAlign(Alignment.CENTER); 123 setAlign(Alignment.CENTER);
141 } 124 }
142 125
143
144 /** 126 /**
145 * This method takes distances values and sets them to the textboxes 127 * This method takes distances values and sets them to the textboxes
146 * visualizied by this widget. 128 * visualizied by this widget.
147 * 129 *
148 * @param from The from value. 130 * @param from
149 * @param to The to value. 131 * The from value.
150 * @param steps The max steps. 132 * @param to
151 */ 133 * The to value.
152 public void setValues(double from, double to, double steps) { 134 * @param steps
153 NumberFormat f = NumberFormat.getDecimalFormat(); 135 * The max steps.
154 136 */
155 fromItem.setValue(f.format(from)); 137 public void setValues(final double from, final double to, final double steps) {
156 toItem.setValue(f.format(to)); 138 final NumberFormat f = NumberFormat.getDecimalFormat();
157 stepItem.setValue(f.format(steps)); 139
140 this.fromItem.setValue(f.format(from));
141 this.toItem.setValue(f.format(to));
142 this.stepItem.setValue(f.format(steps));
158 } 143 }
159 144
160 public boolean validateForm() { 145 public boolean validateForm() {
161 try { 146 try {
162 return 147 return validateForm(this.fromItem) && validateForm(this.toItem) && validateForm(this.stepItem);
163 validateForm(fromItem) && 148 }
164 validateForm(toItem) && 149 catch (final NumberFormatException nfe) {
165 validateForm(stepItem);
166 }
167 catch (NumberFormatException nfe) {
168 return false; 150 return false;
169 } 151 }
170 } 152 }
171 153
172 /** 154 /**
175 * displayed. 157 * displayed.
176 * 158 *
177 * Also if negativeToAllowed is false, an error is registered if 159 * Also if negativeToAllowed is false, an error is registered if
178 * the 'to' field contains a negative value. 160 * the 'to' field contains a negative value.
179 * 161 *
180 * @param item The FormItem. 162 * @param item
163 * The FormItem.
181 */ 164 */
182 @SuppressWarnings("unchecked") 165 @SuppressWarnings("unchecked")
183 protected boolean validateForm(FormItem item) { 166 protected boolean validateForm(final FormItem item) {
184 if (item instanceof StaticTextItem) { 167 if (item instanceof StaticTextItem) {
185 return true; 168 return true;
186 } 169 }
187 170
188 boolean valid = true; 171 boolean valid = true;
189 172
190 String v = (String) item.getValue(); 173 final String v = (String) item.getValue();
191 174
192 NumberFormat f = NumberFormat.getDecimalFormat(); 175 final NumberFormat f = NumberFormat.getDecimalFormat();
193 @SuppressWarnings("rawtypes") 176 @SuppressWarnings("rawtypes")
194 Map errors = getErrors(); 177 final Map errors = getErrors();
195 178
196 try { 179 try {
197 if (v == null) { 180 if (v == null) {
198 throw new NumberFormatException("empty"); 181 throw new NumberFormatException("empty");
199 } 182 }
200 183
201 f.parse(v); 184 f.parse(v);
202 185
203 errors.remove(item.getFieldName()); 186 errors.remove(item.getFieldName());
204 } 187 }
205 catch (NumberFormatException nfe) { 188 catch (final NumberFormatException nfe) {
206 errors.put(item.getFieldName(), MESSAGES.wrongFormat()); 189 errors.put(item.getFieldName(), this.MESSAGES.wrongFormat());
207 190
208 item.focusInItem(); 191 item.focusInItem();
209 192
210 valid = false; 193 valid = false;
211 } 194 }
213 setErrors(errors, true); 196 setErrors(errors, true);
214 197
215 return valid; 198 return valid;
216 } 199 }
217 200
218
219 /** 201 /**
220 * Returns the double value of <i>value</i>. 202 * Returns the double value of <i>value</i>.
221 * 203 *
222 * @return the double value of <i>value</i>. 204 * @return the double value of <i>value</i>.
223 */ 205 */
224 protected double getDouble(String value) { 206 protected double getDouble(final String value) {
225 NumberFormat f = NumberFormat.getDecimalFormat(); 207 final NumberFormat f = NumberFormat.getDecimalFormat();
226 208
227 String[] splitted = value.split(" "); 209 final String[] splitted = StringArrayParseHelper.getArrayFromRawString(value); // Nullpointer?
228 210
229 return f.parse(splitted[0]); 211 return f.parse(splitted[0]);
230 } 212 }
231 213
232
233 /** 214 /**
234 * Returns the start value. 215 * Returns the start value.
235 * 216 *
236 * @return the start value. 217 * @return the start value.
237 */ 218 */
238 public double getFrom() throws NullPointerException { 219 public double getFrom() throws NullPointerException {
239 String v = getValueAsString(FIELD_FROM); 220 final String v = getValueAsString(FIELD_FROM);
240 221
241 return getDouble(v); 222 return getDouble(v);
242 } 223 }
243 224
244
245 /** 225 /**
246 * Returns the end value. 226 * Returns the end value.
247 * 227 *
248 * @return the end value. 228 * @return the end value.
249 */ 229 */
250 public double getTo() throws NullPointerException { 230 public double getTo() throws NullPointerException {
251 String v = getValueAsString(FIELD_TO); 231 final String v = getValueAsString(FIELD_TO);
252 232
253 return getDouble(v); 233 return getDouble(v);
254 } 234 }
255 235
256
257 /** 236 /**
258 * Returns the step width. 237 * Returns the step width.
259 * 238 *
260 * @return the step width. 239 * @return the step width.
261 */ 240 */
262 public double getStep() throws NullPointerException { 241 public double getStep() throws NullPointerException {
263 String v = getValueAsString(FIELD_WIDTH); 242 final String v = getValueAsString(FIELD_WIDTH);
264 243
265 return getDouble(v); 244 return getDouble(v);
266 } 245 }
267 246
268
269 /** 247 /**
270 * Sets the value of the field with name <i>fieldname</i>. 248 * Sets the value of the field with name <i>fieldname</i>.
271 * 249 *
272 * @param value The new value. 250 * @param value
273 * @param fieldname The name of the field. 251 * The new value.
274 */ 252 * @param fieldname
275 public void setDoubleValue(double value, String fieldname) { 253 * The name of the field.
276 NumberFormat f = NumberFormat.getDecimalFormat(); 254 */
255 public void setDoubleValue(final double value, final String fieldname) {
256 final NumberFormat f = NumberFormat.getDecimalFormat();
277 setValue(fieldname, f.format(value)); 257 setValue(fieldname, f.format(value));
278 } 258 }
279 259
280
281 /** 260 /**
282 * Sets a new start value. 261 * Sets a new start value.
283 * 262 *
284 * @param value The new start value. 263 * @param value
285 */ 264 * The new start value.
286 public void setFrom(double value) { 265 */
266 public void setFrom(final double value) {
287 setDoubleValue(value, FIELD_FROM); 267 setDoubleValue(value, FIELD_FROM);
288 } 268 }
289 269
290
291 /** 270 /**
292 * Sets a new end value. 271 * Sets a new end value.
293 * 272 *
294 * @param value The new end value. 273 * @param value
295 */ 274 * The new end value.
296 public void setTo(double value) { 275 */
276 public void setTo(final double value) {
297 setDoubleValue(value, FIELD_TO); 277 setDoubleValue(value, FIELD_TO);
298 } 278 }
299 279
300
301 /** 280 /**
302 * Sets a new step width. 281 * Sets a new step width.
303 * 282 *
304 * @param value The new step width. 283 * @param value
305 */ 284 * The new step width.
306 public void setStep(double value) { 285 */
286 public void setStep(final double value) {
307 setDoubleValue(value, FIELD_WIDTH); 287 setDoubleValue(value, FIELD_WIDTH);
308 } 288 }
309 } 289 }
310 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 290 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org