view flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/CrossSectionChartThemePanel.java @ 891:f6c14ffdfd07

Rest for fix for flys/issue311 . flys-client/trunk@2733 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 14 Sep 2011 09:45:37 +0000
parents c87500c42c5d
children ebba8a8618e6
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.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.OutputMode;

import de.intevation.flys.client.shared.model.Data;
import de.intevation.flys.client.shared.model.DataItem;
import de.intevation.flys.client.shared.model.DefaultArtifact;
import de.intevation.flys.client.shared.model.DefaultData;
import de.intevation.flys.client.shared.model.DefaultDataItem;
import de.intevation.flys.client.shared.model.FacetRecord;


/**
 * ThemePanel much like ChartThemePanel, but shows an "Actions" column,
 * needed for interaction in the CrossSection Charts.
 */
public class CrossSectionChartThemePanel
extends      ChartThemePanel {

    /**
     * Trivial constructor.
     */
    public CrossSectionChartThemePanel(
            Collection collection,
            OutputMode mode)
    {
        super(collection, mode);
    }


    /**
     * Create and configure the Grid to display.
     */
    @Override
    protected ListGrid createGrid() {
        ListGrid list = new ListGrid() {
            @Override
            protected Canvas createRecordComponent(final ListGridRecord record, Integer colNum) {
                    // Only cross_section Facets display an action widget.
                    final FacetRecord facetRecord = (FacetRecord) record;
                    if (!facetRecord.getTheme().getFacet().equals(
                                "cross_section")) {
                        return null;
                    }

                    String fieldName = this.getFieldName(colNum);

                    if (fieldName.equals(GRID_FIELD_ACTIONS)) {
                        HLayout recordCanvas = new HLayout(3);
                        recordCanvas.setHeight(22);
                        recordCanvas.setAlign(Alignment.CENTER);
                        // TODO Refactor in createSpinner(data, artifact)
                        SpinnerItem spinnerItem = new SpinnerItem();
                        spinnerItem.setShowTitle(false);
                        spinnerItem.setTitle("Waterlevel-Spinner");
                        spinnerItem.setWidth(45);
                        // TODO actually get the value from artifact
                        // TODO actually get the range from artifact (or river?)
                        spinnerItem.setDefaultValue(0);
                        spinnerItem.setMin(0);
                        spinnerItem.setMax(1000);
                        spinnerItem.setStep(5f);
                        spinnerItem.setChangeOnKeypress(true);

                        Config config = Config.getInstance();
                        final String serverUrl = config.getServerUrl();
                        final String locale    = config.getLocale();

                        spinnerItem.addChangedHandler(new ChangedHandler() {
                                @Override
                                public void onChanged(ChangedEvent ce) {
                                    if (ce.getValue() == null) {
                                        return;
                                    }
                                    DefaultDataItem kmItem = new DefaultDataItem("cross_section.km",
                                        "cross_section.km", ce.getValue().toString());
                                    DefaultData km = new DefaultData("cross_section.km",
                                        null, null, new DataItem[] {kmItem});
                                    Data[] feedData = new Data[] {km};
                                    feedService.feed(serverUrl,
                                        locale,
                                        new DefaultArtifact(facetRecord.getTheme().getArtifact(), "TODO:hash"),
                                        feedData,
                                        new AsyncCallback<Artifact>() {
                                            public void onFailure(Throwable caught) {
                                                GWT.log("Could not feed artifact " + caught.getMessage());
                                                // TODO SC.warn 
                                            }
                                            public void onSuccess(Artifact artifact) {
                                                GWT.log("Successfully fed");
                                                //TODO and now?
                                                // fireOutputParameterChanged();
                                                // Also update content of spinnerbox
                                                requestRedraw();
                                            }
                                        });
                                }
                            }
                        );

                        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 components (columns) of the theme grid.
     */
    @Override
    protected void initGrid() {
        list.setCanEdit(true);
        list.setCanSort(false);
        list.setShowRecordComponents(true);
        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);

        ListGridField actions = new ListGridField(GRID_FIELD_ACTIONS,
             MSG.chart_themepanel_header_actions(), 50);

        list.setFields(active, name, actions);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org