diff gwt-client/src/main/java/org/dive4elements/river/client/client/ui/uinfo/LoadSedimentHeightPanel.java @ 9066:b5d7a9d79837

uinfo.inundation_duration ui
author gernotbelger
date Fri, 11 May 2018 17:04:35 +0200
parents
children fc02833dbcc1
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/uinfo/LoadSedimentHeightPanel.java	Fri May 11 17:04:35 2018 +0200
@@ -0,0 +1,288 @@
+/* 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.uinfo;
+
+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.i18n.client.NumberFormat;
+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.Canvas;
+import com.smartgwt.client.widgets.Label;
+import com.smartgwt.client.widgets.form.DynamicForm;
+import com.smartgwt.client.widgets.form.fields.IntegerItem;
+import com.smartgwt.client.widgets.form.fields.StaticTextItem;
+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.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 LoadSedimentHeightPanel extends AbstractUIProvider {
+    private static final long serialVersionUID = 1L;
+
+    protected SedimentLoadInfoServiceAsync sedLoadInfoService = GWT.create(SedimentLoadInfoService.class); // TODO: find right datasource
+
+    private static final String datakey = "sedimentheight";
+    protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);
+
+    private TextItem inputItem;
+
+    private ListGrid sedLoadTable;
+
+    protected List<String> validInputs;
+
+    public LoadSedimentHeightPanel() {
+    }
+
+    @Override
+    public Canvas createOld(final DataList dataList) {
+        final List<Data> items = dataList.getAll();
+        final Data item = getData(items, datakey);
+        final DataItem[] alreadySetItems = item.getItems();
+
+        final String v1 = alreadySetItems[0].getStringValue().replace(" ", ", ");
+
+        final Label old = new Label(v1);
+        final HLayout layout = new HLayout();
+        layout.setWidth("400px");
+
+        final Label label = new Label(dataList.getLabel());
+        label.setWidth("200px");
+
+        final Canvas back = getBackButton(dataList.getState());
+
+        layout.addMember(label);
+        layout.addMember(old);
+        layout.addMember(back);
+
+        return layout;
+    }
+
+    @Override
+    public List<String> validate() {
+        return validateYears();
+    }
+
+    @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);
+
+        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("*");
+
+        final String baseUrl = GWT.getHostPageBaseURL();
+        final ListGridField pinFrom = new ListGridField("fromIcon", this.MESSAGES.selection());
+        pinFrom.setWidth(60);
+        pinFrom.setType(ListGridFieldType.ICON);
+        pinFrom.setCellIcon(baseUrl + this.MESSAGES.markerGreen());
+
+        pinFrom.addRecordClickHandler(new RecordClickHandler() {
+            @Override
+            public void onRecordClick(final RecordClickEvent e) {
+                final Record r = e.getRecord();
+                appendValue(r.getAttribute("date"));
+            }
+        });
+
+        this.sedLoadTable.setFields(pinFrom, date, descr);
+        return this.sedLoadTable;
+    }
+
+    public Canvas createWidget(final DataList data) {
+        final VLayout layout = new VLayout();
+
+        final Label title = new Label(data.get(0).getDescription());
+        title.setHeight("35px");
+
+        final DynamicForm form = new DynamicForm();
+        form.setNumCols(4);
+
+        final StaticTextItem textItem = new StaticTextItem("TEST");
+        textItem.setValue("cm");
+        textItem.setShowTitle(false);
+
+        this.inputItem = new IntegerItem(this.MSG.year());
+        this.inputItem.setShowTitle(false);
+        form.setFields(this.inputItem, textItem);
+
+        layout.addMember(title);
+        layout.addMember(form);
+
+        return layout;
+    }
+
+    @Override
+    protected Data[] getData() {
+        validateYears();
+        if (this.inputItem != null && !this.inputItem.getValueAsString().isEmpty()) {
+            final List<Data> data = new ArrayList<Data>();
+
+            final DataItem yearsdata = new DefaultDataItem(datakey, datakey, this.inputItem.getValueAsString().trim()); // DATA-STRING: years (in
+                                                                                                                        // uinfo.xml)
+            data.add(new DefaultData(datakey, null, null, new DataItem[] { yearsdata }));
+
+            return data.toArray(new Data[data.size()]);
+        }
+        return new Data[0];
+    }
+
+    protected List<String> validateYears() {
+        final List<String> errors = new ArrayList<String>();
+        final NumberFormat nf = NumberFormat.getDecimalFormat();
+
+        if (this.inputItem.getValueAsString() == null || this.inputItem.getValueAsString().trim().isEmpty()) {
+            errors.add(this.MESSAGES.empty_filter());
+            return errors;
+        }
+
+        final String sValue = this.inputItem.getValueAsString();// .trim().split(" ");
+        String filtered = "";
+        int goodValues = 0;
+
+        int value = 0;
+        try {
+            value = Integer.parseInt(sValue);
+        }
+        catch (final NumberFormatException e) {
+            errors.add(this.MESSAGES.wrongFormat() + ": " + sValue);
+
+        }
+        boolean isGood = false;
+        for (final String validYear : this.validInputs) {
+            /* No list contains for strings? */
+            if (sValue.equals(validYear)) {
+                isGood = true;
+                break;
+            }
+        }
+        if (!isGood) {
+            String tmp = this.MESSAGES.no_data_for_year();
+            tmp = tmp.replace("$1", sValue);
+            errors.add(tmp);
+            // continue;
+        }
+        goodValues++;
+        if (goodValues > 1) {
+            filtered += " " + Integer.toString(value);
+        } else {
+            filtered = Integer.toString(value);
+        }
+
+        return errors;
+    }
+
+    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.validInputs = 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(LoadSedimentHeightPanel.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.validInputs.add(rec.getDate());
+        }
+    }
+
+    protected void appendValue(final String year) { // SINGLE-Input
+        final String oldYears = this.inputItem.getValueAsString();
+        this.inputItem.setValue(year);
+    }
+}

http://dive4elements.wald.intevation.org