view flys-client/src/main/java/de/intevation/flys/client/client/ui/StyleEditorWindow.java @ 1338:c730f0438510

Set window position to page center. flys-client/trunk@2983 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Mon, 17 Oct 2011 10:22:19 +0000
parents 9bf72f84728d
children d935fd42ae0e
line wrap: on
line source
package de.intevation.flys.client.client.ui;

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.SpinnerItem;
import com.smartgwt.client.widgets.form.fields.ColorPickerItem;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
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);


    public StyleEditorWindow (
        Collection collection,
        CollectionItemAttribute attributes,
        FacetRecord facet)
    {
        this.collection = collection;
        this.attributes = attributes;
        this.facet = facet;
        this.layout = new VLayout();

        init();
        initPanels();
    }


    protected void init() {
        setTitle(MSG.properties());
        setWidth(270);
        setHeight(200);
        setCanDragReposition(true);
        setCanDragResize(true);

        layout.setWidth100();
        layout.setHeight100();
    }


    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.RIGHT);

        layout.addMember(createPropertyGrid());;
        layout.addMember(buttons);
        addItem(layout);
    }


    public void setThemePanel (ThemePanel panel) {
        this.panel = panel;
    }


    public void onClick(ClickEvent event) {
        this.hide();
    }


    protected VLayout createPropertyGrid() {
        VLayout properties = new VLayout();

        Theme t = facet.getTheme();
        Style s = attributes.getStyle(t.getFacet(), t.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 f = new DynamicForm();
        f.setFields(name);
        properties.addMember(f);

        for (int i = 0; i < s.getNumSettings(); i ++) {
            final StyleSetting set = s.getSetting(i);
            DynamicForm property = createPropertyUI(
                set.getDisplayName(),
                set.getName(),
                set.getType(),
                set.getDefaultValue());
            properties.addMember(property);
        }
        return properties;
    }


    protected DynamicForm createPropertyUI(
        String dname,
        String name,
        String type,
        String value)
    {
        DynamicForm df = new DynamicForm();

        FormItem f;
        if(type.equals("int")) {
            SpinnerItem s = new SpinnerItem(name, dname);
            s.setMin(1);
            s.setMax(10);
            s.setValue(value);
            f = s;
        }
        else if (type.equals("boolean")) {
            CheckboxItem c = new CheckboxItem(name, dname);
            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, dname);
            c.setValue(rgbToHtml(value));
            f = c;
        }
        else if (type.equals("Dash")) {
            f = new ComboBoxItem(name, dname);
            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 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;
    }


    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;
    }


    protected void saveStyle () {
        GWT.log("StyleEditorWindow.saveStyle()");
        Config config = Config.getInstance();
        String url = config.getServerUrl();
        String locale = config.getLocale();

        itemAttributeService.setCollectionItemAttribute(
            this.collection,
            attributes.getArtifact(),
            url,
            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();
    }


    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.equals("linecolor")) {
            value = htmlToRgb(value);
        }
        set.setDefaultValue(value);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org