view gwt-client/src/main/java/org/dive4elements/river/client/client/ui/uinfo/SuperVegZonesTablePanel.java @ 9068:a5cf8d7bff3c

access uinfo.salx; tablePanel edit/non-edit merge
author gernotbelger
date Mon, 14 May 2018 18:24:35 +0200
parents
children 1ffd38826175
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */
package org.dive4elements.river.client.client.ui.uinfo;

import java.util.ArrayList;
import java.util.List;

import org.dive4elements.river.client.client.ui.AbstractUIProvider;
import org.dive4elements.river.client.shared.model.Data;
import org.dive4elements.river.client.shared.model.DataItem;
import org.dive4elements.river.client.shared.model.DataList;
import org.dive4elements.river.client.shared.model.DefaultData;
import org.dive4elements.river.client.shared.model.DefaultDataItem;

import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * @author Domenico Nardi Tironi
 *
 */
public abstract class SuperVegZonesTablePanel extends AbstractUIProvider {
    private static final long serialVersionUID = 1L;
    public static final String tableCellSeparator = "tableCellSeparator";
    public static final String tableRowSeparator = "tableRowSeparator";

    protected static final String datakey = "vegzones";

    protected final ListGrid elements = new ListGrid();
    protected TextItem vegzone;
    protected TextItem start;
    protected TextItem end;
    private ListGrid table;

    final protected VLayout root = new VLayout();
    final protected HLayout input = new HLayout();
    final protected VLayout tableLayout = new VLayout();

    public SuperVegZonesTablePanel() {

    }

    protected final Canvas createHelper() {
        this.table = new ListGrid();
        this.table.setShowHeaderContextMenu(false);
        this.table.setWidth100();
        this.table.setShowRecordComponents(true);
        this.table.setShowRecordComponentsByCell(true);
        this.table.setHeight100();
        this.table.setEmptyMessage(this.MSG.empty_table());
        this.table.setCanReorderFields(false);

        /* Input support pins */
        // final String baseUrl = GWT.getHostPageBaseURL();
        // final ListGridField pinFrom = new ListGridField("fromIcon", this.MSG.uinfo_vegetation_zones_from());
        // pinFrom.setWidth(300);
        // pinFrom.setType(ListGridFieldType.ICON);
        // pinFrom.setCellIcon(baseUrl + this.MSG.markerGreen());
        //
        // final ListGridField pinTo = new ListGridField("toIcon", this.MSG.uinfo_vegetation_zones_to());
        // pinTo.setType(ListGridFieldType.ICON);
        // pinTo.setWidth(300);
        // pinTo.setCellIcon(baseUrl + this.MSG.markerRed());
        //
        // pinFrom.addRecordClickHandler(new RecordClickHandler() {
        // @Override
        // public void onRecordClick(final RecordClickEvent e) {
        // final Record r = e.getRecord();
        // VegetationzonesTableEditPanel.this.vegzone.setValue(r.getAttribute("date")); // date??
        // }
        // });
        //
        // pinFrom.addRecordClickHandler(new RecordClickHandler() {
        // @Override
        // public void onRecordClick(final RecordClickEvent e) {
        // final Record r = e.getRecord();
        // VegetationzonesTableEditPanel.this.start.setValue(r.getAttribute("date"));
        // }
        // });
        //
        // pinTo.addRecordClickHandler(new RecordClickHandler() {
        // @Override
        // public void onRecordClick(final RecordClickEvent e) {
        // final Record r = e.getRecord();
        // VegetationzonesTableEditPanel.this.end.setValue(r.getAttribute("date"));
        // }
        // });
        //
        // final ListGridField date = new ListGridField("date", this.MSG.year());
        // date.setType(ListGridFieldType.TEXT);
        // date.setWidth(100);
        //
        // final ListGridField descr = new ListGridField("description", this.MSG.description());
        // descr.setType(ListGridFieldType.TEXT);
        // descr.setWidth("*");
        //
        // this.table.setFields(pinFrom, pinTo, date, descr);
        return this.table;
    }

    public abstract Canvas createWidget(final DataList data);

    protected final void addDataInit(final DataList data) {
        for (final Data dataItemContainer : data.getAll()) {
            if (dataItemContainer.getItems() != null) {
                for (final DataItem dataItem : dataItemContainer.getItems()) {
                    if (dataItem.getStringValue() != null && dataItem.getStringValue().contains(tableRowSeparator)) {

                        final String[] rows = dataItem.getStringValue().split(tableRowSeparator);
                        for (final String row : rows) {
                            this.elements.addData(createEntry(row));
                        }
                    }
                }
            }
        }
    }

