comparison flys-client/src/main/java/de/intevation/flys/client/client/widgets/KMSpinner.java @ 3717:3b4cef59836a

KMSpinner refactoring flys-client/trunk@5482 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Christian Lins <christian.lins@intevation.de>
date Sun, 16 Sep 2012 12:38:24 +0000
parents
children 4aa7216b329a
comparison
equal deleted inserted replaced
3716:4e33aa341e51 3717:3b4cef59836a
1 package de.intevation.flys.client.client.widgets;
2
3 import com.google.gwt.core.client.GWT;
4 import com.google.gwt.i18n.client.NumberFormat;
5 import com.smartgwt.client.data.Record;
6 import com.smartgwt.client.widgets.Button;
7 import com.smartgwt.client.widgets.Label;
8 import com.smartgwt.client.widgets.events.ClickEvent;
9 import com.smartgwt.client.widgets.form.DynamicForm;
10 import com.smartgwt.client.widgets.form.FormItemValueFormatter;
11 import com.smartgwt.client.widgets.form.FormItemValueParser;
12 import com.smartgwt.client.widgets.form.fields.FormItem;
13 import com.smartgwt.client.widgets.form.fields.TextItem;
14 import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
15 import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
16 import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
17 import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
18 import com.smartgwt.client.widgets.layout.HLayout;
19
20 import de.intevation.flys.client.shared.model.FacetRecord;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 /**
26 * SpinnerItem-like element with text label and up/down buttons.
27 */
28 public class KMSpinner extends HLayout {
29 protected List<KMSpinnerChangeListener> listeners = new ArrayList<KMSpinnerChangeListener>();
30
31 protected Label label;
32 protected FacetRecord facetRecord;
33 protected double value;
34
35 public KMSpinner(double initialValue, FacetRecord facetRecord) {
36 super(2);
37 this.facetRecord = facetRecord;
38 this.value = initialValue;
39
40 setWidth("99%");
41
42 // minusButton shall ask service for previous available cs.
43 Button minusButton = new Button("-");
44 minusButton.setWidth(18);
45 minusButton.setHeight(18);
46 minusButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
47 public void onClick(ClickEvent evt) {
48 fireChangedEvent(value - 0.1d, false);
49 }
50 });
51
52 DynamicForm form = new DynamicForm();
53 final TextItem kmField = new TextItem();
54 kmField.setValue(initialValue);
55 kmField.setWidth("*");
56 kmField.setTitle("");
57 kmField.setHeight(16);
58
59 FormItemValueFormatter doubleFormat = new FormItemValueFormatter() {
60 public String formatValue(Object value, Record record, DynamicForm form, FormItem item) {
61 if (value != null) {
62 NumberFormat nf = NumberFormat.getDecimalFormat();
63 try {
64 double d = Double.valueOf(value.toString()).doubleValue();
65 return nf.format(d);
66 }
67 catch (Exception e) {
68 return value.toString();
69 }
70 }
71 else {
72 return null;
73 }
74 }
75 };
76 kmField.setEditorValueFormatter(doubleFormat);
77
78 FormItemValueParser doubleParser = new FormItemValueParser() {
79 public Object parseValue(String value, DynamicForm form, FormItem item) {
80 if (value == null)
81 return null;
82 try {
83 NumberFormat nf = NumberFormat.getDecimalFormat();
84 double d = nf.parse(value.toString());
85 return Double.toString(d);
86 }
87 catch(NumberFormatException nfe) {
88 return value;
89 }
90 }
91 };
92 kmField.setEditorValueParser(doubleParser);
93
94 // Update on focus lost and enter-pressed.
95 kmField.addBlurHandler(new BlurHandler() {
96 @Override
97 public void onBlur(BlurEvent be) {
98 if (kmField.getValue() != null) {
99 try {
100 fireChangedEvent(Double.parseDouble(kmField.getValue().toString()), true);
101 }
102 catch(NumberFormatException nfe) {
103 GWT.log("entered string cannot be parsed to double.");
104 }
105 }
106 }
107 });
108 kmField.addKeyPressHandler(new KeyPressHandler() {
109 @Override
110 public void onKeyPress(KeyPressEvent kpe) {
111 if (kpe.getKeyName().equals("Enter")) {
112 kmField.blurItem();
113 }
114 }
115 });
116
117 // TODO: i18n Now add all the validators, formatters, editors/parsers etc.
118 form.setFields(kmField);
119 form.setTitle("");
120 form.setTitlePrefix("");
121 form.setTitleSuffix("");
122 form.setTitleWidth(0);
123 form.setWidth(50);
124 form.setHeight(18);
125
126 // PlusButton shall ask service for next available cs.
127 Button plusButton = new Button("+");
128 plusButton.setWidth(18);
129 plusButton.setHeight(18);
130 plusButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
131 public void onClick(ClickEvent evt) {
132 fireChangedEvent(value + 0.1d, true);
133 }
134 });
135
136 this.addMember(minusButton);
137 this.addMember(form);
138 this.addMember(plusButton);
139 }
140
141 public void addChangeListener(KMSpinnerChangeListener listener) {
142 this.listeners.add(listener);
143 }
144
145 protected void fireChangedEvent(double val, boolean up) {
146 for(KMSpinnerChangeListener listener : listeners) {
147 listener.spinnerValueEntered(this, val, facetRecord, up);
148 }
149 }
150 }
151

http://dive4elements.wald.intevation.org