comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/NaviChartOutputTab.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/chart/NaviChartOutputTab.java@821a02bbfb4e
children 172338b1407f
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.client.client.ui.chart;
2
3 import java.util.Map;
4 import java.util.HashMap;
5
6 import com.google.gwt.core.client.GWT;
7 import com.google.gwt.i18n.client.NumberFormat;
8
9 import com.smartgwt.client.types.Alignment;
10
11 import com.smartgwt.client.widgets.Button;
12 import com.smartgwt.client.widgets.Canvas;
13
14 import com.smartgwt.client.widgets.events.ClickEvent;
15 import com.smartgwt.client.widgets.events.ClickHandler;
16
17 import com.smartgwt.client.widgets.form.DynamicForm;
18
19 import com.smartgwt.client.widgets.form.fields.TextItem;
20
21 import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
22 import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
23
24 import com.smartgwt.client.widgets.layout.HLayout;
25 import com.smartgwt.client.widgets.layout.VLayout;
26
27 import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
28 import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;
29
30 import org.dive4elements.river.client.client.Config;
31
32 import org.dive4elements.river.client.client.ui.CollectionView;
33
34 import org.dive4elements.river.client.shared.model.Artifact;
35 import org.dive4elements.river.client.shared.model.Collection;
36 import org.dive4elements.river.client.shared.model.FixAnalysisArtifact;
37 import org.dive4elements.river.client.shared.model.FixFilter;
38 import org.dive4elements.river.client.shared.model.OutputMode;
39
40 import java.util.Date;
41
42
43 /**
44 * Tab representing and showing one Chart-output with a "navi" thing.
45 *
46 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
47 */
48 public class NaviChartOutputTab
49 extends ChartOutputTab
50 implements TabSelectedHandler
51 {
52 protected TextItem currentkm;
53
54 public NaviChartOutputTab(
55 String title,
56 Collection collection,
57 OutputMode mode,
58 CollectionView collectionView
59 ){
60 super(title, collection, mode, collectionView);
61 right.removeChild(chart);
62 right.addChild(createNaviChart());
63 collectionView.registerTabHandler(this);
64 }
65
66
67 protected Canvas createNaviChart() {
68 final Artifact art = collectionView.getArtifact();
69 VLayout root = new VLayout();
70 root.setWidth100();
71 root.setHeight100();
72
73 HLayout layout = new HLayout();
74 layout.setAlign(Alignment.CENTER);
75
76 DynamicForm form = new DynamicForm();
77 Button lower = new Button("<<");
78 lower.setWidth(30);
79 Button upper = new Button(">>");
80 upper.setWidth(30);
81 currentkm = new TextItem();
82 currentkm.setWidth(60);
83 currentkm.setShowTitle(false);
84
85 form.setFields(currentkm);
86 form.setWidth(60);
87
88 double fromKm;
89 double toKm;
90
91 if (art instanceof FixAnalysisArtifact) {
92 FixAnalysisArtifact fix = (FixAnalysisArtifact) art;
93 FixFilter fixFilter = fix.getFilter();
94 String s = fix.getArtifactDescription().getDataValueAsString("ld_step");
95 try {
96 double ds = Double.parseDouble(s);
97 collectionView.setSteps(ds);
98 }
99 catch(NumberFormatException nfe) {
100 collectionView.setSteps(100d);
101 }
102 fromKm = fixFilter.getFromKm();
103 toKm = fixFilter.getToKm();
104 }
105 else {
106 // Probably WINFOArtifact kind of artifact.
107 String ld_step =
108 art.getArtifactDescription().getDataValueAsString("ld_step");
109 try {
110 collectionView.setSteps(Double.valueOf(ld_step));
111 }
112 catch (Exception e) {
113 GWT.log("No ld_steps data or not parsable.");
114 return root;
115 }
116
117 double[] kmRange = art.getArtifactDescription().getKMRange();
118 if (kmRange == null || kmRange.length == 2) {
119 fromKm = kmRange[0];
120 toKm = kmRange[1];
121 }
122 else {
123 GWT.log("No KM range in description found.");
124 return root;
125 }
126 }
127
128 collectionView.setMinKm(fromKm);
129 collectionView.setMaxKm(toKm);
130
131 final NumberFormat nf = NumberFormat.getDecimalFormat();
132 if (collectionView.getCurrentKm() == -1d) {
133 try {
134 double d = Double.valueOf(fromKm);
135 currentkm.setValue(nf.format(d));
136 } catch (NumberFormatException e) {
137 currentkm.setValue(fromKm);
138 }
139 collectionView.setCurrentKm(fromKm);
140 }
141 else {
142 try {
143 double d = Double.valueOf(fromKm);
144 currentkm.setValue(nf.format(d));
145 } catch (NumberFormatException e) {
146 currentkm.setValue(fromKm);
147 }
148 currentkm.setValue(collectionView.getCurrentKm());
149 }
150
151 lower.addClickHandler(new ClickHandler() {
152 public void onClick(ClickEvent ce) {
153 tbarPanel.deselectControls();
154 updateChartDown();
155 try {
156 double d = Double.valueOf(collectionView.getCurrentKm());
157 currentkm.setValue(nf.format(d));
158 } catch (NumberFormatException e) {
159 currentkm.setValue(collectionView.getCurrentKm());
160 }
161 }
162 });
163
164 upper.addClickHandler(new ClickHandler() {
165 public void onClick(ClickEvent ce) {
166 tbarPanel.deselectControls();
167 updateChartUp();
168 try {
169 double d = Double.valueOf(collectionView.getCurrentKm());
170 currentkm.setValue(nf.format(d));
171 } catch (NumberFormatException e) {
172 currentkm.setValue(collectionView.getCurrentKm());
173 }
174 }
175 });
176
177 currentkm.addKeyPressHandler(new KeyPressHandler() {
178 public void onKeyPress(KeyPressEvent kpe) {
179 if (!kpe.getKeyName().equals("Enter")) {
180 return;
181 }
182 if(kpe.getItem().getValue() != null) {
183 tbarPanel.deselectControls();
184 try {
185 String s = kpe.getItem().getValue().toString();
186 double d;
187 try {
188 d = nf.parse(s);
189 currentkm.setValue(nf.format(d));
190 } catch (NumberFormatException e) {
191 d = -1d;
192 }
193 if (d <= collectionView.getMaxKm() &&
194 d >= collectionView.getMinKm()) {
195 collectionView.setCurrentKm(d);
196 tbarPanel.updateLinks();
197 if (right != null) {
198 updateChartPanel();
199 updateChartInfo();
200 }
201 }
202 }
203 catch(NumberFormatException nfe) {
204 // Do nothing.
205 }
206 }
207 }
208 });
209 layout.addMember(lower);
210 layout.addMember(form);
211 layout.addMember(upper);
212
213 root.addMember(chart);
214 root.addMember(layout);
215 return root;
216 }
217
218
219 /** Callback when km-up-button is clicked.
220 * Increases collectionViews KM and refreshes view. */
221 protected void updateChartUp() {
222 double currentKm = collectionView.getCurrentKm();
223 if (currentKm < collectionView.getMaxKm()) {
224 // Why this math?
225 double newVal = currentKm * 100;
226 newVal += (collectionView.getSteps() / 10);
227 collectionView.setCurrentKm((double)Math.round(newVal) / 100);
228 tbarPanel.updateLinks();
229 updateChartPanel();
230 updateChartInfo();
231 }
232 }
233
234 /** Callback when km-down-button is clicked.
235 * Decreases collectionViews KM and refreshes view. */
236 protected void updateChartDown() {
237 double currentKm = collectionView.getCurrentKm();
238 if (currentKm > collectionView.getMinKm()) {
239 // Why this math?
240 double newVal = currentKm * 100;
241 newVal -= (collectionView.getSteps() / 10);
242 collectionView.setCurrentKm((double)Math.round(newVal) / 100);
243 tbarPanel.updateLinks();
244 updateChartPanel();
245 updateChartInfo();
246 }
247
248 }
249
250 /**
251 * Returns the existing chart panel.
252 *
253 * @return the existing chart panel.
254 */
255 @Override
256 public Canvas getChartPanel() {
257 return chart;
258 }
259
260 /**
261 * Builds the URL that points to the chart image.
262 *
263 * @param width The width of the requested chart.
264 * @param height The height of the requested chart.
265 * @param xr Optional x range (used for zooming).
266 * @param yr Optional y range (used for zooming).
267 *
268 * @return the URL to the chart image.
269 */
270 @Override
271 protected String getImgUrl(int width, int height) {
272 Config config = Config.getInstance();
273
274 String imgUrl = GWT.getModuleBaseURL();
275 imgUrl += "chart";
276 imgUrl += "?uuid=" + collection.identifier();
277 imgUrl += "&type=" + mode.getName();
278 imgUrl += "&locale=" + config.getLocale();
279 imgUrl += "&timestamp=" + new Date().getTime();
280 imgUrl += "&width=" + Integer.toString(width);
281 imgUrl += "&height=" + Integer.toString(height - 40);
282
283 Number[] zoom = getZoomValues();
284
285 if (zoom != null) {
286 if (zoom[0].intValue() != 0 || zoom[1].intValue() != 1) {
287 // a zoom range of 0-1 means displaying the whole range. In such
288 // case we don't need to zoom.
289 imgUrl += "&minx=" + zoom[0];
290 imgUrl += "&maxx=" + zoom[1];
291 }
292
293 if (zoom[2].intValue() != 0 || zoom[3].intValue() != 1) {
294 // a zoom range of 0-1 means displaying the whole range. In such
295 // case we don't need to zoom.
296 imgUrl += "&miny=" + zoom[2];
297 imgUrl += "&maxy=" + zoom[3];
298 }
299 }
300
301 if (collectionView.getArtifact() instanceof FixAnalysisArtifact) {
302 if (collectionView.getCurrentKm() == -1) {
303 FixAnalysisArtifact fix =
304 (FixAnalysisArtifact) collectionView.getArtifact();
305 collectionView.setCurrentKm(fix.getFilter().getFromKm());
306 }
307 }
308 else if (collectionView.getCurrentKm() == -1) {
309 collectionView.setCurrentKm(collectionView.getArtifact().getArtifactDescription().getKMRange()[0]);
310 }
311 if (collectionView.getCurrentKm() != -1) {
312 imgUrl += "&currentKm=" + collectionView.getCurrentKm();
313 }
314
315 return imgUrl;
316 }
317
318 public void onTabSelected(TabSelectedEvent tse) {
319 if (this.equals(tse.getTab())) {
320 updateChartPanel();
321 updateChartInfo();
322 currentkm.setValue(collectionView.getCurrentKm());
323 }
324 }
325
326 @Override
327 public Map<String, String> getChartAttributes() {
328 Map<String, String> attr = new HashMap<String, String>();
329
330 attr = super.getChartAttributes();
331 attr.put("km", String.valueOf(collectionView.getCurrentKm()));
332
333 return attr;
334 }
335 }
336 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org