view gwt-client/src/main/java/org/dive4elements/river/client/client/ui/uinfo/AbstractVegZonesTablePanel.java @ 9070:611a523fc42f

VegetationZoneAccessHelper, VegetationTablePanels verbessert
author gernotbelger
date Tue, 15 May 2018 18:04:36 +0200
parents gwt-client/src/main/java/org/dive4elements/river/client/client/ui/uinfo/SuperVegZonesTablePanel.java@1ffd38826175
children 41f4bc83aa7a
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.client.ui.PanelHelper;
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.types.Alignment;
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.ListGridField;
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 AbstractVegZonesTablePanel extends AbstractUIProvider {
    private static final long serialVersionUID = 1L;
    public static final String TABLE_CELL_SEPARATOR = "TABLE_CELL_SEPARATOR";
    public static final String TABLE_ROW_SEPARATOR = "TABLE_ROW_SEPARATOR";

    protected static final String datakey = "vegzones";

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

    protected ListGridField vegzoneField;// = new ListGridField("vegzone", this.MSG.uinfo_vegetation_zones_label());
    protected ListGridField fromField;// = new ListGridField("from", this.MSG.uinfo_vegetation_zones_from());
    protected ListGridField toField;// = new ListGridField("to", this.MSG.uinfo_vegetation_zones_to());

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

    protected final void createTable(final DataList data, final int width) {
        data.add(VegetationzonesTablePanel.getDummyData()); // TODO: GET REAL DATA!

        final Label title = new Label(data.get(0).getDescription());
        title.setHeight("35px"); // orig:25

        this.elements.setWidth(width); // 185
        this.elements.setHeight(300); //
        this.elements.setShowHeaderContextMenu(false);
        this.elements.setCanReorderFields(false);
        this.elements.setCanSort(false);
        this.elements.setCanEdit(false);
        this.vegzoneField = new ListGridField("vegzone", this.MSG.uinfo_vegetation_zones_label());
        this.fromField = new ListGridField("from", this.MSG.uinfo_vegetation_zones_from());
        this.toField = new ListGridField("to", this.MSG.uinfo_vegetation_zones_to());
        this.vegzoneField.setWidth(245);
        this.fromField.setWidth(80);
        this.toField.setWidth(80);
        this.fromField.setAlign(Alignment.RIGHT);
        this.toField.setAlign(Alignment.RIGHT);

        addDataInit(data);

        this.root.addMember(title);
        this.tableLayout.addMember(this.elements);
        this.root.addMember(this.input);
        this.root.addMember(this.tableLayout);
        this.root.addMember(PanelHelper.getSpacer(10));

    }

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

    private 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(TABLE_ROW_SEPARATOR)) {

                        final String[] rows = dataItem.getStringValue().split(TABLE_ROW_SEPARATOR);
                        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.TABLE_ROW_SEPARATOR);
        for (final String entry : entries) {
            final String[] vals = entry.split(VegetationzonesTablePanel.TABLE_CELL_SEPARATOR);
            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>();

        // TODO: move to messages
        final String d = "Zonaler Wald" + TABLE_CELL_SEPARATOR + "0" + TABLE_CELL_SEPARATOR + "5" + TABLE_ROW_SEPARATOR//
                + "Hartholzaue, trocken" + TABLE_CELL_SEPARATOR + "5" + TABLE_CELL_SEPARATOR + "40" + TABLE_ROW_SEPARATOR//
                + "Hartholzaue, feucht" + TABLE_CELL_SEPARATOR + "40" + TABLE_CELL_SEPARATOR + "80" + TABLE_ROW_SEPARATOR//
                + "Silberweidenwald" + TABLE_CELL_SEPARATOR + "80" + TABLE_CELL_SEPARATOR + "140" + TABLE_ROW_SEPARATOR//
                + "Weidengebüsch" + TABLE_CELL_SEPARATOR + "140" + TABLE_CELL_SEPARATOR + "200" + TABLE_ROW_SEPARATOR//
                + "Uferröhricht" + TABLE_CELL_SEPARATOR + "200" + TABLE_CELL_SEPARATOR + "260" + TABLE_ROW_SEPARATOR//
                + "Uferpioniere" + TABLE_CELL_SEPARATOR + "260" + TABLE_CELL_SEPARATOR + "320" + TABLE_ROW_SEPARATOR//
                + "Vegetationslos" + TABLE_CELL_SEPARATOR + "320" + TABLE_CELL_SEPARATOR + "365" + TABLE_ROW_SEPARATOR//
                + "Wasserfläche" + TABLE_CELL_SEPARATOR + "365" + TABLE_CELL_SEPARATOR + "365" + TABLE_ROW_SEPARATOR;//

        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.TABLE_CELL_SEPARATOR + r.getAttribute("from")
                    + VegetationzonesTablePanel.TABLE_CELL_SEPARATOR + r.getAttribute("to");
            d += VegetationzonesTablePanel.TABLE_ROW_SEPARATOR;
        }

        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(TABLE_CELL_SEPARATOR)) {

            final String[] vals = row.split(TABLE_CELL_SEPARATOR);
            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