Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/fixation/FixationPanel.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/fixation/FixationPanel.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.client.ui.fixation; | |
2 | |
3 import com.google.gwt.core.client.GWT; | |
4 import com.google.gwt.i18n.client.DateTimeFormat; | |
5 import com.google.gwt.i18n.client.NumberFormat; | |
6 import com.google.gwt.json.client.JSONNumber; | |
7 import com.google.gwt.json.client.JSONObject; | |
8 import com.google.gwt.json.client.JSONString; | |
9 import com.google.gwt.user.client.rpc.AsyncCallback; | |
10 | |
11 import com.smartgwt.client.types.Alignment; | |
12 import com.smartgwt.client.util.SC; | |
13 import com.smartgwt.client.widgets.Button; | |
14 import com.smartgwt.client.widgets.Canvas; | |
15 import com.smartgwt.client.widgets.HTMLPane; | |
16 import com.smartgwt.client.widgets.Img; | |
17 import com.smartgwt.client.widgets.events.ClickEvent; | |
18 import com.smartgwt.client.widgets.events.ClickHandler; | |
19 import com.smartgwt.client.widgets.events.ResizedEvent; | |
20 import com.smartgwt.client.widgets.events.ResizedHandler; | |
21 import com.smartgwt.client.widgets.form.DynamicForm; | |
22 import com.smartgwt.client.widgets.form.fields.TextItem; | |
23 import com.smartgwt.client.widgets.form.fields.events.ChangedEvent; | |
24 import com.smartgwt.client.widgets.form.fields.events.ChangedHandler; | |
25 import com.smartgwt.client.widgets.layout.HLayout; | |
26 import com.smartgwt.client.widgets.layout.VLayout; | |
27 import com.smartgwt.client.widgets.tab.Tab; | |
28 import com.smartgwt.client.widgets.tab.TabSet; | |
29 | |
30 import org.dive4elements.river.client.client.Config; | |
31 import org.dive4elements.river.client.client.FLYSConstants; | |
32 import org.dive4elements.river.client.client.services.FixingsOverviewService; | |
33 import org.dive4elements.river.client.client.services.FixingsOverviewServiceAsync; | |
34 import org.dive4elements.river.client.client.ui.AbstractUIProvider; | |
35 import org.dive4elements.river.client.shared.model.Data; | |
36 import org.dive4elements.river.client.shared.model.DataList; | |
37 import org.dive4elements.river.client.shared.model.FixAnalysisArtifact; | |
38 import org.dive4elements.river.client.shared.model.FixFilter; | |
39 import org.dive4elements.river.client.shared.model.FixingsOverviewInfo; | |
40 | |
41 import java.util.Date; | |
42 import java.util.HashMap; | |
43 | |
44 | |
45 /** | |
46 * This UIProvider creates helper panel for fixation analysis without input | |
47 * elements. | |
48 * | |
49 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> | |
50 */ | |
51 public abstract class FixationPanel | |
52 extends AbstractUIProvider | |
53 implements ResizedHandler | |
54 { | |
55 private static final long serialVersionUID = -3667553404493415619L; | |
56 | |
57 protected static HashMap<String, FixationPanel> instances = new HashMap<String, FixationPanel>(); | |
58 | |
59 /** The message class that provides i18n strings. */ | |
60 protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); | |
61 | |
62 protected FixingsOverviewServiceAsync overviewService = | |
63 GWT.create(FixingsOverviewService.class); | |
64 | |
65 protected String htmlOverview; | |
66 protected FixingsOverviewInfo fixInfo; | |
67 protected TabSet tabs; | |
68 protected Tab events; | |
69 protected Tab chart; | |
70 protected VLayout chartContainer; | |
71 protected Img chartImg; | |
72 protected TextItem kmText; | |
73 | |
74 public static final DateTimeFormat DTF = DateTimeFormat.getFormat("dd.MM.yyyy"); | |
75 | |
76 | |
77 public FixationPanel() { | |
78 chartImg = new Img(); | |
79 htmlOverview = ""; | |
80 } | |
81 | |
82 | |
83 /** Get the (master) artifact UUID. */ | |
84 protected String getArtifactUuid() { | |
85 return this.artifact.getUuid(); | |
86 } | |
87 | |
88 protected void init() { | |
89 } | |
90 | |
91 @Override | |
92 public Data[] getData() { | |
93 return null; | |
94 } | |
95 | |
96 @Override | |
97 public Canvas create(DataList list) { | |
98 VLayout layout = new VLayout(); | |
99 | |
100 Canvas helper = createHelper(); | |
101 this.helperContainer.addMember(helper); | |
102 | |
103 Canvas submit = getNextButton(); | |
104 Canvas widget = createWidget(list); | |
105 | |
106 layout.addMember(widget); | |
107 layout.addMember(submit); | |
108 return layout; | |
109 } | |
110 | |
111 @Override | |
112 public Canvas createOld(DataList list) { | |
113 return new DynamicForm(); | |
114 } | |
115 | |
116 protected Canvas createHelper() { | |
117 Config config = Config.getInstance(); | |
118 String locale = config.getLocale (); | |
119 | |
120 tabs = new TabSet(); | |
121 events = new Tab(MESSAGES.events()); | |
122 chart = new Tab(MESSAGES.kmchart()); | |
123 | |
124 chartContainer = new VLayout(); | |
125 Canvas scroll = createChartHelper(); | |
126 | |
127 VLayout layout = new VLayout(); | |
128 layout.addResizedHandler(this); | |
129 layout.addMember(chartContainer); | |
130 layout.addMember(scroll); | |
131 layout.setAlign(Alignment.CENTER); | |
132 chart.setPane(layout); | |
133 | |
134 final HTMLPane eventPane = new HTMLPane(); | |
135 | |
136 String river = artifact.getArtifactDescription().getRiver(); | |
137 createCallback(); | |
138 | |
139 String callBack = "fixationCallback(this.checked, this.name)"; | |
140 | |
141 if (this.artifact instanceof FixAnalysisArtifact == false) | |
142 return chartContainer; | |
143 | |
144 FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact; | |
145 | |
146 overviewService.generateOverview( | |
147 locale, | |
148 artifact.getUuid(), | |
149 getOverviewFilter(art.getFilter()), | |
150 renderCheckboxes(), | |
151 callBack, | |
152 new AsyncCallback<FixingsOverviewInfo>() { | |
153 @Override | |
154 public void onFailure(Throwable caught) { | |
155 GWT.log("Could not receive overview."); | |
156 SC.warn(caught.getMessage()); | |
157 } | |
158 @Override | |
159 public void onSuccess(FixingsOverviewInfo info) { | |
160 GWT.log("Successfully loaded overview."); | |
161 fixInfo = info; | |
162 htmlOverview = info.getHTML(); | |
163 FixAnalysisArtifact art = (FixAnalysisArtifact)artifact; | |
164 FixFilter filter = art.getFilter(); | |
165 filter.setRiver(info.getRiver()); | |
166 if (filter.getCurrentKm() == -Double.MAX_VALUE || | |
167 filter.getCurrentKm() == -1d) { | |
168 filter.setCurrentKm(info.getFrom()); | |
169 filter.setToKm(info.getTo()); | |
170 } | |
171 if (kmText != null) { | |
172 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
173 try { | |
174 double d = Double.valueOf(filter.getCurrentKm()); | |
175 kmText.setValue(nf.format(d)); | |
176 } catch (NumberFormatException e) { | |
177 kmText.setValue(filter.getCurrentKm()); | |
178 } | |
179 } | |
180 eventPane.setContents(htmlOverview); | |
181 updateChartTab(fixInfo.getFrom()); | |
182 events.setPane(eventPane); | |
183 success(); | |
184 } | |
185 }); | |
186 | |
187 tabs.addTab(events); | |
188 tabs.addTab(chart); | |
189 | |
190 return tabs; | |
191 } | |
192 | |
193 | |
194 protected Canvas createChartHelper() { | |
195 DynamicForm form = new DynamicForm(); | |
196 Button lower = new Button("<<"); | |
197 lower.setWidth(30); | |
198 Button upper = new Button(">>"); | |
199 upper.setWidth(30); | |
200 kmText = new TextItem(); | |
201 kmText.setWidth(60); | |
202 kmText.setShowTitle(false); | |
203 | |
204 | |
205 form.setFields(kmText); | |
206 form.setWidth(60); | |
207 lower.addClickHandler(new ClickHandler() { | |
208 @Override | |
209 public void onClick(ClickEvent ce) { | |
210 FixFilter filter = updateChartTabLow(); | |
211 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
212 try { | |
213 double d = Double.valueOf(filter.getCurrentKm()); | |
214 kmText.setValue(nf.format(d)); | |
215 } catch (NumberFormatException e) { | |
216 kmText.setValue(filter.getCurrentKm()); | |
217 } | |
218 } | |
219 }); | |
220 | |
221 upper.addClickHandler(new ClickHandler() { | |
222 @Override | |
223 public void onClick(ClickEvent ce) { | |
224 FixFilter filter = updateChartTabUp(); | |
225 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
226 try { | |
227 double d = Double.valueOf(filter.getCurrentKm()); | |
228 kmText.setValue(nf.format(d)); | |
229 } catch (NumberFormatException e) { | |
230 kmText.setValue(filter.getCurrentKm()); | |
231 } | |
232 } | |
233 }); | |
234 | |
235 kmText.addChangedHandler(new ChangedHandler() { | |
236 @Override | |
237 public void onChanged(ChangedEvent ce) { | |
238 //TODO: get current value. | |
239 if(ce.getItem().getValue() != null) { | |
240 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
241 try { | |
242 double d = nf.parse(ce.getItem().getValue().toString()); | |
243 updateChartTab(d); | |
244 } | |
245 catch(NumberFormatException nfe) { | |
246 // Do nothing. | |
247 } | |
248 } | |
249 } | |
250 }); | |
251 | |
252 HLayout layout = new HLayout(); | |
253 layout.setAlign(Alignment.CENTER); | |
254 | |
255 layout.addMember(lower); | |
256 layout.addMember(form); | |
257 layout.addMember(upper); | |
258 return layout; | |
259 } | |
260 | |
261 protected void updateChartTab(double km) { | |
262 Config config = Config.getInstance(); | |
263 String locale = config.getLocale (); | |
264 | |
265 FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact; | |
266 | |
267 if (fixInfo != null) { | |
268 if (km < fixInfo.getFrom()) km = fixInfo.getFrom(); | |
269 if (km > fixInfo.getTo()) km = fixInfo.getTo(); | |
270 } | |
271 | |
272 FixFilter filter = art.getFilter(); | |
273 | |
274 if (km < filter.getFromKm()) km = filter.getFromKm(); | |
275 if (km > filter.getToKm()) km = filter.getToKm(); | |
276 | |
277 filter.setCurrentKm(km); | |
278 | |
279 int hWidth = helperContainer.getWidth() - 12; | |
280 int hHeight = helperContainer.getHeight() - 62; | |
281 | |
282 if ((int)(hHeight *4f/3) < hWidth) { | |
283 hWidth = (int)(hHeight * 4f/3); | |
284 } | |
285 else { | |
286 hHeight = (int)(hWidth *3f/4); | |
287 } | |
288 | |
289 String imgUrl = GWT.getModuleBaseURL(); | |
290 imgUrl += "fixings-km-chart"; | |
291 imgUrl += "?locale=" + locale; | |
292 imgUrl += "&filter=" + getChartFilter(filter, hWidth, hHeight); | |
293 | |
294 if (chartContainer.hasMember(chartImg)) { | |
295 chartImg.setWidth(hWidth); | |
296 chartImg.setHeight(hHeight); | |
297 chartImg.setSrc(imgUrl); | |
298 } | |
299 else { | |
300 chartImg = new Img(imgUrl, hWidth, hHeight); | |
301 chartContainer.addMember(chartImg); | |
302 } | |
303 } | |
304 | |
305 | |
306 protected FixFilter updateChartTabLow() { | |
307 FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact; | |
308 | |
309 FixFilter filter = art.getFilter(); | |
310 | |
311 double curr = filter.getCurrentKm(); | |
312 if (curr > filter.getFromKm()) { | |
313 double newVal = (curr - 0.1) * 10; | |
314 long round = Math.round(newVal); | |
315 updateChartTab(((double)round) / 10); | |
316 } | |
317 return filter; | |
318 } | |
319 | |
320 | |
321 protected FixFilter updateChartTabUp() { | |
322 FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact; | |
323 | |
324 FixFilter filter = art.getFilter(); | |
325 | |
326 double curr = filter.getCurrentKm(); | |
327 if (curr < filter.getToKm()) { | |
328 double newVal = (curr + 0.1) * 10; | |
329 long round = Math.round(newVal); | |
330 updateChartTab(((double)round) / 10); | |
331 } | |
332 return filter; | |
333 } | |
334 | |
335 | |
336 @Override | |
337 public void onResized(ResizedEvent re) { | |
338 FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact; | |
339 | |
340 updateChartTab(art.getFilter().getCurrentKm()); | |
341 } | |
342 | |
343 | |
344 private native void createCallback() /*-{ | |
345 $wnd.fixationCallback = @org.dive4elements.river.client.client.ui.fixation.FixationPanel::helperCallback(ZLjava/lang/String;); | |
346 }-*/; | |
347 | |
348 private static void helperCallback(boolean checked, String name) { | |
349 String[] parts = name.split(":"); | |
350 String uuid = parts[0]; | |
351 String cid = parts[1]; | |
352 | |
353 FixationPanel p = FixationPanel.getInstance(uuid); | |
354 if (p != null) { | |
355 p.setValues(cid, checked); | |
356 } | |
357 } | |
358 | |
359 private static FixationPanel getInstance(String uuid) { | |
360 return instances.get(uuid); | |
361 } | |
362 | |
363 public abstract Canvas createWidget(DataList data); | |
364 public abstract void setValues(String cid, boolean checked); | |
365 public abstract boolean renderCheckboxes(); | |
366 public abstract void success(); | |
367 | |
368 | |
369 /** Creates JSON string from filter. */ | |
370 public static String getOverviewFilter(FixFilter filter) { | |
371 String river = filter.getRiver(); | |
372 | |
373 if (river != null && river.length() > 0) { | |
374 JSONObject jfix = new JSONObject(); | |
375 JSONObject jfilter = new JSONObject(); | |
376 JSONObject jrName = new JSONObject(); | |
377 JSONString jrValue = new JSONString(river); | |
378 jrName.put("name", jrValue); | |
379 jfilter.put("river", jrName); | |
380 jfix.put("fixings", createFilter(filter, jfilter)); | |
381 return jfix.toString(); | |
382 } | |
383 return ""; | |
384 } | |
385 | |
386 public String getChartFilter(FixFilter filter, int width, int height) { | |
387 String river = filter.getRiver(); | |
388 double currentKm = filter.getCurrentKm(); | |
389 double fromKm = filter.getFromKm(); | |
390 double toKm = filter.getToKm(); | |
391 | |
392 if (river != null && river.length() > 0 && | |
393 currentKm >= fromKm && currentKm <= toKm) | |
394 { | |
395 JSONObject jfix = new JSONObject(); | |
396 JSONObject jfilter = new JSONObject(); | |
397 JSONObject jrName = new JSONObject(); | |
398 JSONString jrValue = new JSONString(river); | |
399 JSONObject jkm = new JSONObject(); | |
400 JSONNumber jkmValue = new JSONNumber(currentKm); | |
401 JSONObject jextent = new JSONObject(); | |
402 JSONNumber jwidth = new JSONNumber(width); | |
403 JSONNumber jheight = new JSONNumber(height); | |
404 | |
405 jkm.put("value", jkmValue); | |
406 jrName.put("name", jrValue); | |
407 jfilter.put("river", jrName); | |
408 jfilter.put("km", jkm); | |
409 jextent.put("width", jwidth); | |
410 jextent.put("height", jheight); | |
411 jfilter.put("extent", jextent); | |
412 jfix.put("fixings", createFilter(filter, jfilter)); | |
413 return jfix.toString(); | |
414 } | |
415 return ""; | |
416 } | |
417 | |
418 protected static JSONObject createFilter(FixFilter filter, JSONObject root) { | |
419 double fromKm = filter.getFromKm(); | |
420 double toKm = filter.getToKm(); | |
421 boolean hasDate = filter.getFromDate() > 0 && filter.getToDate() > 0; | |
422 | |
423 if (fromKm >= 0 && toKm >= 0 && fromKm <= toKm) { | |
424 JSONObject range = new JSONObject(); | |
425 JSONObject fromtokm = new JSONObject(); | |
426 JSONNumber f = new JSONNumber(fromKm); | |
427 JSONNumber t = new JSONNumber(toKm); | |
428 fromtokm.put("from", f); | |
429 fromtokm.put("to", t); | |
430 root.put("range", fromtokm); | |
431 } | |
432 | |
433 JSONObject and = new JSONObject(); | |
434 if (hasDate) { | |
435 long fromDate = filter.getFromDate(); | |
436 long toDate = filter.getToDate(); | |
437 | |
438 Date df = new Date(fromDate); | |
439 Date dt = new Date(toDate); | |
440 | |
441 JSONObject daterange = new JSONObject(); | |
442 JSONString f = new JSONString(DTF.format(df)); | |
443 JSONString t = new JSONString(DTF.format(dt)); | |
444 | |
445 daterange.put("from", f); | |
446 daterange.put("to", t); | |
447 and.put("date-range", daterange); | |
448 } | |
449 | |
450 int fromClass = filter.getFromClass(); | |
451 int toClass = filter.getToClass(); | |
452 | |
453 if (fromClass >= 0 && toClass >= 0 && fromClass <= toClass) { | |
454 JSONObject classrange = new JSONObject(); | |
455 JSONNumber f = new JSONNumber(fromClass); | |
456 JSONNumber t = new JSONNumber(toClass); | |
457 | |
458 classrange.put("from", f); | |
459 classrange.put("to", t); | |
460 and.put("sector-range", classrange); | |
461 } | |
462 | |
463 int[] events = filter.getEvents(); | |
464 | |
465 if (events.length > 0) { | |
466 StringBuilder cids = new StringBuilder(); | |
467 | |
468 for (int i = 0; i < events.length; i++) { | |
469 if (i > 0) cids.append(' '); | |
470 cids.append(events[i]); | |
471 } | |
472 JSONObject columns = new JSONObject(); | |
473 columns.put("cids", new JSONString(cids.toString())); | |
474 and.put("columns", columns); | |
475 } | |
476 if (and.size() > 0) { | |
477 JSONObject jFilter = new JSONObject(); | |
478 jFilter.put("and", and); | |
479 root.put("filter", jFilter); | |
480 } | |
481 return root; | |
482 } | |
483 } | |
484 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |