Mercurial > dive4elements > river
view flys-client/src/main/java/de/intevation/flys/client/client/ui/StyleEditorWindow.java @ 1482:cd8a146d29cd
Update chart if properties are changed successfully.
flys-client/trunk@3537 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Fri, 23 Dec 2011 13:38:42 +0000 |
parents | 2f525f54e429 |
children | bd6c8b06cb94 |
line wrap: on
line source
package de.intevation.flys.client.client.ui; import java.util.Arrays; import java.util.LinkedHashMap; import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.rpc.AsyncCallback; import com.smartgwt.client.widgets.Window; import com.smartgwt.client.widgets.layout.VLayout; import com.smartgwt.client.widgets.layout.HLayout; import com.smartgwt.client.widgets.Button; import com.smartgwt.client.widgets.form.DynamicForm; import com.smartgwt.client.widgets.form.fields.FormItem; import com.smartgwt.client.widgets.form.fields.CheckboxItem; import com.smartgwt.client.widgets.form.fields.ColorPickerItem; import com.smartgwt.client.widgets.form.fields.SelectItem; import com.smartgwt.client.widgets.form.fields.StaticTextItem; import com.smartgwt.client.widgets.events.ClickEvent; import com.smartgwt.client.widgets.events.ClickHandler; import com.smartgwt.client.widgets.form.events.ItemChangedEvent; import com.smartgwt.client.widgets.form.events.ItemChangedHandler; import com.smartgwt.client.types.Alignment; import de.intevation.flys.client.shared.model.Collection; import de.intevation.flys.client.shared.model.CollectionItemAttribute; import de.intevation.flys.client.shared.model.Style; import de.intevation.flys.client.shared.model.StyleSetting; import de.intevation.flys.client.shared.model.FacetRecord; import de.intevation.flys.client.shared.model.Theme; import de.intevation.flys.client.client.services.CollectionItemAttributeServiceAsync; import de.intevation.flys.client.client.services.CollectionItemAttributeService; import de.intevation.flys.client.client.ui.ThemePanel; import de.intevation.flys.client.client.FLYSConstants; import de.intevation.flys.client.client.Config; /** * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> */ public class StyleEditorWindow extends Window implements ClickHandler { /** The interface that provides i18n messages. */ protected FLYSConstants MSG = GWT.create(FLYSConstants.class); /** The collection. */ protected Collection collection; /** The parent ThemePanel. */ protected ThemePanel panel; /** The attributes. */ protected CollectionItemAttribute attributes; /** The selected facet. */ protected FacetRecord facet; /** Main layout. */ protected VLayout layout; /** The service used to set collection item attributes. */ protected CollectionItemAttributeServiceAsync itemAttributeService = GWT.create(CollectionItemAttributeService.class); /** * Setup editor dialog. * @param collection The collection the current theme belongs to. * @param attributes The collection attributes. * @param facet The selected facet. */ public StyleEditorWindow ( Collection collection, CollectionItemAttribute attributes, FacetRecord facet) { this.collection = collection; this.attributes = attributes; this.facet = facet; this.layout = new VLayout(); init(); initPanels(); } /** * Initialize the window and set the layout. */ protected void init() { setTitle(MSG.properties()); setCanDragReposition(true); setCanDragResize(true); layout.setMargin(10); layout.setWidth100(); layout.setHeight100(); } /** * Initialize the static window content like buttons and main layout. */ protected void initPanels() { HLayout buttons = new HLayout(); Button accept = new Button(MSG.label_ok()); Button cancel = new Button(MSG.label_cancel()); cancel.addClickHandler(this); accept.addClickHandler(new ClickHandler() { public void onClick(ClickEvent e) { saveStyle(); } }); buttons.addMember(accept); buttons.addMember(cancel); buttons.setAlign(Alignment.CENTER); buttons.setHeight(30); VLayout propGrid = createPropertyGrid(); layout.addMember(propGrid); layout.addMember(buttons); addItem(layout); setWidth(300); setHeight(410); } /** * Setter for the parent panel. * @param panel The panel. */ public void setThemePanel (ThemePanel panel) { this.panel = panel; } /** * this method is called when the user aborts theming. * @param event The event. */ public void onClick(ClickEvent event) { this.hide(); } /** * This method creates the property grid for available styling attributes. * @return The layout containing the UI elements. */ protected VLayout createPropertyGrid() { VLayout properties = new VLayout(); Theme theme = facet.getTheme(); Style style = attributes.getStyle(theme.getFacet(), theme.getIndex()); StaticTextItem name = new StaticTextItem("name", "Name"); name.setValue(facet.getName()); name.setTitleStyle("color:#000; width:120px"); name.setTitleAlign(Alignment.LEFT); name.setDisabled(true); name.setShowDisabled(false); DynamicForm form = new DynamicForm(); form.setFields(name); properties.addMember(form); // Done via array to keep the order. String[] sets = {"showlines", "showpoints", "linetype", "linesize", "linecolor", "font", "textstyle", "textsize", "textcolor", "textorientation", "backgroundcolor", "showbackground"}; for (String settingName: sets) { StyleSetting set = style.getSetting(settingName); if (set == null) { continue; } DynamicForm property = createPropertyUI( set.getDisplayName(), set.getName(), set.getType(), set.getDefaultValue()); properties.addMember(property); } // Add settings not in whitelist above. for (StyleSetting set: style.getSettings()) { if (Arrays.asList(sets).contains(set.getName()) || set == null) { continue; } DynamicForm property = createPropertyUI( set.getDisplayName(), set.getName(), set.getType(), set.getDefaultValue()); properties.addMember(property); } return properties; } /** * Create a property form. * @param dname The display name. * @param name The property name. * @param type The property type. * @param value The current value. * * @return The dynamic form for the attribute property. */ protected DynamicForm createPropertyUI( String dname, String name, String type, String value) { DynamicForm df = new DynamicForm(); FormItem f; if(type.equals("int")) { f = new SelectItem(name, MSG.getString(name)); if (name.equals("linesize")) { f = createLineSizeUI(f); } else if (name.equals("textsize")) { LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>(); valueMap.put("3", "3"); valueMap.put("5", "5"); valueMap.put("8", "8"); valueMap.put("10", "10"); valueMap.put("12", "12"); valueMap.put("14", "14"); valueMap.put("18", "18"); valueMap.put("24", "24"); f.setValueMap(valueMap); } f.setValue(value); } else if (type.equals("boolean")) { if(name.equals("textorientation")) { f = new SelectItem(name, MSG.getString(name)); LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>(); valueMap.put("true", "horizontal"); valueMap.put("false", "vertical"); f.setValueMap(valueMap); f.setValue(value); } else { CheckboxItem c = new CheckboxItem(name, MSG.getString(name)); if(value.equals("true")) { c.setValue(true); } else { c.setValue(false); } c.setLabelAsTitle(true); f = c; } } else if (type.equals("Color")) { ColorPickerItem c = new ColorPickerItem(name, MSG.getString(name)); c.setValue(rgbToHtml(value)); f = c; } else if (type.equals("Dash")) { f = new SelectItem(name, MSG.getString(name)); LinkedHashMap<String, String> valueIcons = new LinkedHashMap<String, String>(); f.setImageURLPrefix(GWT.getHostPageBaseURL() + "images/linestyle-dash-"); f.setImageURLSuffix(".png"); f.setValueIconHeight(20); f.setValueIconWidth(80); LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>(); valueMap.put("10", ""); valueMap.put("10,5", ""); valueMap.put("20,10", ""); valueMap.put("30,10", ""); valueMap.put("20,5,15,5", ""); valueIcons.put("10", "10"); valueIcons.put("10,5", "10-5"); valueIcons.put("20,10", "20-10"); valueIcons.put("30,10", "30-10"); valueIcons.put("20,5,15,5", "20-5-15-5"); f.setValueIcons(valueIcons); f.setValueMap(valueMap); f.setValue(value); } else if (type.equals("Font")) { f = new SelectItem(name, MSG.getString(name)); LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>(); valueMap.put("arial", "<span style='font-family:arial'>Arial</span>"); valueMap.put("courier", "<span style='font-family:courier'>Courier</span>"); valueMap.put("verdana", "<span style='font-family:verdana'>Verdana</span>"); valueMap.put("times", "<span style='font-family:times'>Times</span>"); f.setValueMap(valueMap); f.setValue(value); } else if (type.equals("Style")) { f = new SelectItem(name, MSG.getString(name)); LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>(); valueMap.put("standard", "<span style='font-style:normal'>Normal</span>"); valueMap.put("bold", "<span style='font-weight:bold'>Bold</span>"); valueMap.put("italic", "<span style='font-style:italic'>Italic</span>"); f.setValueMap(valueMap); f.setValue(value); } else { f = new FormItem(); } f.setTitleStyle("color:#000; width:120px"); f.setTitleAlign(Alignment.LEFT); df.setFields(f); df.addItemChangedHandler(new ItemChangedHandler() { public void onItemChanged(ItemChangedEvent e) { String name = e.getItem().getName(); String newValue = e.getNewValue().toString(); setNewValue(name, newValue); } }); return df; } protected FormItem createLineSizeUI(FormItem f) { LinkedHashMap<String, String> valueIcons = new LinkedHashMap<String, String>(); f.setImageURLPrefix(GWT.getHostPageBaseURL() + "images/linestyle-"); f.setImageURLSuffix("px.png"); f.setValueIconHeight(20); f.setValueIconWidth(80); LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>(); valueMap.put("1", ""); valueMap.put("2", ""); valueMap.put("3", ""); valueMap.put("4", ""); valueMap.put("5", ""); valueMap.put("6", ""); valueMap.put("7", ""); valueMap.put("8", ""); valueIcons.put("1", "1"); valueIcons.put("2", "2"); valueIcons.put("3", "3"); valueIcons.put("4", "4"); valueIcons.put("5", "5"); valueIcons.put("6", "6"); valueIcons.put("7", "7"); valueIcons.put("8", "8"); f.setValueIcons(valueIcons); f.setValueMap(valueMap); return f; } /** * Static method to convert a color from RGB to HTML notation. * @param rgb String in RGB notation. * * @return String in HTML notation. */ protected static String rgbToHtml(String rgb) { String[] parts = rgb.split(","); int values[] = new int[parts.length]; for (int i = 0; i < parts.length; i++) { parts[i] = parts[i].trim(); try { values[i] = Integer.parseInt(parts[i]); } catch(NumberFormatException nfe) { return "#000000"; } } String hex = "#"; for (int i = 0; i < values.length; i++) { if (values[i] < 16) { hex += "0"; } hex += Integer.toHexString(values[i]); } return hex; } /** * Static method to convert a color from HTML to RGB notation. * @param html String in HTML notation. * * @return String in RGB notation. */ protected static String htmlToRgb(String html) { if (!html.startsWith("#")) { return "0, 0, 0"; } int r = Integer.valueOf(html.substring(1, 3), 16); int g = Integer.valueOf(html.substring(3, 5), 16); int b = Integer.valueOf(html.substring(5, 7), 16); return r + ", " + g + ", " + b; } /** * Saves the current style attributes and requests a redraw. */ protected void saveStyle () { GWT.log("StyleEditorWindow.saveStyle()"); Config config = Config.getInstance(); String locale = config.getLocale(); itemAttributeService.setCollectionItemAttribute( this.collection, attributes.getArtifact(), locale, attributes, new AsyncCallback<Void>() { public void onFailure (Throwable caught) { GWT.log("Could not set Collection item attributes."); } public void onSuccess(Void v) { GWT.log("Successfully saved collection item attributes."); panel.requestRedraw(); } }); this.hide(); } /** * Sets a new value for an attribute. * @param name Attribute name. * @param value The new value. */ protected final void setNewValue(String name, String value) { Theme t = facet.getTheme(); Style s = attributes.getStyle(t.getFacet(), t.getIndex()); StyleSetting set = s.getSetting(name); if(name.indexOf("color") != -1) { value = htmlToRgb(value); } set.setDefaultValue(value); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :