view flys-client/src/main/java/de/intevation/flys/client/client/ui/TableDataPanel.java @ 263:f56523bf4c55

Changed the empty table message for helper and data tables. flys-client/trunk@1883 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Tue, 10 May 2011 15:36:07 +0000
parents 50a95db68e66
children be842e36ce1c
line wrap: on
line source
package de.intevation.flys.client.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

import de.intevation.flys.client.shared.model.DataList;

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

import de.intevation.flys.client.client.services.CSVExportService;
import de.intevation.flys.client.client.services.CSVExportServiceAsync;

/**
 * This UIProvider creates a widget that displays calculated data in a table.
 *
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class TableDataPanel
{
    /** The message class that provides i18n strings.*/
    protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);

    /** The DistanceInfoService used to retrieve locations about rivers.*/
    protected CSVExportServiceAsync exportService =
        GWT.create(CSVExportService.class);

    /** A container that will contain the location or the distance panel.*/
    protected VLayout container;

    /** The export type.*/
    protected String name;

    /** The UUID of the collection */
    protected String uuid;

    /** The table*/
    protected ListGrid dataTable;


    /**
     * Creates a new LocationDistancePanel instance.
     */
    public TableDataPanel() {
        container = new VLayout();
        dataTable = new ListGrid();
        name      = "";
    }


    /**
     * This method creates a widget that contains a table
     *
     * @param data The data that might be inserted.//Use this?
     *
     * @return a panel.
     */
    public Canvas create() {
        Config config    = Config.getInstance();
        String url       = config.getServerUrl();
        String locale    = config.getLocale ();
        dataTable.setEmptyMessage(MESSAGES.empty_table());

        exportService.getCSV(url, locale, uuid, name,
            new AsyncCallback<List>() {
                public void onFailure(Throwable caught) {
                    GWT.log("Could not recieve csv.");
                    GWT.log(caught.getMessage());
                }

                public void onSuccess(List l) {
                    GWT.log("Recieved csv with " + l.size() + " lines.");
                    setData(l);
                }
            }
        );

        Label l = new Label (MESSAGES.calcTableTitle());
        l.setHeight(20);

        container.addMember(l);
        container.addMember(dataTable);

        return container;
    }


    public void setName(String name) {
      this.name = name;
    }

    public void setUuid(String uuid) {
      this.uuid = uuid;
    }


    public Canvas createOld(DataList dataList) {
        return null;
    }


    protected Canvas createWidget(DataList data) {
        return null;
    }


    /**
     * This method sets the data to a dynmic table.
     *
     * @param list List if String[] containing the data.
     */
    public void setData(List list) {
        String[] header = (String[])list.get(0);
        ListGridField[] fields = new ListGridField[header.length];
        for(int i = 0; i < header.length; i++) {
            ListGridField f = new ListGridField(String.valueOf(i));
            fields[i] = f;
            f.setTitle(header[i]);
        }

        if (header.length == 2) {
            dataTable.setFields(fields[0], fields[1]);
        }
        else if(header.length == 3) {
            dataTable.setFields(fields[0], fields[1], fields[2]);
        }
        else if(header.length == 4) {
            dataTable.setFields(fields[0], fields[1], fields[2], fields[3]);
        }
        else if(header.length == 5) {
            dataTable.setFields(
                fields[0],
                fields[1],
                fields[2],
                fields[3],
                fields[4]);
        }

        for(int i = 1; i < list.size(); i++) {
            String[] sItem = (String[])list.get(i);
            ListGridRecord r = new ListGridRecord();
            for(int j = 0; j < sItem.length; j++) {
                r.setAttribute(String.valueOf(j), sItem[j]);
            }
            dataTable.addData(r);
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org