    @Override
    public final Canvas create(final DataList data) {
        final VLayout layout = new VLayout();
        final Canvas helper = createHelper();
        this.helperContainer.addMember(helper);

        final Canvas submit = getNextButton();
        final Canvas widget = createWidget(data);

        layout.addMember(widget);
        layout.addMember(submit); // TODO: SUBMIT

        // fetchSedimentLoadData(); //TODO: feed from database...

        return layout;
    }

    @Override
    public Canvas createOld(final DataList dataList) {
        final HLayout layout = new HLayout();
        layout.setWidth("400px");
        final VLayout vLayout = new VLayout();
        vLayout.setWidth(130);
        final Label label = new Label(dataList.getLabel());
        label.setWidth("200px");
        label.setHeight(25);

        final List<Data> items = dataList.getAll();
        final Data str = getData(items, datakey);
        final DataItem[] strItems = str.getItems();

        final String[] entries = strItems[0].getLabel().split(VegetationzonesTablePanel.tableRowSeparator);
        for (final String entry : entries) {
            final String[] vals = entry.split(VegetationzonesTablePanel.tableCellSeparator);
            final Label dateLabel = new Label(vals[0] + " (" + vals[1] + "-" + vals[2] + ")");
            dateLabel.setHeight(20);
            vLayout.addMember(dateLabel);
        }
        final Canvas back = getBackButton(dataList.getState());
        layout.addMember(label);
        layout.addMember(vLayout);
        layout.addMember(back);

        return layout;
    }

    protected static final Data[] getDummyData() {
        final List<Data> data = new ArrayList<Data>();
        String d = "";
        d = d + "Zonaler Wald" + tableCellSeparator + "0" + tableCellSeparator + "5" + tableRowSeparator;
        d = d + "Hartholzaue, trocken" + tableCellSeparator + "5" + tableCellSeparator + "40" + tableRowSeparator;
        d = d + "Hartholzaue, feucht" + tableCellSeparator + "40" + tableCellSeparator + "80" + tableRowSeparator;
        d = d + "Silberweidenwald" + tableCellSeparator + "80" + tableCellSeparator + "140" + tableRowSeparator;
        d = d + "Weidengebüsch" + tableCellSeparator + "140" + tableCellSeparator + "200" + tableRowSeparator;
        d = d + "Uferröhricht" + tableCellSeparator + "200" + tableCellSeparator + "260" + tableRowSeparator;
        d = d + "Uferpioniere" + tableCellSeparator + "260" + tableCellSeparator + "320" + tableRowSeparator;
        d = d + "Vegetationslos" + tableCellSeparator + "320" + tableCellSeparator + "365" + tableRowSeparator;
        d = d + "Wasserfläche" + tableCellSeparator + "365" + tableCellSeparator + "365" + tableRowSeparator;

        final DataItem item = new DefaultDataItem(datakey, "entryDescription", d); // DATA-key
        data.add(new DefaultData(datakey, null, null, new DataItem[] { item }));
        return data.toArray(new Data[data.size()]);
    }

    @Override
    protected final Data[] getData() {
        final List<Data> data = new ArrayList<Data>();

        final ListGridRecord[] lgr = this.elements.getRecords();
        if (lgr.length == 0) {
            return getDummyData();// new Data[0]; // return getDummyData();
        }
        String d = "";
        for (final ListGridRecord element : lgr) {
            final Record r = element;
            d += r.getAttribute("vegzone") + VegetationzonesTablePanel.tableCellSeparator + r.getAttribute("from")
                    + VegetationzonesTablePanel.tableCellSeparator + r.getAttribute("to");
            d += VegetationzonesTablePanel.tableRowSeparator;
        }

        final DataItem item = new DefaultDataItem(datakey, null, d); // DATA-key
        data.add(new DefaultData(datakey, null, null, new DataItem[] { item }));
        return data.toArray(new Data[data.size()]);
    }

    public final ListGridRecord createEntry(final String row) {

        if (row.contains(tableCellSeparator)) {

            final String[] vals = row.split(tableCellSeparator);
            if (vals.length == 3) {
                final String vegzone = vals[0];
                final String from = vals[1];
                final String to = vals[2];

                if (vegzone == null || from == null || to == null) {
                    return null;
                }

                final ListGridRecord r = new ListGridRecord();
                r.setAttribute("vegzone", vegzone);
                r.setAttribute("from", from);
                r.setAttribute("to", to);
                return r;

            }

        }
        return null;
    }

}

http://dive4elements.wald.intevation.org