diff gwt-client/src/main/java/org/dive4elements/river/client/client/ui/sinfo/CollisionLoadEpochPanel.java @ 9063:b6919e3c2d86

work on uinfo-inundation_duration states
author gernotbelger
date Tue, 08 May 2018 15:21:23 +0200
parents
children 766890addcb2
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/sinfo/CollisionLoadEpochPanel.java	Tue May 08 15:21:23 2018 +0200
@@ -0,0 +1,383 @@
+/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
+ * Software engineering by Intevation GmbH
+ *
+ * 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.sinfo;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.dive4elements.river.client.client.Config;
+import org.dive4elements.river.client.client.FLYSConstants;
+import org.dive4elements.river.client.client.services.SedimentLoadInfoService;
+import org.dive4elements.river.client.client.services.SedimentLoadInfoServiceAsync;
+import org.dive4elements.river.client.client.ui.AbstractUIProvider;
+import org.dive4elements.river.client.shared.model.ArtifactDescription;
+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 org.dive4elements.river.client.shared.model.SedimentLoadInfoObject;
+import org.dive4elements.river.client.shared.model.SedimentLoadInfoRecord;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.smartgwt.client.data.Record;
+import com.smartgwt.client.types.ListGridFieldType;
+import com.smartgwt.client.util.SC;
+import com.smartgwt.client.widgets.Button;
+import com.smartgwt.client.widgets.Canvas;
+import com.smartgwt.client.widgets.Label;
+import com.smartgwt.client.widgets.events.ClickEvent;
+import com.smartgwt.client.widgets.events.ClickHandler;
+import com.smartgwt.client.widgets.form.DynamicForm;
+import com.smartgwt.client.widgets.form.fields.TextItem;
+import com.smartgwt.client.widgets.form.validator.IsIntegerValidator;
+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.grid.events.RecordClickEvent;
+import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
+import com.smartgwt.client.widgets.layout.HLayout;
+import com.smartgwt.client.widgets.layout.VLayout;
+
+public class CollisionLoadEpochPanel extends AbstractUIProvider {
+    protected SedimentLoadInfoServiceAsync sedLoadInfoService = GWT.create(SedimentLoadInfoService.class);
+
+    protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);
+
+    protected ListGrid elements;
+    private TextItem start;
+    private TextItem end;
+    private ListGrid sedLoadTable; // TODO: Datenquelle für Collision richtig wählen! Sediment komplett eliminieren
+
+    protected List<String> validYears;
+
+    public Canvas createWidget(final DataList data) {
+        final HLayout input = new HLayout();
+        final VLayout root = new VLayout();
+        final VLayout grid = new VLayout();
+        final VLayout intFields = new VLayout();
+        final Button add = new Button(this.MSG.add_date());
+        this.elements = new ListGrid();
+
+        final Label title = new Label(data.get(0).getDescription());
+        title.setHeight("25px");
+
+        final DynamicForm form = new DynamicForm();
+        form.setNumCols(4);
+        this.start = new TextItem(this.MSG.from());
+        this.start.setWidth(60);
+        this.start.setValidators(new IsIntegerValidator());
+        this.end = new TextItem(this.MSG.to());
+        this.end.setWidth(60);
+        this.end.setValidators(new IsIntegerValidator());
+        form.setFields(this.start, this.end);
+        add.addClickHandler(new ClickHandler() {
+            @Override
+            public void onClick(final ClickEvent ce) {
+                final String v1 = CollisionLoadEpochPanel.this.start.getValueAsString();
+                final String v2 = CollisionLoadEpochPanel.this.end.getValueAsString();
+                if (v1 == null || v2 == null) {
+                    return;
+                }
+                if (!isValidEpoch(v1, v2)) {
+                    return;
+                }
+                final ListGridRecord r = new ListGridRecord();
+                r.setAttribute("from", v1);
+                r.setAttribute("to", v2);
+                CollisionLoadEpochPanel.this.elements.addData(r);
+            }
+        });
+
+        final Label sel = new Label(this.MSG.select());
+        sel.setHeight(25);
+        this.elements.setWidth(185);
+        this.elements.setHeight(120);
+        this.elements.setShowHeaderContextMenu(false);
+        this.elements.setCanReorderFields(false);
+        this.elements.setCanSort(false);
+        this.elements.setCanEdit(false);
+        final ListGridField from = new ListGridField("from", this.MSG.from());
+        final ListGridField to = new ListGridField("to", this.MSG.to());
+        from.setWidth(70);
+        to.setWidth(70);
+
+        final ListGridField removeField = new ListGridField("_removeRecord", "Remove Record") {
+            {
+                setType(ListGridFieldType.ICON);
+                setIcon(GWT.getHostPageBaseURL() + CollisionLoadEpochPanel.this.MSG.removeFeature());
+                setCanEdit(false);
+                setCanFilter(false);
+                setCanSort(false);
+                setCanGroupBy(false);
+                setCanFreeze(false);
+                setWidth(25);
+            }
+        };
+
+        this.elements.addRecordClickHandler(new RecordClickHandler() {
+            @Override
+            public void onRecordClick(final RecordClickEvent event) {
+                // Just handle remove-clicks
+                if (!event.getField().getName().equals(removeField.getName())) {
+                    return;
+                }
+                event.getViewer().removeData(event.getRecord());
+            }
+        });
+
+        this.elements.setFields(from, to, removeField);
+
+        intFields.addMember(form);
+        intFields.addMember(add);
+        grid.addMember(sel);
+        grid.addMember(this.elements);
+        input.addMember(intFields);
+        input.addMember(grid);
+        root.addMember(title);
+        root.addMember(input);
+
+        return root;
+    }
+
+    @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, "epochs");
+        final DataItem[] strItems = str.getItems();
+
+        final String[] pairs = strItems[0].getLabel().split(";");
+        for (final String pair : pairs) {
+            final String[] vals = pair.split(",");
+            final Label dateLabel = new Label(vals[0] + " - " + vals[1]);
+            dateLabel.setHeight(20);
+            vLayout.addMember(dateLabel);
+        }
+        final Canvas back = getBackButton(dataList.getState());
+        layout.addMember(label);
+        layout.addMember(vLayout);
+        layout.addMember(back);
+
+        return layout;
+    }
+
+    @Override
+    public 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);
+
+        fetchSedimentLoadData();
+
+        return layout;
+    }
+
+    private Canvas createHelper() {
+        this.sedLoadTable = new ListGrid();
+        this.sedLoadTable.setShowHeaderContextMenu(false);
+        this.sedLoadTable.setWidth100();
+        this.sedLoadTable.setShowRecordComponents(true);
+        this.sedLoadTable.setShowRecordComponentsByCell(true);
+        this.sedLoadTable.setHeight100();
+        this.sedLoadTable.setEmptyMessage(this.MSG.empty_table());
+        this.sedLoadTable.setCanReorderFields(false);
+
+        /* Input support pins */
+        final String baseUrl = GWT.getHostPageBaseURL();
+        final ListGridField pinFrom = new ListGridField("fromIcon", this.MESSAGES.from());
+        pinFrom.setWidth(30);
+        pinFrom.setType(ListGridFieldType.ICON);
+        pinFrom.setCellIcon(baseUrl + this.MESSAGES.markerGreen());
+
+        final ListGridField pinTo = new ListGridField("toIcon", this.MESSAGES.to());
+        pinTo.setType(ListGridFieldType.ICON);
+        pinTo.setWidth(30);
+        pinTo.setCellIcon(baseUrl + this.MESSAGES.markerRed());
+
+        pinFrom.addRecordClickHandler(new RecordClickHandler() {
+            @Override
+            public void onRecordClick(final RecordClickEvent e) {
+                final Record r = e.getRecord();
+                CollisionLoadEpochPanel.this.start.setValue(r.getAttribute("date"));
+            }
+        });
+
+        pinTo.addRecordClickHandler(new RecordClickHandler() {
+            @Override
+            public void onRecordClick(final RecordClickEvent e) {
+                final Record r = e.getRecord();
+                CollisionLoadEpochPanel.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.sedLoadTable.setFields(pinFrom, pinTo, date, descr);
+        return this.sedLoadTable;
+    }
+
+    @Override
+    protected Data[] getData() {
+        final List<Data> data = new ArrayList<Data>();
+
+        final ListGridRecord[] lgr = this.elements.getRecords();
+        if (lgr.length == 0) {
+            return new Data[0];
+        }
+        String d = "";
+        for (final ListGridRecord element : lgr) {
+            final Record r = element;
+            d += r.getAttribute("from") + "," + r.getAttribute("to");
+            d += ";";
+        }
+
+        final DataItem item = new DefaultDataItem("epochs", null, d);
+        data.add(new DefaultData("epochs", null, null, new DataItem[] { item }));
+        return data.toArray(new Data[data.size()]);
+    }
+
+    protected void fetchSedimentLoadData() {
+        final Config config = Config.getInstance();
+        final String locale = config.getLocale();
+
+        final ArtifactDescription adescr = this.artifact.getArtifactDescription();
+        final DataList[] data = adescr.getOldData();
+
+        final double[] km = this.artifact.getArtifactDescription().getKMRange();
+        final String river = this.artifact.getArtifactDescription().getRiver();
+
+        String sq_ti_id = "";
+        this.validYears = new ArrayList<String>(data.length);
+        for (final DataList element : data) {
+            final Data str = getData(element.getAll(), "sq_ti_id");
+            if (str != null) {
+                final DataItem[] strItems = str.getItems();
+                sq_ti_id = strItems[0].getStringValue();
+                break;
+            }
+        }
+
+        if (sq_ti_id.isEmpty()) {
+            GWT.log("Failed to find sq time interval id in data.");
+        }
+
+        this.sedLoadInfoService.getSedimentLoadInfo(locale, river, "single", km[0], km[1], sq_ti_id, new AsyncCallback<SedimentLoadInfoObject[]>() {
+            @Override
+            public void onFailure(final Throwable caught) {
+                GWT.log("Could not recieve sediment load informations.");
+                SC.warn(CollisionLoadEpochPanel.this.MSG.getString(caught.getMessage()));
+            }
+
+            @Override
+            public void onSuccess(final SedimentLoadInfoObject[] sedLoad) {
+                final int num = sedLoad != null ? sedLoad.length : 0;
+                GWT.log("Recieved " + num + " sediment load informations.");
+
+                if (num == 0) {
+                    return;
+                }
+
+                addSedimentLoadInfo(sedLoad);
+            }
+        });
+    }
+
+    protected void addSedimentLoadInfo(final SedimentLoadInfoObject[] sedLoad) {
+        for (final SedimentLoadInfoObject sl : sedLoad) {
+            final SedimentLoadInfoRecord rec = new SedimentLoadInfoRecord(sl);
+            this.sedLoadTable.addData(rec);
+            this.validYears.add(rec.getDate());
+        }
+    }
+
+    /*
+     * Validate the epoch input. We do this here and not in an overridden
+     * validate method as we want to validate before an epoch is added
+     * to the list of epochs.
+     */
+    protected boolean isValidEpoch(final String y1, final String y2) {
+        // First check that both are integer
+        int iY1;
+        int iY2;
+        final List<String> errors = new ArrayList<String>();
+        try {
+            iY1 = Integer.parseInt(y1);
+        }
+        catch (final NumberFormatException e) {
+            errors.add(this.MESSAGES.wrongFormat() + ": " + y1);
+        }
+        try {
+            iY2 = Integer.parseInt(y2);
+        }
+        catch (final NumberFormatException e) {
+            errors.add(this.MESSAGES.wrongFormat() + ": " + y2);
+        }
+        if (!errors.isEmpty()) {
+            showErrors(errors);
+            return false;
+        }
+        boolean startIsGood = false;
+        boolean endIsGood = false;
+        for (final String validYear : this.validYears) {
+            if (startIsGood || y1.equals(validYear)) {
+                startIsGood = true;
+            }
+            if (endIsGood || y2.equals(validYear)) {
+                endIsGood = true;
+            }
+            if (startIsGood && endIsGood) {
+                break;
+            }
+            /*
+             * alternative check if data lies in between
+             * int aYear = Integer.parseInt(validYear);
+             * if (aYear >= iY1 && aYear <= iY2) {
+             * isGood = true;
+             * break;
+             * }
+             */
+        }
+        if (!startIsGood) {
+            String tmp = this.MESSAGES.no_data_for_year();
+            tmp = tmp.replace("$1", y1);
+            errors.add(tmp);
+        }
+        if (!endIsGood) {
+            String tmp = this.MESSAGES.no_data_for_year();
+            tmp = tmp.replace("$1", y2);
+            errors.add(tmp);
+        }
+        if (!errors.isEmpty()) {
+            showErrors(errors);
+            return false;
+        }
+        return true;
+    }
+}

http://dive4elements.wald.intevation.org