comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/DoubleRangePanel.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.Alignment;
9 import com.smartgwt.client.widgets.form.DynamicForm;
10 import com.smartgwt.client.widgets.form.fields.FloatItem;
11 import com.smartgwt.client.widgets.form.fields.FormItem;
12 import com.smartgwt.client.widgets.form.fields.StaticTextItem;
13 import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
14
15 import de.intevation.flys.client.client.FLYSMessages;
16
17
18
19 /**
20 * This class creates a DynamicForm with three input fields.
21 *
22 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
23 */
24 public class DoubleRangePanel
25 extends DynamicForm
26 {
27 /** The message class that provides i18n strings.*/
28 protected FLYSMessages MESSAGES = GWT.create(FLYSMessages.class);
29
30
31 /** The constant name of the input field to enter the start of a distance.*/
32 public static final String FIELD_FROM = "from";
33
34 /** The constant name of the input field to enter the end of a distance.*/
35 public static final String FIELD_TO = "to";
36
37 /** The constant name of the input field to enter the step width of a
38 * distance.*/
39 public static final String FIELD_WIDTH = "step";
40
41
42 /**
43 * Creates a new form with a single input field that displays an array of
44 * double values.
45 *
46 * @param name The name of the TextItem.
47 * @param title The title of the TextItem.
48 * @param values The double values that should be displayed initially.
49 * @param handler The BlurHandler that is used to valide the input.
50 */
51 public DoubleRangePanel(
52 String titleFrom, String titleTo, String titleStep,
53 double from, double to, double step,
54 int width,
55 BlurHandler handler)
56 {
57 FloatItem fromItem = new FloatItem(FIELD_FROM);
58 FloatItem toItem = new FloatItem(FIELD_TO);
59 FloatItem stepItem = new FloatItem(FIELD_WIDTH);
60
61 fromItem.addBlurHandler(handler);
62 toItem.addBlurHandler(handler);
63 stepItem.addBlurHandler(handler);
64
65 NumberFormat f = NumberFormat.getDecimalFormat();
66
67 fromItem.setValue(f.format(from));
68 toItem.setValue(f.format(to));
69 stepItem.setValue(f.format(step));
70
71 StaticTextItem fromText = new StaticTextItem("staticFrom");
72 fromText.setValue(titleFrom);
73 fromText.setShowTitle(false);
74 fromItem.setShowTitle(false);
75
76 StaticTextItem toText = new StaticTextItem("staticTo");
77 toText.setValue(titleTo);
78 toText.setShowTitle(false);
79 toItem.setShowTitle(false);
80
81 StaticTextItem stepText = new StaticTextItem("staticStep");
82 stepText.setValue(titleStep);
83 stepText.setShowTitle(false);
84 stepItem.setShowTitle(false);
85
86 int itemWidth = width / 6;
87 fromItem.setWidth(itemWidth);
88 fromText.setWidth(itemWidth);
89 toItem.setWidth(itemWidth);
90 toText.setWidth(itemWidth);
91 stepItem.setWidth(itemWidth);
92 stepText.setWidth(itemWidth);
93
94 setFields(fromItem, fromText, toItem, toText, stepItem, stepText);
95 setFixedColWidths(false);
96 setNumCols(6);
97 setWidth(width);
98 setAlign(Alignment.CENTER);
99 }
100
101
102 /**
103 * This method validates the entered text in the input fields. If
104 * there are values that doesn't represent a valid float, an error is
105 * displayed.
106 *
107 * @param item The FormItem.
108 */
109 protected boolean validateForm(FormItem item) {
110 boolean valid = true;
111
112 String v = (String) item.getValue();
113
114 NumberFormat f = NumberFormat.getDecimalFormat();
115 Map errors = getErrors();
116
117 try {
118 double value = f.parse(v);
119
120 errors.remove(item.getFieldName());
121 }
122 catch (NumberFormatException nfe) {
123 errors.put(item.getFieldName(), MESSAGES.wrongFormat());
124
125 item.focusInItem();
126
127 valid = false;
128 }
129
130 setErrors(errors, true);
131
132 return valid;
133 }
134
135
136 /**
137 * Returns the double value of <i>value</i>.
138 *
139 * @return the double value of <i>value</i>.
140 */
141 protected double getDouble(String value) {
142 NumberFormat f = NumberFormat.getDecimalFormat();
143
144 return f.parse(value);
145 }
146
147
148 /**
149 * Returns the start value.
150 *
151 * @return the start value.
152 */
153 public double getFrom() {
154 String v = getValueAsString(FIELD_FROM);
155
156 return getDouble(v);
157 }
158
159
160 /**
161 * Returns the end value.
162 *
163 * @return the end value.
164 */
165 public double getTo() {
166 String v = getValueAsString(FIELD_TO);
167
168 return getDouble(v);
169 }
170
171
172 /**
173 * Returns the step width.
174 *
175 * @return the step width.
176 */
177 public double getStep() {
178 String v = getValueAsString(FIELD_WIDTH);
179
180 return getDouble(v);
181 }
182 }
183 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org