comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationPicker.java @ 1590:4b773cfd11b5

Refactoring to ease code-reuse. flys-client/trunk@3882 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 02 Feb 2012 13:42:12 +0000
parents
children 99bd77501188
comparison
equal deleted inserted replaced
1589:c9ae3c8504d5 1590:4b773cfd11b5
1 package de.intevation.flys.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
8 import com.smartgwt.client.data.Criteria;
9 import com.smartgwt.client.data.Criterion;
10 import com.smartgwt.client.data.AdvancedCriteria;
11 import com.smartgwt.client.types.OperatorId;
12
13 import com.smartgwt.client.widgets.form.DynamicForm;
14 import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
15 import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
16
17 import com.smartgwt.client.widgets.form.fields.SelectItem;
18 import com.smartgwt.client.widgets.form.fields.StaticTextItem;
19 import com.smartgwt.client.widgets.layout.HLayout;
20 import com.smartgwt.client.widgets.grid.ListGrid;
21 import com.smartgwt.client.widgets.grid.ListGridField;
22 import com.smartgwt.client.widgets.grid.ListGridRecord;
23 import com.smartgwt.client.widgets.grid.CellFormatter;
24 import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
25
26 import com.smartgwt.client.types.ListGridFieldType;
27 import com.smartgwt.client.types.Alignment;
28
29 import de.intevation.flys.client.client.FLYSConstants;
30 import de.intevation.flys.client.client.event.FilterHandler;
31 import de.intevation.flys.client.client.event.StringFilterEvent;
32 import de.intevation.flys.client.client.event.RangeFilterEvent;
33
34 /**
35 * Bundle widgets and handler for a lacation input helper.
36 *
37 * Note that the construction is weird and driven by issues that arose due to
38 * reasons not understood.
39 */
40 public class LocationPicker
41 implements FilterHandler
42 {
43 /** The message class that provides i18n strings.*/
44 protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
45
46 /** The locations table. */
47 protected ListGrid locationTable;
48
49 protected HLayout filterLayout;
50
51 DynamicForm resultCountForm;
52
53 RecordClickHandler handler;
54
55 /** Text to show number of matched items when filtered. */
56 protected StaticTextItem filterResultCount;
57
58
59 public LocationPicker(RecordClickHandler handler) {
60 locationTable = new ListGrid();
61 locationTable.setShowHeaderContextMenu(false);
62 this.handler = handler;
63 }
64
65 public void prepareFilter() {
66
67 filterResultCount = new StaticTextItem(MSG.resultCount());
68 filterResultCount.setTitleAlign(Alignment.LEFT);
69 filterResultCount.setTitleStyle("color: #000");
70
71 final TableFilter filter = new TableFilter();
72 filter.setHeight("30px");
73 filter.addFilterHandler(this);
74
75 final RangeTableFilter filterRange = new RangeTableFilter();
76 filterRange.setHeight("30px");
77 filterRange.addFilterHandler(this);
78 filterRange.setVisible(false);
79
80 SelectItem filterCriteria = new SelectItem();
81 filterCriteria.setShowTitle(false);
82 filterCriteria.setWidth(100);
83 filterCriteria.addChangedHandler(new ChangedHandler() {
84 public void onChanged(ChangedEvent e) {
85 if(e.getValue().toString().equals("range")) {
86 filterRange.setVisible(true);
87 filter.setVisible(false);
88 filter.clear();
89 filterResultCount.setValue("");
90 }
91 else {
92 filterRange.setVisible(false);
93 filterRange.clear();
94 filter.setVisible(true);
95 filterResultCount.setValue("");
96 }
97 }
98 });
99
100 LinkedHashMap<String, String> filterMap =
101 new LinkedHashMap<String, String>();
102 filterMap.put("description", MSG.description());
103 filterMap.put("range", MSG.range());
104 filterCriteria.setValueMap(filterMap);
105 filterCriteria.setValue("description");
106
107 DynamicForm form = new DynamicForm();
108 form.setFields(filterCriteria);
109
110 resultCountForm = new DynamicForm();
111 resultCountForm.setFields(filterResultCount);
112
113 filterLayout = new HLayout();
114 filterLayout.addMember(form);
115 filterLayout.addMember(filter);
116 filterLayout.addMember(filterRange);
117 }
118
119
120 /** Access the main widget, a table in which locations can be chosen. */
121 public ListGrid getLocationTable() {
122 return locationTable;
123 }
124
125
126 /** Access the 'form' that shows the filter result count. */
127 public DynamicForm getResultCountForm() {
128 return resultCountForm;
129 }
130
131
132 /** Access the layout containing filter stuff. */
133 public HLayout getFilterLayout() {
134 return filterLayout;
135 }
136
137
138 /**
139 * This method creates a table that contains the location values.
140 */
141 protected void createLocationTable(/*RecordClickHandler handler*/) {
142 GWT.log("Create Location Table in LocationPicker");
143
144 String baseUrl = GWT.getHostPageBaseURL();
145
146 locationTable.setWidth100();
147 locationTable.setShowRecordComponents(true);
148 locationTable.setShowRecordComponentsByCell(true);
149 locationTable.setHeight100();
150 locationTable.setEmptyMessage(MSG.empty_filter());
151 locationTable.setCanReorderFields(false);
152
153 ListGridField addLocation = new ListGridField ("", "");
154 addLocation.setType (ListGridFieldType.ICON);
155 addLocation.setWidth (20);
156 addLocation.addRecordClickHandler (handler);
157 addLocation.setCellIcon (baseUrl + MSG.markerGreen());
158 GWT.log ("i18n: ..... " + MSG.description());
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
202
203 locationTable.setFields(
204 addLocation, ldescr, loc, lside, bottom, top);
205 }
206
207
208 @Override
209 public void onFilterCriteriaChanged(StringFilterEvent event) {
210 String search = event.getFilter();
211
212 if (search != null && search.length() > 0) {
213 Criteria c = new Criteria("description", search);
214 locationTable.filterData(c);
215 filterResultCount.setValue(locationTable.getRecords().length);
216 }
217 else {
218 // TODO Remove filter
219 }
220 }
221
222
223 @Override
224 public void onFilterCriteriaChanged(RangeFilterEvent event) {
225 Float from = event.getFrom() - 0.001f;
226 Float to = event.getTo() + 0.001f;
227
228 Criterion combinedFilter = null;
229 if (from.equals(Float.NaN) && to.equals(Float.NaN)) {
230 locationTable.clearCriteria();
231 filterResultCount.setValue("");
232 return;
233 }
234 else if (from.equals(Float.NaN)) {
235 combinedFilter =
236 new Criterion("from", OperatorId.LESS_OR_EQUAL, to);
237 }
238 else if (to.equals(Float.NaN)) {
239 combinedFilter =
240 new Criterion("from", OperatorId.GREATER_OR_EQUAL, from);
241 }
242 else {
243 combinedFilter =
244 new AdvancedCriteria(OperatorId.AND, new Criterion[] {
245 new Criterion("from", OperatorId.GREATER_OR_EQUAL, from),
246 new Criterion("from", OperatorId.LESS_OR_EQUAL, to)
247 });
248 }
249 locationTable.filterData(combinedFilter);
250 filterResultCount.setValue(locationTable.getRecords().length);
251 }
252 }
253 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org