Mercurial > dive4elements > river
view gwt-client/src/main/java/org/dive4elements/river/client/client/ui/uinfo/LoadSingleEpochPanel.java @ 9077:2b13de1b0897
moved single_year/multi_year common code to AbstractPanel
author | gernotbelger |
---|---|
date | Tue, 22 May 2018 15:32:00 +0200 |
parents | b5d7a9d79837 |
children |
line wrap: on
line source
/* 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.client.ui.PanelHelper; 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.Canvas; import com.smartgwt.client.widgets.Label; 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.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 LoadSingleEpochPanel extends AbstractUIProvider { private static final long serialVersionUID = 1L; 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; protected List<String> validYears; public Canvas createWidget(final DataList data) { // final HLayout input = new HLayout(); final VLayout root = new VLayout(); // final VLayout intFields = new VLayout(); 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.setValue(2000);// TODO: get minDate this.start.setCanEdit(false); this.start.setValidators(new IsIntegerValidator()); this.end = new TextItem(this.MSG.to()); this.end.setWidth(60); this.end.setValidators(new IsIntegerValidator()); this.end.setCanEdit(false); this.end.setValue(2005);// TODO: get maxDate form.setFields(this.start, this.end); // intFields.addMember(form); // input.addMember(intFields); root.addMember(title); root.addMember(PanelHelper.getSpacer(10)); root.addMember(form); // 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, "totalepoch"); 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(); // TODO: get minMax from right datasource 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(); LoadSingleEpochPanel.this.start.setValue("darf nicht geändert werden");// r.getAttribute("date")); } }); pinTo.addRecordClickHandler(new RecordClickHandler() { @Override public void onRecordClick(final RecordClickEvent e) { final Record r = e.getRecord(); LoadSingleEpochPanel.this.end.setValue("darf nicht geändert werden"); } }); 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 += ";"; // } d = this.start.getValueAsString() + "," + this.end.getValueAsString() + ";"; final DataItem item = new DefaultDataItem("totalepoch", null, d); data.add(new DefaultData("totalepoch", 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(LoadSingleEpochPanel.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; } }