view flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartThemePanel.java @ 854:67c678903280

Refactored to allow specialized controls within the ChartThemePanel, stubby first steps towards such a control (bound to FeedService). flys-client/trunk@2641 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 02 Sep 2011 13:20:06 +0000
parents f43d06d6a4a2
children 9f07f67f60a5
line wrap: on
line source
package de.intevation.flys.client.client.ui.chart;

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

import com.smartgwt.client.types.Alignment; 
import com.smartgwt.client.widgets.Canvas;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.form.fields.SpinnerItem;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;

import de.intevation.flys.client.client.Config;
import de.intevation.flys.client.shared.model.Artifact;
import de.intevation.flys.client.shared.model.Collection;
import de.intevation.flys.client.shared.model.Theme;
import de.intevation.flys.client.shared.model.OutputMode;

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

import de.intevation.flys.client.client.services.FeedService;
import de.intevation.flys.client.client.services.FeedServiceAsync;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class ChartThemePanel extends ThemePanel {

    /** The interface that provides i18n messages. */
    private FLYSConstants MSG = GWT.create(FLYSConstants.class);


    public static final String GRID_FIELD_ACTIVE  = "active";
    public static final String GRID_FIELD_NAME    = "name";
    public static final String GRID_FIELD_ACTIONS = "actions";

    FeedServiceAsync feedService = GWT.create(
        de.intevation.flys.client.client.services.FeedService.class);

    public ChartThemePanel(Collection collection, OutputMode mode) {
        super(collection, mode);

        initGrid();
        initLayout();

        updateGrid();
    }


    /**
     * Create and configure the Grid to display.
     */
    protected ListGrid createGrid() {
        ListGrid list = new ListGrid() {
            @Override
            protected Canvas createRecordComponent(final ListGridRecord record, Integer colNum) {
                    GWT.log("createRecordComponent! " + this.getFieldName(colNum));

                    String fieldName = this.getFieldName(colNum);

                    if (fieldName.equals("actions")) {
                        GWT.log("GG UU II: Put actopms");
                        HLayout recordCanvas = new HLayout(3);
                        recordCanvas.setHeight(22);
                        recordCanvas.setAlign(Alignment.CENTER);
                        // Grid Cell Widgets example
                        SpinnerItem spinnerItem = new SpinnerItem();
                        spinnerItem.setShowTitle(false);
                        spinnerItem.setTitle("Waterlevel-Spinner");
                        spinnerItem.setWidth(50);
                        spinnerItem.setDefaultValue(5);
                        spinnerItem.setMin(130);
                        spinnerItem.setMax(1000);
                        spinnerItem.setStep(5f);
                        Config config = Config.getInstance();
                        final String serverUrl = config.getServerUrl();
                        final String locale = config.getLocale();

                        spinnerItem.addChangedHandler(new ChangedHandler() {
                                @Override
                                public void onChanged(ChangedEvent ce) {
                                    // artifact instead of null
                                    // data[] instead of null
                                    feedService.feed(serverUrl, locale, null, null,
                                        new AsyncCallback<Artifact>() {
                                            public void onFailure(Throwable caught) {
                                                GWT.log("Could not feed artifact");
                                                // TODO SC.warn 
                                            }
                                            public void onSuccess(Artifact artifact) {
                                                GWT.log("Successfully fed");
                                                //TODO and now?
                                            }
                                        });
                                }
                            }
                        );

                        DynamicForm formWrap = new DynamicForm();
                        formWrap.setFields(spinnerItem);
                        formWrap.setTitlePrefix("");
                        formWrap.setTitleSuffix("");
                        recordCanvas.addMember(formWrap);
                        return recordCanvas;
                    }
                    else {
                        return null;
                    }
                }
            };
        list.setCanResizeFields(true);
        list.setShowRecordComponents(true);
        list.setShowRecordComponentsByCell(true);
        list.setShowAllRecords(true);
        return list;
    }


    /**
     * Initializes the layout of this panel.
     */
    protected void initLayout() {
        setWidth100();
        setHeight100();

        VLayout layout = new VLayout();
        layout.setWidth100();
        layout.setHeight100();

        layout.addMember(list);
        layout.addMember(navigation);

        addChild(layout);
    }


    /**
     * Initializes the components (columns) of the theme grid.
     */
    protected void initGrid() {
        list.setCanEdit(true);
        list.setCanSort(false);
        //list.setShowRecordComponents(false);
        list.setShowRecordComponentsByCell(true);
        list.setShowHeader(true);
        list.setShowHeaderContextMenu(false);
        list.setWidth100();
        list.setHeight100();

        list.addEditCompleteHandler(this);

        ListGridField active = new ListGridField(GRID_FIELD_ACTIVE, " ", 20);
        active.setType(ListGridFieldType.BOOLEAN);

        ListGridField name = new ListGridField(
            GRID_FIELD_NAME, MSG.chart_themepanel_header_themes());
        name.setType(ListGridFieldType.TEXT);

        // TODO Visibility of this Field (~"column") shall depend on
        // availability of facets allowing for actions.
        ListGridField actions = new ListGridField(GRID_FIELD_ACTIONS,
                GRID_FIELD_ACTIONS, 60);

        list.setFields(active, name, actions);
    }


    @Override
    public void activateTheme(Theme theme, boolean active) {
        theme.setActive(active ? 1 : 0);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org