comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/StyleEditorWindow.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/StyleEditorWindow.java@995320711d80
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
1 package de.intevation.flys.client.client.ui;
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.Window;
11 import com.smartgwt.client.widgets.events.ClickEvent;
12 import com.smartgwt.client.widgets.events.ClickHandler;
13 import com.smartgwt.client.widgets.form.DynamicForm;
14 import com.smartgwt.client.widgets.form.events.ItemChangedEvent;
15 import com.smartgwt.client.widgets.form.events.ItemChangedHandler;
16 import com.smartgwt.client.widgets.form.fields.CheckboxItem;
17 import com.smartgwt.client.widgets.form.fields.ColorPickerItem;
18 import com.smartgwt.client.widgets.form.fields.FormItem;
19 import com.smartgwt.client.widgets.form.fields.SelectItem;
20 import com.smartgwt.client.widgets.form.fields.StaticTextItem;
21 import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
22 import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
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.form.validator.IsFloatValidator;
26 import com.smartgwt.client.widgets.layout.HLayout;
27 import com.smartgwt.client.widgets.layout.VLayout;
28
29 import de.intevation.flys.client.client.Config;
30 import de.intevation.flys.client.client.FLYSConstants;
31 import de.intevation.flys.client.client.services.CollectionItemAttributeService;
32 import de.intevation.flys.client.client.services.CollectionItemAttributeServiceAsync;
33 import de.intevation.flys.client.client.services.ThemeListingService;
34 import de.intevation.flys.client.client.services.ThemeListingServiceAsync;
35 import de.intevation.flys.client.client.utils.DoubleValidator;
36 import de.intevation.flys.client.shared.model.Collection;
37 import de.intevation.flys.client.shared.model.CollectionItemAttribute;
38 import de.intevation.flys.client.shared.model.FacetRecord;
39 import de.intevation.flys.client.shared.model.Style;
40 import de.intevation.flys.client.shared.model.StyleSetting;
41 import de.intevation.flys.client.shared.model.Theme;
42
43 import java.util.Arrays;
44 import java.util.Iterator;
45 import java.util.LinkedHashMap;
46 import java.util.Map;
47 import java.util.Set;
48
49 /**
50 * Editor window for styles.
51 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
52 */
53 public class StyleEditorWindow
54 extends Window
55 implements ClickHandler
56 {
57 /** The interface that provides i18n messages. */
58 protected FLYSConstants MSG = GWT.create(FLYSConstants.class);
59
60 /** The collection. */
61 protected Collection collection;
62
63 /** The parent ThemePanel. */
64 protected ThemePanel panel;
65
66 /** The attributes. */
67 protected CollectionItemAttribute attributes;
68
69 /** The selected facet. */
70 protected FacetRecord facet;
71
72 /** Main layout. */
73 protected VLayout layout;
74
75 /** The form that contains all the input widgets. */
76 protected DynamicForm df;
77
78 protected VLayout properties;
79
80 protected Canvas container;
81
82 protected Map<String, Style> styleGroups;
83
84 protected Style current;
85
86 protected SelectItem styleChooser;
87
88 /** The service used to set collection item attributes. */
89 protected CollectionItemAttributeServiceAsync itemAttributeService =
90 GWT.create(CollectionItemAttributeService.class);
91
92 /** The service used to request a list of themes. */
93 protected ThemeListingServiceAsync themeListingService =
94 GWT.create(ThemeListingService.class);
95
96
97 /**
98 * Setup editor dialog.
99 * @param collection The collection the current theme belongs to.
100 * @param attributes The collection attributes.
101 * @param facet The selected facet.
102 */
103 public StyleEditorWindow (
104 Collection collection,
105 CollectionItemAttribute attributes,
106 FacetRecord facet)
107 {
108 this.collection = collection;
109 this.attributes = attributes;
110 this.facet = facet;
111 this.layout = new VLayout();
112 this.properties = new VLayout();
113 this.container = new Canvas();
114 this.styleChooser = new SelectItem("style", "Style");
115
116 styleChooser.setTitleStyle("color:#000;");
117 styleChooser.setTitleAlign(Alignment.LEFT);
118 styleChooser.setValue("aktuell");
119 styleChooser.addChangedHandler(new ChangedHandler() {
120 @Override
121 public void onChanged(ChangedEvent ce) {
122 String value = ce.getValue().toString();
123 Style s = null;
124 if (value.equals("aktuell")) {
125 s = current;
126 }
127 else if (styleGroups.containsKey(value)) {
128 s = styleGroups.get(value);
129 }
130
131 if (s != null) {
132 setNewStyle(s);
133 properties.removeMember(container);
134 container = createPropertyGrid(s);
135 properties.addMember(container);
136 }
137 }
138 });
139
140 DynamicForm f = new DynamicForm();
141 f.setFields(styleChooser);
142 f.setColWidths("40%", "60%");
143
144 layout.addMember(f);
145 init();
146 initPanels();
147 }
148
149
150 /**
151 * Initialize the window and set the layout.
152 */
153 protected void init() {
154 setTitle(MSG.properties());
155 setCanDragReposition(true);
156 setCanDragResize(true);
157 layout.setMargin(10);
158
159 layout.setWidth100();
160 layout.setHeight100();
161
162 Config config = Config.getInstance();
163 String locale = config.getLocale();
164
165 Theme theme = facet.getTheme();
166 Style style = attributes.getStyle(theme.getFacet(), theme.getIndex());
167 if(style == null) {
168 GWT.log("StyleEditorWindow.init(): style == null");
169 return;
170 }
171 String name = style.getName();
172 this.current = style;
173
174 themeListingService.list(
175 locale,
176 name,
177 new AsyncCallback<Map<String, Style> >() {
178 @Override
179 public void onFailure(Throwable caught) {
180 GWT.log("No listloaded.");
181 }
182 @Override
183 public void onSuccess(Map<String, Style> list) {
184 GWT.log("Successfully loaded list.");
185
186 styleGroups = list;
187 Set<String> keys = list.keySet();
188 LinkedHashMap<String, String> valueMap =
189 new LinkedHashMap<String, String>();
190 valueMap.put("aktuell", "Aktuell");
191 Iterator<String> iter = keys.iterator();
192 while (iter.hasNext()) {
193 String s = iter.next().toString();
194 Style tmp = styleGroups.get(s);
195 tmp.setFacet(current.getFacet());
196 tmp.setIndex(current.getIndex());
197 valueMap.put(s, s);
198 }
199 styleChooser.setValueMap(valueMap);
200 }
201 });
202 }
203
204
205 /**
206 * Initialize the static window content like buttons and main layout.
207 */
208 protected void initPanels() {
209 HLayout buttons = new HLayout();
210 Button accept = new Button(MSG.label_ok());
211 Button cancel = new Button(MSG.label_cancel());
212 cancel.addClickHandler(this);
213 accept.addClickHandler(new ClickHandler() {
214 @Override
215 public void onClick(ClickEvent e) {
216 // TODO Fix this, for whatever reason it doesnt work
217 // (always valid).
218 if (df == null) {
219 return;
220 }
221 if (!df.hasErrors() && df.validate()) {
222 saveStyle();
223 }
224 }
225 });
226
227 buttons.addMember(accept);
228 buttons.addMember(cancel);
229 buttons.setAlign(Alignment.CENTER);
230 buttons.setHeight(30);
231
232 Theme theme = facet.getTheme();
233 Style style = attributes.getStyle(theme.getFacet(), theme.getIndex());
234
235 container = createPropertyGrid(style);
236 properties.addMember(container);
237 layout.addMember(properties);
238 layout.addMember(buttons);
239 addItem(layout);
240 setWidth(400);
241 setHeight(410);
242 }
243
244
245 /**
246 * Setter for the parent panel.
247 * @param panel The panel.
248 */
249 public void setThemePanel (ThemePanel panel) {
250 this.panel = panel;
251 }
252
253
254 /**
255 * this method is called when the user aborts theming.
256 * @param event The event.
257 */
258 @Override
259 public void onClick(ClickEvent event) {
260 this.hide();
261 }
262
263
264 /**
265 * This method creates the property grid for available styling attributes.
266 * @return The layout containing the UI elements.
267 */
268 protected VLayout createPropertyGrid(Style style) {
269 VLayout vl = new VLayout();
270
271 StaticTextItem name = new StaticTextItem("name", "Name");
272 name.setValue(facet.getName());
273 name.setTitleStyle("color:#000;");
274 name.setTitleAlign(Alignment.LEFT);
275 name.setDisabled(true);
276 name.setShowDisabled(false);
277
278 DynamicForm form = new DynamicForm();
279 form.setFields(name);
280 form.setColWidths("40%", "60%");
281
282
283 vl.addMember(form);
284
285 if (style == null) {
286 SC.warn("No style found.");
287 return vl;
288 }
289
290 // Done via array to keep the order.
291 String[] sets = {"showlines",
292 "showpoints",
293 "linetype",
294 "linesize",
295 "linecolor",
296 "font",
297 "textstyle",
298 "textsize",
299 "pointcolor",
300 "pointsize",
301 "showpointlabel",
302 "textcolor",
303 "backgroundcolor",
304 "showbackground",
305 "showlinelabel",
306 "labelfontface",
307 "labelfontcolor",
308 "labelfontsize",
309 "labelfontstyle",
310 "textorientation",
311 "labelshowbg",
312 "labelbgcolor",
313 "bandwidth",
314 "bandwidthcolor",
315 "transparency",
316 "showminimum",
317 "showmaximum"};
318
319 for (String settingName: sets) {
320 StyleSetting set = style.getSetting(settingName);
321
322 if (set == null || set.isHidden()) {
323 continue;
324 }
325
326 DynamicForm property = createPropertyUI(
327 set.getDisplayName(),
328 set.getName(),
329 set.getType(),
330 set.getDefaultValue());
331 if (property != null) {
332 vl.addMember(property);
333 }
334 }
335
336 // Add settings not in whitelist above.
337 for (StyleSetting set: style.getSettings()) {
338
339 if (Arrays.asList(sets).contains(set.getName()) ||
340 set == null ||
341 set.isHidden()
342 ) {
343 continue;
344 }
345
346 DynamicForm property = createPropertyUI(
347 set.getDisplayName(),
348 set.getName(),
349 set.getType(),
350 set.getDefaultValue());
351 if (property != null) {
352 vl.addMember(property);
353 }
354 }
355
356 return vl;
357 }
358
359
360 /**
361 * Create a property form.
362 * @param dname The display name.
363 * @param name The property name.
364 * @param type The property type.
365 * @param value The current value.
366 *
367 * @return The dynamic form for the attribute property.
368 */
369 protected DynamicForm createPropertyUI(
370 String dname,
371 String name,
372 String type,
373 String value)
374 {
375 df = new DynamicForm();
376 df.setColWidths("40%", "60%");
377
378 FormItem f;
379 if(type.equals("int")) {
380 f = new SelectItem(name, MSG.getString(name));
381 if (name.equals("linesize")) {
382 f = createLineSizeUI(f);
383 f.setValue(value);
384 }
385 else if (name.equals("labelfontsize")) {
386 LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
387 valueMap.put("3", "3");
388 valueMap.put("5", "5");
389 valueMap.put("8", "8");
390 valueMap.put("10", "10");
391 valueMap.put("12", "12");
392 valueMap.put("14", "14");
393 valueMap.put("18", "18");
394 valueMap.put("24", "24");
395 f.setValueMap(valueMap);
396 f.setValue(value);
397 }
398 else if (name.equals("bandwidth")) {
399 LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
400 valueMap.put("0", "0");
401 valueMap.put("1", "1");
402 valueMap.put("2", "2");
403 valueMap.put("3", "3");
404 valueMap.put("4", "4");
405 valueMap.put("5", "5");
406 valueMap.put("6", "6");
407 valueMap.put("7", "7");
408 valueMap.put("8", "8");
409 valueMap.put("9", "9");
410 valueMap.put("10", "10");
411 valueMap.put("11", "11");
412 f.setValueMap(valueMap);
413 f.setValue(value);
414 }
415 else if (name.equals("pointsize")) {
416 LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
417 valueMap.put("1", "1");
418 valueMap.put("2", "2");
419 valueMap.put("3", "3");
420 valueMap.put("4", "4");
421 valueMap.put("5", "5");
422 valueMap.put("6", "6");
423 valueMap.put("7", "7");
424 f.setValueMap(valueMap);
425 f.setValue(value);
426 }
427 else if (name.equals("numclasses")) {
428 LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
429 valueMap.put("5", "5");
430 valueMap.put("6", "6");
431 valueMap.put("7", "7");
432 valueMap.put("8", "8");
433 valueMap.put("9", "9");
434 valueMap.put("10", "10");
435 valueMap.put("12", "12");
436 valueMap.put("14", "14");
437 valueMap.put("16", "16");
438 valueMap.put("18", "18");
439 valueMap.put("20", "20");
440 f.setValueMap(valueMap);
441 f.setValue(value);
442 // FIXME: Make that work again
443 return null;
444 }
445 else if (name.contains("transparency")) {
446 LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
447 for (int n = 10; n < 100; n += 10) {
448 valueMap.put(Integer.toString(n), n + "%");
449 }
450 f.setValueMap(valueMap);
451 f.setValue(value);
452 }
453 }
454 else if (type.equals("boolean")) {
455 if(name.equals("textorientation")) {
456 f = new SelectItem(name, MSG.getString(name));
457 LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
458 valueMap.put("true", MSG.getString("horizontal"));
459 valueMap.put("false", MSG.getString("vertical"));
460 f.setValueMap(valueMap);
461 f.setValue(value);
462 }
463 else {
464 CheckboxItem c = new CheckboxItem(name, MSG.getString(name));
465 if(value.equals("true")) {
466 c.setValue(true);
467 }
468 else {
469 c.setValue(false);
470 }
471 c.setLabelAsTitle(true);
472 f = c;
473 }
474 }
475 else if (type.equals("Color")) {
476 ColorPickerItem c = new ColorPickerItem(name, MSG.getString(name));
477 c.setValue(rgbToHtml(value));
478 f = c;
479 }
480 else if (type.equals("double")) {
481 f = new FormItem(name);
482 IsFloatValidator fpv = new IsFloatValidator();
483
484 f.setValidators(fpv);
485 f.setValidateOnChange(true);
486 f.setTitle(MSG.getString(name));
487 f.addBlurHandler(new BlurHandler() {
488 @Override
489 public void onBlur(BlurEvent e) {
490 DoubleValidator validator = new DoubleValidator();
491 Map<?, ?> errors = e.getForm().getErrors();
492 if(validator.validate(e.getItem(), errors)) {
493 e.getForm().setErrors(errors, true);
494 }
495 else {
496 e.getForm().setErrors(errors, true);
497 }
498 }
499 });
500 f.setValue(value);
501 }
502 else if (type.equals("Dash")) {
503 f = new SelectItem(name, MSG.getString(name));
504 LinkedHashMap<String, String> valueIcons = new LinkedHashMap<String, String>();
505 f.setImageURLPrefix(GWT.getHostPageBaseURL() + "images/linestyle-dash-");
506 f.setImageURLSuffix(".png");
507 f.setValueIconHeight(20);
508 f.setValueIconWidth(80);
509 LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
510 valueMap.put("10", "");
511 valueMap.put("10,5", "");
512 valueMap.put("20,10", "");
513 valueMap.put("30,10", "");
514 valueMap.put("20,5,15,5", "");
515 valueIcons.put("10", "10");
516 valueIcons.put("10,5", "10-5");
517 valueIcons.put("20,10", "20-10");
518 valueIcons.put("30,10", "30-10");
519 valueIcons.put("20,5,15,5", "20-5-15-5");
520 f.setValueIcons(valueIcons);
521 f.setValueMap(valueMap);
522 f.setValue(value);
523 }
524 else if (type.equals("Font")) {
525 f = new SelectItem(name, MSG.getString(name));
526 LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
527 valueMap.put("arial", "<span style='font-family:arial'>Arial</span>");
528 valueMap.put("courier", "<span style='font-family:courier'>Courier</span>");
529 valueMap.put("verdana", "<span style='font-family:verdana'>Verdana</span>");
530 valueMap.put("times", "<span style='font-family:times'>Times</span>");
531 f.setValueMap(valueMap);
532 f.setValue(value);
533 }
534 else if (type.equals("Style")) {
535 f = new SelectItem(name, MSG.getString(name));
536 LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
537 valueMap.put("standard", "<span style='font-style:normal'>Normal</span>");
538 valueMap.put("bold", "<span style='font-weight:bold'>Bold</span>");
539 valueMap.put("italic", "<span style='font-style:italic'>Italic</span>");
540 f.setValueMap(valueMap);
541 f.setValue(value);
542 }
543 else if (type.equals("Symbol")) {
544 //create an empty element as long as this property can not be
545 //changed.
546 f = new StaticTextItem("");
547 }
548 else {
549 f = new FormItem();
550 }
551 f.setTitleStyle("color:#000;");
552 f.setTitleAlign(Alignment.LEFT);
553 df.setFields(f);
554 df.addItemChangedHandler(new ItemChangedHandler() {
555 @Override
556 public void onItemChanged(ItemChangedEvent e) {
557 String name = e.getItem().getName();
558 String newValue = e.getNewValue().toString();
559 setNewValue(name, newValue);
560 }
561 });
562
563 return df;
564 }
565
566
567 protected FormItem createLineSizeUI(FormItem f) {
568 LinkedHashMap<String, String> valueIcons = new LinkedHashMap<String, String>();
569 f.setImageURLPrefix(GWT.getHostPageBaseURL() + "images/linestyle-");
570 f.setImageURLSuffix("px.png");
571 f.setValueIconHeight(20);
572 f.setValueIconWidth(80);
573 LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
574 valueMap.put("1", "");
575 valueMap.put("2", "");
576 valueMap.put("3", "");
577 valueMap.put("4", "");
578 valueMap.put("5", "");
579 valueMap.put("6", "");
580 valueMap.put("7", "");
581 valueMap.put("8", "");
582 valueIcons.put("1", "1");
583 valueIcons.put("2", "2");
584 valueIcons.put("3", "3");
585 valueIcons.put("4", "4");
586 valueIcons.put("5", "5");
587 valueIcons.put("6", "6");
588 valueIcons.put("7", "7");
589 valueIcons.put("8", "8");
590 f.setValueIcons(valueIcons);
591 f.setValueMap(valueMap);
592 return f;
593 }
594
595
596 /**
597 * Static method to convert a color from RGB to HTML notation.
598 * @param rgb String in RGB notation.
599 *
600 * @return String in HTML notation.
601 */
602 protected static String rgbToHtml(String rgb) {
603 String[] parts = rgb.split(",");
604 int values[] = new int[parts.length];
605 for (int i = 0; i < parts.length; i++) {
606 parts[i] = parts[i].trim();
607 try {
608 values[i] = Integer.parseInt(parts[i]);
609 }
610 catch(NumberFormatException nfe) {
611 return "#000000";
612 }
613 }
614 String hex = "#";
615 for (int i = 0; i < values.length; i++) {
616 if (values[i] < 16) {
617 hex += "0";
618 }
619 hex += Integer.toHexString(values[i]);
620 }
621 return hex;
622 }
623
624
625 /**
626 * Static method to convert a color from HTML to RGB notation.
627 * @param html String in HTML notation.
628 *
629 * @return String in RGB notation.
630 */
631 protected static String htmlToRgb(String html) {
632 if (!html.startsWith("#")) {
633 return "0, 0, 0";
634 }
635
636 int r = Integer.valueOf(html.substring(1, 3), 16);
637 int g = Integer.valueOf(html.substring(3, 5), 16);
638 int b = Integer.valueOf(html.substring(5, 7), 16);
639
640 return r + ", " + g + ", " + b;
641 }
642
643
644 /**
645 * Saves the current style attributes and requests a redraw.
646 */
647 protected void saveStyle () {
648 GWT.log("StyleEditorWindow.saveStyle()");
649 Config config = Config.getInstance();
650 String locale = config.getLocale();
651
652 itemAttributeService.setCollectionItemAttribute(
653 this.collection,
654 attributes.getArtifact(),
655 locale,
656 attributes,
657 new AsyncCallback<Void>() {
658 @Override
659 public void onFailure (Throwable caught) {
660 GWT.log("Could not set Collection item attributes.");
661 }
662 @Override
663 public void onSuccess(Void v) {
664 GWT.log("Successfully saved collection item attributes.");
665 panel.requestRedraw();
666 }
667 });
668
669
670 this.hide();
671 }
672
673
674 /**
675 * Sets a new value for an attribute.
676 * @param name Attribute name.
677 * @param value The new value.
678 */
679 protected final void setNewValue(String name, String value) {
680 Theme t = facet.getTheme();
681 Style s = attributes.getStyle(t.getFacet(), t.getIndex());
682 StyleSetting set = s.getSetting(name);
683 String type = set.getType();
684
685 if(name.indexOf("color") != -1
686 || (type != null && type.toLowerCase().indexOf("color") > -1)) {
687 value = htmlToRgb(value);
688 }
689 set.setDefaultValue(value);
690 }
691
692
693 protected final void setNewStyle(Style style) {
694 Theme t = facet.getTheme();
695 Style s = attributes.getStyle(t.getFacet(), t.getIndex());
696 attributes.removeStyle(s.getName());
697 attributes.appendStyle(style);
698 }
699 }
700 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org