comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/LocationPicker.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/ui/LocationPicker.java@821a02bbfb4e
children 172338b1407f
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.client.client.ui;
2
3 import java.util.LinkedHashMap;
4
5 import com.google.gwt.core.client.GWT;
6 import com.google.gwt.i18n.client.NumberFormat;
7 import com.smartgwt.client.data.AdvancedCriteria;
8 import com.smartgwt.client.data.Criteria;
9 import com.smartgwt.client.data.Criterion;
10 import com.smartgwt.client.types.Alignment;
11 import com.smartgwt.client.types.ListGridFieldType;
12 import com.smartgwt.client.types.OperatorId;
13 import com.smartgwt.client.widgets.form.DynamicForm;
14 import com.smartgwt.client.widgets.form.fields.SelectItem;
15 import com.smartgwt.client.widgets.form.fields.StaticTextItem;
16 import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
17 import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
18 import com.smartgwt.client.widgets.grid.CellFormatter;
19 import com.smartgwt.client.widgets.grid.ListGrid;
20 import com.smartgwt.client.widgets.grid.ListGridField;
21 import com.smartgwt.client.widgets.grid.ListGridRecord;
22 import com.smartgwt.client.widgets.grid.events.CellClickHandler;
23 import com.smartgwt.client.widgets.layout.HLayout;
24
25 import org.dive4elements.river.client.client.FLYSConstants;
26 import org.dive4elements.river.client.client.event.FilterHandler;
27 import org.dive4elements.river.client.client.event.RangeFilterEvent;
28 import org.dive4elements.river.client.client.event.StringFilterEvent;
29
30 /**
31 * Bundle widgets and handler for a lacation input helper.
32 *
33 * Note that the construction is weird and driven by issues that arose due to
34 * reasons not understood.
35 */
36 public class LocationPicker
37 implements FilterHandler
38 {
39 /** The message class that provides i18n strings.*/
40 protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
41
42 /** The locations table. */
43 protected ListGrid locationTable;
44
45 protected HLayout filterLayout;
46
47 DynamicForm resultCountForm;
48
49 CellClickHandler handler;
50
51 boolean isDistance = false;
52
53 /** Text to show number of matched items when filtered. */
54 protected StaticTextItem filterResultCount;
55
56
57 public LocationPicker(CellClickHandler handler) {
58 locationTable = new ListGrid();
59 locationTable.setShowHeaderContextMenu(false);
60 this.handler = handler;
61 }
62
63 public void prepareFilter() {
64
65 filterResultCount = new StaticTextItem(MSG.resultCount());
66 filterResultCount.setTitleAlign(Alignment.LEFT);
67 filterResultCount.setTitleStyle("color: #000");
68
69 final TableFilter filter = new TableFilter();
70 filter.setHeight("30px");
71 filter.addFilterHandler(this);
72
73 final RangeTableFilter filterRange = new RangeTableFilter();
74 filterRange.setHeight("30px");
75 filterRange.addFilterHandler(this);
76 filterRange.setVisible(false);
77
78 SelectItem filterCriteria = new SelectItem();
79 filterCriteria.setShowTitle(false);
80 filterCriteria.setWidth(100);
81 filterCriteria.addChangedHandler(new ChangedHandler() {
82 public void onChanged(ChangedEvent e) {
83 if(e.getValue().toString().equals("range")) {
84 filterRange.setVisible(true);
85 filter.setVisible(false);
86 filter.clear();
87 filterResultCount.setValue("");
88 }
89 else {
90 filterRange.setVisible(false);
91 filterRange.clear();
92 filter.setVisible(true);
93 filterResultCount.setValue("");
94 }
95 }
96 });
97
98 LinkedHashMap<String, String> filterMap =
99 new LinkedHashMap<String, String>();
100 filterMap.put("description", MSG.description());
101 filterMap.put("range", MSG.range());
102 filterCriteria.setValueMap(filterMap);
103 filterCriteria.setValue("description");
104
105 DynamicForm form = new DynamicForm();
106 form.setFields(filterCriteria);
107
108 resultCountForm = new DynamicForm();
109 resultCountForm.setFields(filterResultCount);
110
111 filterLayout = new HLayout();
112 filterLayout.addMember(form);
113 filterLayout.addMember(filter);
114 filterLayout.addMember(filterRange);
115 }
116
117
118 /** Access the main widget, a table in which locations can be chosen. */
119 public ListGrid getLocationTable() {
120 return locationTable;
121 }
122
123
124 /** Access the 'form' that shows the filter result count. */
125 public DynamicForm getResultCountForm() {
126 return resultCountForm;
127 }
128
129
130 /** Access the layout containing filter stuff. */
131 public HLayout getFilterLayout() {
132 return filterLayout;
133 }
134
135
136 /**
137 * This method creates a table that contains the location values.
138 */
139 public void createLocationTable(/*RecordClickHandler handler*/) {
140 GWT.log("Create Location Table in LocationPicker");
141
142 String baseUrl = GWT.getHostPageBaseURL();
143
144 locationTable.setWidth100();
145 locationTable.setShowRecordComponents(true);
146 locationTable.setShowRecordComponentsByCell(true);
147 locationTable.setHeight100();
148 locationTable.setEmptyMessage(MSG.empty_filter());
149 locationTable.setCanReorderFields(false);
150
151 ListGridField addLocation = new ListGridField ("", "");
152 addLocation.setType (ListGridFieldType.ICON);
153 addLocation.setWidth (20);
154 addLocation.setCellIcon (baseUrl + MSG.markerGreen());
155 ListGridField addTo = new ListGridField ("", "");
156 addTo.setType (ListGridFieldType.ICON);
157 addTo.setWidth (20);
158 addTo.setCellIcon (baseUrl + MSG.markerRed());
159
160 ListGridField ldescr = new ListGridField("description",
161 MSG.description());
162 ldescr.setType(ListGridFieldType.TEXT);
163 ldescr.setWidth("*");
164 ListGridField lside = new ListGridField("riverside",
165 MSG.riverside());
166 lside.setType(ListGridFieldType.TEXT);
167 lside.setWidth("10%");
168
169 ListGridField loc = new ListGridField("from", MSG.location());
170 loc.setCellFormatter(new CellFormatter() {
171 public String format(
172 Object value,
173 ListGridRecord record,
174 int rowNum, int colNum) {
175 if (value == null) return null;
176 try {
177 NumberFormat nf;
178 double v = Double.parseDouble((String)value);
179 nf = NumberFormat.getFormat("###0.00##");
180 return nf.format(v);
181 }
182 catch (Exception e) {
183 return value.toString();
184 }
185 }
186 }
187 );
188 loc.setType(ListGridFieldType.FLOAT);
189
190 loc.setWidth("10%");
191
192 ListGridField bottom =
193 new ListGridField("bottom", MSG.bottom_edge());
194 bottom.setType(ListGridFieldType.TEXT);
195 bottom.setWidth("10%");
196
197 ListGridField top =
198 new ListGridField("top", MSG.top_edge());
199 top.setType(ListGridFieldType.TEXT);
200 top.setWidth("10%");
201 locationTable.addCellClickHandler(handler);
202 if (isDistance) {
203 locationTable.setFields(
204 addLocation, addTo, ldescr, loc, lside, bottom, top);
205 }
206 else {
207 locationTable.setFields(
208 addLocation, ldescr, loc, lside, bottom, top);
209 }
210 }
211
212
213 @Override
214 public void onFilterCriteriaChanged(StringFilterEvent event) {
215 String search = event.getFilter();
216
217 if (search != null && search.length() > 0) {
218 Criteria c = new Criteria("description", search);
219 locationTable.filterData(c);
220 filterResultCount.setValue(locationTable.getRecords().length);
221 }
222 else {
223 locationTable.clearCriteria();
224 filterResultCount.setValue("");
225 }
226 }
227
228
229 @Override
230 public void onFilterCriteriaChanged(RangeFilterEvent event) {
231 Float from = event.getFrom() - 0.001f;
232 Float to = event.getTo() + 0.001f;
233
234 Criterion combinedFilter = null;
235 if (from.equals(Float.NaN) && to.equals(Float.NaN)) {
236 locationTable.clearCriteria();
237 filterResultCount.setValue("");
238 return;
239 }
240 else if (from.equals(Float.NaN)) {
241 combinedFilter =
242 new Criterion("from", OperatorId.LESS_OR_EQUAL, to);
243 }
244 else if (to.equals(Float.NaN)) {
245 combinedFilter =
246 new Criterion("from", OperatorId.GREATER_OR_EQUAL, from);
247 }
248 else {
249 combinedFilter =
250 new AdvancedCriteria(OperatorId.AND, new Criterion[] {
251 new Criterion("from", OperatorId.GREATER_OR_EQUAL, from),
252 new Criterion("from", OperatorId.LESS_OR_EQUAL, to)
253 });
254 }
255 locationTable.filterData(combinedFilter);
256 filterResultCount.setValue(locationTable.getRecords().length);
257 }
258
259 public void setIsDistance(boolean value) {
260 this.isDistance = value;
261 }
262
263 public boolean isDistance() {
264 return this.isDistance;
265 }
266 }
267 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org