Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/client/widgets/KMSpinner.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-client/src/main/java/org/dive4elements/river/client/client/widgets/KMSpinner.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.client.widgets; | |
2 | |
3 import com.google.gwt.core.client.GWT; | |
4 import com.google.gwt.i18n.client.NumberFormat; | |
5 | |
6 import com.smartgwt.client.data.Record; | |
7 import com.smartgwt.client.widgets.Button; | |
8 import com.smartgwt.client.widgets.Label; | |
9 import com.smartgwt.client.widgets.events.ClickEvent; | |
10 import com.smartgwt.client.widgets.form.DynamicForm; | |
11 import com.smartgwt.client.widgets.form.FormItemValueFormatter; | |
12 import com.smartgwt.client.widgets.form.FormItemValueParser; | |
13 import com.smartgwt.client.widgets.form.fields.FormItem; | |
14 import com.smartgwt.client.widgets.form.fields.TextItem; | |
15 import com.smartgwt.client.widgets.form.fields.events.BlurEvent; | |
16 import com.smartgwt.client.widgets.form.fields.events.BlurHandler; | |
17 import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent; | |
18 import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler; | |
19 import com.smartgwt.client.widgets.layout.HLayout; | |
20 | |
21 import org.dive4elements.river.client.shared.model.FacetRecord; | |
22 | |
23 import java.util.ArrayList; | |
24 import java.util.List; | |
25 | |
26 /** | |
27 * SpinnerItem-like element with text label and up/down buttons. | |
28 */ | |
29 public class KMSpinner extends HLayout { | |
30 protected List<KMSpinnerChangeListener> listeners = new ArrayList<KMSpinnerChangeListener>(); | |
31 | |
32 protected Label label; | |
33 protected FacetRecord facetRecord; | |
34 protected double value; | |
35 | |
36 public KMSpinner(double initialValue, FacetRecord facetRecord) { | |
37 super(2); | |
38 this.facetRecord = facetRecord; | |
39 this.value = initialValue; | |
40 | |
41 setWidth("99%"); | |
42 setHeight(18); | |
43 | |
44 // minusButton shall ask service for previous available cs. | |
45 Button minusButton = new Button("-"); | |
46 minusButton.setWidth(18); | |
47 minusButton.setHeight(18); | |
48 minusButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() { | |
49 public void onClick(ClickEvent evt) { | |
50 fireChangedEvent(value - 0.1d, false); | |
51 } | |
52 }); | |
53 | |
54 DynamicForm form = new DynamicForm(); | |
55 final TextItem kmField = new TextItem(); | |
56 kmField.setValue(initialValue); | |
57 kmField.setWidth("*"); | |
58 kmField.setTitle(""); | |
59 kmField.setHeight(16); | |
60 | |
61 FormItemValueFormatter doubleFormat = new FormItemValueFormatter() { | |
62 public String formatValue(Object value, Record record, DynamicForm form, FormItem item) { | |
63 if (value != null) { | |
64 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
65 try { | |
66 double d = Double.valueOf(value.toString()).doubleValue(); | |
67 return nf.format(d); | |
68 } | |
69 catch (Exception e) { | |
70 GWT.log("EditorValueFormatter exception: " + e.toString()); | |
71 | |
72 // Remove junk chars from input string | |
73 return doublefyString(value.toString()); | |
74 } | |
75 } | |
76 else { | |
77 return null; | |
78 } | |
79 } | |
80 }; | |
81 kmField.setEditorValueFormatter(doubleFormat); | |
82 | |
83 FormItemValueParser doubleParser = new FormItemValueParser() { | |
84 public Object parseValue(String value, DynamicForm form, FormItem item) { | |
85 if (value == null) | |
86 return null; | |
87 try { | |
88 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
89 double d = nf.parse(value.toString()); | |
90 return Double.toString(d); | |
91 } | |
92 catch(NumberFormatException nfe) { | |
93 return value; | |
94 } | |
95 } | |
96 }; | |
97 kmField.setEditorValueParser(doubleParser); | |
98 | |
99 // Update on focus lost and enter-pressed. | |
100 kmField.addBlurHandler(new BlurHandler() { | |
101 @Override | |
102 public void onBlur(BlurEvent be) { | |
103 if (kmField.getValue() != null) { | |
104 try { | |
105 fireChangedEvent(Double.parseDouble(kmField.getValue().toString()), true); | |
106 } | |
107 catch(NumberFormatException nfe) { | |
108 GWT.log("entered string cannot be parsed to double."); | |
109 } | |
110 } | |
111 } | |
112 }); | |
113 kmField.addKeyPressHandler(new KeyPressHandler() { | |
114 @Override | |
115 public void onKeyPress(KeyPressEvent kpe) { | |
116 if (kpe.getKeyName().equals("Enter")) { | |
117 kmField.blurItem(); | |
118 } | |
119 } | |
120 }); | |
121 | |
122 // TODO: i18n Now add all the validators, formatters, editors/parsers etc. | |
123 form.setFields(kmField); | |
124 form.setTitle(""); | |
125 form.setTitlePrefix(""); | |
126 form.setTitleSuffix(""); | |
127 form.setTitleWidth(0); | |
128 form.setWidth(50); | |
129 form.setHeight(18); | |
130 | |
131 // PlusButton shall ask service for next available cs. | |
132 Button plusButton = new Button("+"); | |
133 plusButton.setWidth(18); | |
134 plusButton.setHeight(18); | |
135 plusButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() { | |
136 public void onClick(ClickEvent evt) { | |
137 fireChangedEvent(value + 0.1d, true); | |
138 } | |
139 }); | |
140 | |
141 this.addMember(minusButton); | |
142 this.addMember(form); | |
143 this.addMember(plusButton); | |
144 } | |
145 | |
146 public void addChangeListener(KMSpinnerChangeListener listener) { | |
147 this.listeners.add(listener); | |
148 } | |
149 | |
150 protected void fireChangedEvent(double val, boolean up) { | |
151 for(KMSpinnerChangeListener listener : listeners) { | |
152 listener.spinnerValueEntered(this, val, facetRecord, up); | |
153 } | |
154 } | |
155 | |
156 /** | |
157 * Remove junk chars from double string. | |
158 * This method should work for most locales, but not for | |
159 * exotic ones that do not use "." or "," as decimal | |
160 * separator. | |
161 * @return | |
162 */ | |
163 protected String doublefyString(String str) { | |
164 StringBuilder buf = new StringBuilder(str.length()); | |
165 | |
166 for (int n = 0; n < str.length(); n++) { | |
167 char c = str.charAt(n); | |
168 if ((c >= '0' && c <= '9') || c == '.' || c == ',') { | |
169 buf.append(c); | |
170 } | |
171 } | |
172 | |
173 return buf.toString(); | |
174 } | |
175 } | |
176 |