comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/chart/ChartPropertiesEditor.java @ 5834:f507086aa94b

Repaired internal references.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:31:32 +0200
parents flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartPropertiesEditor.java@b296d435fc69
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
1 package de.intevation.flys.client.client.ui.chart;
2
3 import com.google.gwt.core.client.GWT;
4 import com.google.gwt.user.client.rpc.AsyncCallback;
5
6 import com.smartgwt.client.types.Alignment;
7 import com.smartgwt.client.util.SC;
8 import com.smartgwt.client.widgets.Button;
9 import com.smartgwt.client.widgets.Canvas;
10 import com.smartgwt.client.widgets.Label;
11 import com.smartgwt.client.widgets.Window;
12 import com.smartgwt.client.widgets.events.ClickEvent;
13 import com.smartgwt.client.widgets.events.ClickHandler;
14 import com.smartgwt.client.widgets.form.DynamicForm;
15 import com.smartgwt.client.widgets.form.fields.CheckboxItem;
16 import com.smartgwt.client.widgets.form.fields.FormItem;
17 import com.smartgwt.client.widgets.form.fields.SelectItem;
18 import com.smartgwt.client.widgets.form.fields.TextItem;
19 import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
20 import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
21 import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
22 import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
23 import com.smartgwt.client.widgets.layout.HLayout;
24 import com.smartgwt.client.widgets.layout.VLayout;
25 import com.smartgwt.client.widgets.tab.Tab;
26 import com.smartgwt.client.widgets.tab.TabSet;
27
28 import de.intevation.flys.client.client.Config;
29 import de.intevation.flys.client.client.FLYSConstants;
30 import de.intevation.flys.client.client.services.CollectionAttributeService;
31 import de.intevation.flys.client.client.services.CollectionAttributeServiceAsync;
32 import de.intevation.flys.client.client.utils.DoubleValidator;
33 import de.intevation.flys.client.client.utils.IntegerValidator;
34 import de.intevation.flys.client.shared.model.BooleanProperty;
35 import de.intevation.flys.client.shared.model.Collection;
36 import de.intevation.flys.client.shared.model.DoubleProperty;
37 import de.intevation.flys.client.shared.model.IntegerProperty;
38 import de.intevation.flys.client.shared.model.OutputSettings;
39 import de.intevation.flys.client.shared.model.Property;
40 import de.intevation.flys.client.shared.model.PropertyGroup;
41 import de.intevation.flys.client.shared.model.PropertySetting;
42 import de.intevation.flys.client.shared.model.Settings;
43 import de.intevation.flys.client.shared.model.StringProperty;
44
45 import java.util.LinkedHashMap;
46 import java.util.List;
47 import java.util.Map;
48
49 /**
50 * Dialog for the Chart-Properties, constructed from respective xml document.
51 *
52 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
53 */
54 public class ChartPropertiesEditor
55 extends Window
56 implements ClickHandler
57 {
58 /** The interface that provides i18n messages. */
59 protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
60
61 /** CollectionAttribute Update Service. */
62 protected CollectionAttributeServiceAsync updater =
63 GWT.create(CollectionAttributeService.class);
64
65 /** The tab called the editor window. */
66 protected ChartOutputTab tab;
67
68 /** The tabset for chart properties. */
69 protected TabSet tabs;
70
71 /** The collection. */
72 protected Collection collection;
73
74 /** The cloned output settings. */
75 protected OutputSettings settings;
76
77 /** The original output settings. */
78 protected OutputSettings origSettings;
79
80
81
82 /**
83 * Setup editor dialog.
84 * @param callerTab The tab called the editor window.
85 */
86 public ChartPropertiesEditor(ChartOutputTab callerTab) {
87 this.tab = callerTab;
88 this.tabs = new TabSet();
89
90 init();
91 }
92
93
94 /**
95 * Initialize the editor window and its components.
96 */
97 protected void init() {
98 setTitle(MSG.properties());
99 setCanDragReposition(true);
100 setCanDragResize(true);
101
102 collection = tab.getCollectionView().getCollection();
103 String outputName = tab.getOutputName();
104 origSettings = (OutputSettings)collection.getSettings(outputName);
105
106 settings = (OutputSettings)origSettings.clone();
107 if (settings == null) {
108 return;
109 }
110 List<String> list = settings.getCategories();
111
112 for (int i = 0; i < list.size(); i++) {
113 Tab t = new Tab(MSG.getString(list.get(i)));
114 List<Property> props = settings.getSettings(list.get(i));
115 List<Property> origProps = origSettings.getSettings(list.get(i));
116 VLayout layout = new VLayout();
117 for (int j = 0; j < props.size(); j++) {
118 if (props.get(j) instanceof PropertyGroup) {
119 layout.addMember(generatePropertyGroup(props.get(j),
120 origProps.get(j)));
121 }
122 else if (props.get(j) instanceof PropertySetting) {
123 PropertySetting p = (PropertySetting)props.get(j);
124 if (p.getAttribute("display").equals("false")) {
125 continue;
126 }
127 layout.addMember(generatePropertySetting(props.get(j),
128 origProps.get(j)));
129 }
130 }
131 t.setPane(layout);
132 tabs.addTab(t);
133 }
134
135 Button accept = new Button(MSG.label_ok());
136 Button cancel = new Button(MSG.label_cancel());
137 cancel.addClickHandler(this);
138 accept.addClickHandler(new ClickHandler() {
139 @Override
140 public void onClick(ClickEvent e) {
141 if(isDialogValid()) {
142 updateCollection();
143 }
144 else {
145 GWT.log("Dialog not valid");
146 SC.warn(MSG.error_dialog_not_valid());
147 }
148 }
149 });
150
151 HLayout buttons = new HLayout();
152 buttons.addMember(accept);
153 buttons.addMember(cancel);
154 buttons.setAlign(Alignment.CENTER);
155 buttons.setHeight(30);
156
157 addItem(tabs);
158 addItem(buttons);
159 setWidth(380);
160 setHeight(470);
161 centerInPage();
162 }
163
164
165 /**
166 * This method is called when the user aborts theming.
167 * @param event The event.
168 */
169 @Override
170 public void onClick(ClickEvent event) {
171 this.destroy();
172 }
173
174
175 /**
176 * Create a section from group (usually axis properties).
177 */
178 protected Canvas generatePropertyGroup(Property group, Property orig) {
179 PropertyGroup pg = (PropertyGroup)group;
180 PropertyGroup origPg = (PropertyGroup)orig;
181
182 if (pg.getName().equals("axis")) {
183 Label scale = new Label(MSG.scale() + " :");
184 scale.setHeight(25);
185 scale.setMargin(2);
186
187 DynamicForm form1 = new DynamicForm();
188 DynamicForm form2 = new DynamicForm();
189 form2.setNumCols(6);
190
191 StringProperty label =
192 (StringProperty)pg.getPropertyByName("label");
193 FormItem title = createStringProperty(label);
194 title.setValue(
195 ((StringProperty)origPg.getPropertyByName("label")).getValue());
196
197 IntegerProperty fontsize =
198 (IntegerProperty)pg.getPropertyByName("font-size");
199 FormItem fs = createIntegerProperty(fontsize);
200 fs.setValue(
201 ((IntegerProperty)
202 origPg.getPropertyByName("font-size")).getValue());
203
204 DoubleProperty upper =
205 (DoubleProperty)pg.getPropertyByName("upper");
206 final FormItem range1 = createDoubleProperty(upper);
207 range1.setName("rangeupper");
208 range1.setWidth(70);
209 range1.setValue(
210 ((DoubleProperty)
211 origPg.getPropertyByName("upper")).toUIString());
212
213 DoubleProperty lower =
214 (DoubleProperty)pg.getPropertyByName("lower");
215 final FormItem range2 = createDoubleProperty(lower);
216 range2.setName("rangelower");
217 range2.setWidth(70);
218 range2.setValue(
219 ((DoubleProperty)
220 origPg.getPropertyByName("lower")).toUIString());
221
222 BooleanProperty fixation =
223 (BooleanProperty)pg.getPropertyByName("fixation");
224 FormItem fix = createBooleanProperty(fixation);
225 fix.setValue(((BooleanProperty)
226 origPg.getPropertyByName("fixation")).getValue().booleanValue());
227
228 fix.addChangedHandler(new ChangedHandler() {
229 @Override
230 public void onChanged(ChangedEvent e) {
231 if ((Boolean)e.getValue()) {
232 range1.enable();
233 range2.enable();
234 }
235 else {
236 range1.disable();
237 range2.disable();
238 }
239 }
240 });
241 if (fix.getValue().toString().equals("true")) {
242 range1.enable();
243 range2.enable();
244 }
245 else {
246 range1.disable();
247 range2.disable();
248 }
249
250 form1.setFields(title, fs);
251 form2.setFields(fix, range2, range1);
252
253 HLayout scaleLayout = new HLayout();
254 scaleLayout.setHeight(30);
255 scaleLayout.addMember(scale);
256 scaleLayout.addMember(form2);
257 scaleLayout.setStyleName("property-dialog-axis");
258
259 VLayout root = new VLayout();
260 root.addMember(form1);
261 root.addMember(scaleLayout);
262 root.setHeight(90);
263
264 return root;
265 }
266 return null;
267 }
268
269
270 /**
271 * Generate a form with items for the properties/settings, preset with
272 * values.
273 */
274 protected DynamicForm generatePropertySetting(
275 Property setting,
276 Property orig)
277 {
278 DynamicForm form = new DynamicForm();
279 FormItem item = new FormItem();
280 if (setting instanceof BooleanProperty) {
281 item = createBooleanProperty((BooleanProperty)setting);
282 item.setValue(((BooleanProperty)orig).getValue().booleanValue());
283 }
284 else if (setting instanceof DoubleProperty) {
285 item = createDoubleProperty((DoubleProperty)setting);
286 item.setValue(((DoubleProperty)orig).toUIString());
287 }
288 else if (setting instanceof IntegerProperty) {
289 item = createIntegerProperty((IntegerProperty)setting);
290 item.setValue(((IntegerProperty)orig).getValue());
291 }
292 else if (setting instanceof StringProperty) {
293 StringProperty property = (StringProperty) setting;
294 item = createStringProperty(property);
295 item.setValue(((StringProperty)orig).getValue());
296 }
297 else {
298 GWT.log("generatePropertySetting: unknown setting type.");
299 }
300 form.setFields(item);
301 return form;
302 }
303
304
305 protected FormItem createStringProperty(final StringProperty sp) {
306 String name = sp.getName();
307 if (name.contains("-")) {
308 name = name.replace("-", "_");
309 }
310
311 String choiceAttribute = sp.getAttribute("choice");
312
313 if (choiceAttribute != null && choiceAttribute.equals("logo")) {
314 SelectItem logoChooser = new SelectItem();
315 logoChooser.setImageURLPrefix(GWT.getHostPageBaseURL() + "images/logo-");
316 logoChooser.setValueIconHeight(50);
317 logoChooser.setValueIconWidth(100);
318
319 LinkedHashMap valueMap = new LinkedHashMap<String, String>();
320 LinkedHashMap<String, String> valueIcons = new LinkedHashMap<String, String>();
321 valueMap.put("none", MSG.getString("none"));
322 /*
323 If you want to add images, remember to change code in these places:
324 flys-artifacts:
325 XYChartGenerator.java
326 Timeseries*Generator.java and
327 in the flys-client projects Chart*Propert*Editor.java.
328 Also, these images have to be put in
329 flys-artifacts/src/main/resources/images/
330 flys-client/src/main/webapp/images/
331 */
332 valueMap.put("BfG", "");
333 valueMap.put("Intevation", "");
334 valueIcons.put("BfG", "bfg.gif");
335 valueIcons.put("Intevation", "intevation.png");
336 logoChooser.setValueIcons(valueIcons);
337 logoChooser.setValueMap(valueMap);
338 logoChooser.setTitleStyle("color:#000;");
339 logoChooser.setTitleAlign(Alignment.LEFT);
340 logoChooser.setTitle(MSG.getString(name));
341 logoChooser.setTitleAlign(Alignment.LEFT);
342 logoChooser.addBlurHandler(new BlurHandler() {
343 @Override
344 public void onBlur(BlurEvent e) {
345 String val;
346 if (e.getItem().getValue() == null) {
347 val = "";
348 }
349 else {
350 val = e.getItem().getValue().toString();
351 }
352 sp.setValue(val);
353 }
354 });
355 return logoChooser;
356 }
357 else if (choiceAttribute != null && choiceAttribute.equals("placeh")) {
358 SelectItem placeChooser = new SelectItem();
359 LinkedHashMap valueMap = new LinkedHashMap<String, String>();
360 valueMap.put("right", MSG.getString("right"));
361 valueMap.put("left", MSG.getString("left"));
362 valueMap.put("center", MSG.getString("center"));
363 placeChooser.setValueMap(valueMap);
364 placeChooser.setTitleStyle("color:#000;");
365 placeChooser.setTitleAlign(Alignment.LEFT);
366 placeChooser.setTitle(MSG.getString(name));
367 placeChooser.setTitleAlign(Alignment.LEFT);
368 placeChooser.addBlurHandler(new BlurHandler() {
369 @Override
370 public void onBlur(BlurEvent e) {
371 String val;
372 if (e.getItem().getValue() == null) {
373 val = "";
374 }
375 else {
376 val = e.getItem().getValue().toString();
377 }
378 sp.setValue(val);
379 }
380 });
381 return placeChooser;
382 }
383 else if (choiceAttribute != null && choiceAttribute.equals("placev")) {
384 SelectItem placeChooser = new SelectItem();
385 LinkedHashMap valueMap = new LinkedHashMap<String, String>();
386 valueMap.put("top", MSG.getString("top"));
387 valueMap.put("bottom", MSG.getString("bottom"));
388 valueMap.put("center", MSG.getString("center"));
389 placeChooser.setValueMap(valueMap);
390 placeChooser.setTitleStyle("color:#000;");
391 placeChooser.setTitleAlign(Alignment.LEFT);
392 placeChooser.setTitle(MSG.getString(name));
393 placeChooser.setTitleAlign(Alignment.LEFT);
394 placeChooser.addBlurHandler(new BlurHandler() {
395 @Override
396 public void onBlur(BlurEvent e) {
397 String val;
398 if (e.getItem().getValue() == null) {
399 val = "";
400 }
401 else {
402 val = e.getItem().getValue().toString();
403 }
404 sp.setValue(val);
405 }
406 });
407 return placeChooser;
408 }
409
410 TextItem item = new TextItem();
411 item.setTitle(MSG.getString(name));
412 item.setTitleAlign(Alignment.LEFT);
413 item.addBlurHandler(new BlurHandler() {
414 @Override
415 public void onBlur(BlurEvent e) {
416 String val;
417 if (e.getItem().getValue() == null) {
418 val = "";
419 }
420 else {
421 val = e.getItem().getValue().toString();
422 }
423 sp.setValue(val);
424 }
425 });
426 return item;
427 }
428
429
430 /**
431 *
432 */
433 protected FormItem createBooleanProperty(final BooleanProperty bp) {
434 String name = bp.getName();
435 if (name.contains("-")) {
436 name = name.replace("-", "_");
437 }
438
439 CheckboxItem item = new CheckboxItem("item", MSG.getString(name));
440 item.setLabelAsTitle(true);
441 item.setTitleStyle("color:#000;");
442 item.setTitleAlign(Alignment.LEFT);
443 item.addBlurHandler(new BlurHandler() {
444 @Override
445 public void onBlur(BlurEvent e) {
446 String val;
447 if (e.getItem().getValue() == null) {
448 val = "";
449 }
450 else {
451 val = e.getItem().getValue().toString();
452 }
453 bp.setValue(val);
454 }
455 });
456 return item;
457 }
458
459
460 /**
461 *
462 */
463 protected FormItem createDoubleProperty(final DoubleProperty dp) {
464 String name = dp.getName();
465 if (name.contains("-")) {
466 name = name.replace("-", "_");
467 }
468
469 TextItem item = new TextItem();
470 item.setTitle(MSG.getString(name));
471 item.setTitleAlign(Alignment.LEFT);
472 item.addBlurHandler(new BlurHandler() {
473 @Override
474 public void onBlur(BlurEvent e) {
475 DoubleValidator validator = new DoubleValidator();
476 Map errors = e.getForm().getErrors();
477 if(validator.validate(e.getItem(), errors)) {
478 dp.setValueFromUI(e.getItem().getValue().toString());
479 }
480 e.getForm().setErrors(errors, true);
481 }
482 });
483 return item;
484 }
485
486
487 /**
488 *
489 */
490 protected FormItem createIntegerProperty(final IntegerProperty ip) {
491 String name = ip.getName();
492 if (name.contains("-")) {
493 name = name.replace("-", "_");
494 }
495
496 TextItem item = new TextItem();
497 item.setTitle(MSG.getString(name));
498 item.setTitleAlign(Alignment.LEFT);
499 item.addBlurHandler(new BlurHandler() {
500 @Override
501 public void onBlur(BlurEvent e) {
502 IntegerValidator validator = new IntegerValidator();
503 Map errors = e.getForm().getErrors();
504 if(validator.validate(e.getItem(), errors)) {
505 ip.setValue(e.getItem().getValue().toString());
506 }
507 e.getForm().setErrors(errors, true);
508 }
509 });
510 return item;
511 }
512
513
514 protected void updateCollection() {
515 final Config config = Config.getInstance();
516 final String loc = config.getLocale();
517
518 GWT.log("PropertiesEditor.updateCollection via RPC now");
519
520 Settings s = settings;
521 collection.addSettings(this.tab.getOutputName(), s);
522 updater.update(collection, loc, new AsyncCallback<Collection>() {
523 @Override
524 public void onFailure(Throwable caught) {
525 GWT.log("Could not update collection attributes.");
526 SC.warn(MSG.getString(caught.getMessage()));
527 }
528 @Override
529 public void onSuccess(Collection collection) {
530 updateChartTab();
531 }
532 });
533 }
534
535 protected void updateChartTab() {
536 this.tab.updateChartInfo();
537 this.tab.updateChartPanel();
538 this.destroy();
539 }
540
541
542 protected boolean isDialogValid() {
543 boolean valid = true;
544 for (int i = 0; i < tabs.getNumTabs(); i++) {
545 Tab t = tabs.getTab(i);
546 Canvas container = t.getPane();
547 Canvas[] children = container.getChildren();
548 for (Canvas c: children) {
549 valid = validateCanvas(c);
550 if(!valid) {
551 return valid;
552 }
553 }
554 }
555 return valid;
556 }
557
558
559 protected boolean validateCanvas(Canvas c) {
560 boolean valid = true;
561 if(c instanceof DynamicForm) {
562 DynamicForm f = (DynamicForm) c;
563 FormItem up = f.getItem("rangeupper");
564 FormItem lo = f.getItem("rangelower");
565
566 if(up != null && lo != null &&
567 !up.isDisabled() && !lo.isDisabled())
568 {
569 validateRange(f);
570 }
571 return !f.hasErrors();
572 }
573 else if(c.getChildren().length > 0) {
574 for (Canvas child: c.getChildren()) {
575 valid = validateCanvas(child);
576 if(!valid) {
577 return valid;
578 }
579 }
580 }
581 return valid;
582 }
583
584 protected boolean validateRange(DynamicForm form) {
585 Map errors = form.getErrors();
586 FormItem up = form.getItem("rangeupper");
587 FormItem lo = form.getItem("rangelower");
588
589 String v1 = up.getValue().toString();
590 String v2 = lo.getValue().toString();
591
592 if(v1.equals(v2)) {
593 errors.put(up.getFieldName(), MSG.wrongFormat());
594 errors.put(lo.getFieldName(), MSG.wrongFormat());
595 form.setErrors(errors, true);
596 return false;
597 }
598 return true;
599 }
600 }
601 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org