view flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/StyleEditorWindow.java @ 1291:1d04f35b2cc0

New methods for color transformation (html to rgb; rgb to html). flys-client/trunk@2891 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 05 Oct 2011 09:26:13 +0000
parents f4deeafa60b3
children bdc270ea6195
line wrap: on
line source
package de.intevation.flys.client.client.ui.chart;

import com.google.gwt.core.client.GWT;

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.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
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.client.ui.CollectionView;

import de.intevation.flys.client.client.FLYSConstants;

/**
 * @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 collection view */
    protected CollectionView view;

    /** The attributes */
    protected CollectionItemAttribute attributes;

    /** Main layout */
    protected VLayout layout;


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

        init();
        initPanels();
    }


    protected void init() {
        setTitle(MSG.properties());
        setWidth(270);
        setHeight(200);
        setCanDragReposition(true);
        setCanDragResize(true);
        setKeepInParentRect(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) {

            }
        });

        buttons.addMember(accept);
        buttons.addMember(cancel);
        buttons.setAlign(Alignment.RIGHT);

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


    public void setCollectionView (CollectionView view) {
        this.view = view;
        setParentElement(this.view.getParentElement());
    }


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


    protected VLayout createPropertyGrid(String stylename) {
        //TODO use the style-(theme-)name from response to get the correct
        //attribute set.

        VLayout properties = new VLayout();

        // get the correct style using the name.
        Style s = attributes.getStyle(0);

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

        return df;
    }

    protected 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++) {
            hex += Integer.toHexString(values[i]);
        }
        return hex;
    }

    protected String htmlToRgb(String html) {
        if (!html.startsWith("#")) {
            return "0, 0, 0";
        }

        GWT.log("sub: " + html.substring(1, 3));
        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;
    }

}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org