andre@8231: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde andre@8231: * Software engineering by Intevation GmbH andre@8231: * andre@8231: * This file is Free Software under the GNU AGPL (>=v3) andre@8231: * and comes with ABSOLUTELY NO WARRANTY! Check out the andre@8231: * documentation coming with Dive4Elements River for details. andre@8231: */ andre@8231: andre@8231: package org.dive4elements.river.client.client.ui.minfo; andre@8231: andre@8231: import java.util.ArrayList; andre@8231: import java.util.List; andre@8231: andre@8231: import com.google.gwt.core.client.GWT; andre@8231: import com.google.gwt.user.client.rpc.AsyncCallback; andre@8231: import com.smartgwt.client.data.Record; andre@8231: import com.smartgwt.client.types.ListGridFieldType; andre@8231: import com.smartgwt.client.types.SelectionAppearance; andre@8231: import com.smartgwt.client.types.SelectionStyle; andre@8231: import com.smartgwt.client.util.SC; andre@8231: import com.smartgwt.client.widgets.Canvas; andre@8231: import com.smartgwt.client.widgets.Label; andre@8231: import com.smartgwt.client.widgets.grid.ListGrid; andre@8231: import com.smartgwt.client.widgets.grid.ListGridField; andre@8231: import com.smartgwt.client.widgets.grid.ListGridRecord; andre@8231: import com.smartgwt.client.widgets.layout.HLayout; andre@8231: import com.smartgwt.client.widgets.layout.VLayout; andre@8231: andre@8231: import org.dive4elements.river.client.client.Config; andre@8231: import org.dive4elements.river.client.client.services.SedimentLoadInfoService; andre@8231: import org.dive4elements.river.client.client.services.SedimentLoadInfoServiceAsync; andre@8231: import org.dive4elements.river.client.client.ui.PeriodPanel; andre@8231: import org.dive4elements.river.client.shared.model.ArtifactDescription; andre@8231: import org.dive4elements.river.client.shared.model.Data; andre@8231: import org.dive4elements.river.client.shared.model.DataItem; andre@8231: import org.dive4elements.river.client.shared.model.DataList; andre@8231: import org.dive4elements.river.client.shared.model.DefaultData; andre@8231: import org.dive4elements.river.client.shared.model.DefaultDataItem; andre@8231: import org.dive4elements.river.client.shared.model.SedimentLoadInfoObject; andre@8231: import org.dive4elements.river.client.shared.model.SedimentLoadInfoRecord; andre@8231: andre@8231: /** Show input to select an official epoch. */ andre@8231: public class SedLoadSQTiPanel andre@8234: extends PeriodPanel andre@8231: { andre@8234: protected SedimentLoadInfoServiceAsync sedLoadInfoService = andre@8234: GWT.create(SedimentLoadInfoService.class); andre@8234: andre@8234: protected ListGrid sedLoadTable; andre@8234: andre@8234: /** Creates layout with title. */ andre@8234: public Canvas createWidget(DataList data) { andre@8234: VLayout root = new VLayout(); andre@8234: andre@8234: Label title = new Label(data.get(0).getDescription()); andre@8234: title.setHeight("25px"); andre@8234: andre@8234: root.addMember(title); andre@8234: andre@8234: return root; andre@8234: } andre@8234: andre@8234: /** Create layout for data entered previously. */ andre@8234: @Override andre@8234: public Canvas createOld(DataList dataList) { andre@8234: HLayout layout = new HLayout(); andre@8234: layout.setWidth("400px"); andre@8234: VLayout vLayout = new VLayout(); andre@8234: vLayout.setWidth(130); andre@8234: Label label = new Label(dataList.getLabel()); andre@8234: label.setWidth("200px"); andre@8234: label.setHeight(25); andre@8234: andre@8234: List items = dataList.getAll(); andre@8234: Data str = getData(items, "sq_ti_date"); andre@8234: DataItem[] strItems = str.getItems(); andre@8234: andre@8234: String dateString = strItems[0].getStringValue(); andre@8234: Label dateLabel = new Label(dateString); andre@8234: dateLabel.setHeight(20); andre@8234: vLayout.addMember(dateLabel); andre@8234: Canvas back = getBackButton(dataList.getState()); andre@8234: layout.addMember(label); andre@8234: layout.addMember(vLayout); andre@8234: layout.addMember(back); andre@8234: tom@8856: GWT.log("Old data: " + strItems[0].getDescription() tom@8856: + " label " + strItems[0].getLabel() tom@8856: + " value: " + strItems[0].getStringValue()); andre@8234: andre@8234: return layout; andre@8234: } andre@8234: andre@8234: public Canvas create(DataList data) { andre@8234: VLayout layout = new VLayout(); andre@8234: Canvas helper = createHelper(); andre@8234: this.helperContainer.addMember(helper); andre@8234: andre@8234: Canvas submit = getNextButton(); andre@8234: Canvas widget = createWidget(data); andre@8234: andre@8234: layout.addMember(widget); andre@8234: layout.addMember(submit); andre@8234: andre@8234: fetchSedimentLoadData(); andre@8234: andre@8234: return layout; andre@8234: } andre@8234: andre@8231: /** Creates the helper grid in which off epochs can be selected. */ andre@8231: protected Canvas createHelper() { andre@8231: sedLoadTable = new ListGrid(); andre@8231: sedLoadTable.setShowHeaderContextMenu(false); andre@8231: sedLoadTable.setWidth100(); andre@8231: sedLoadTable.setShowRecordComponents(true); andre@8231: sedLoadTable.setShowRecordComponentsByCell(true); andre@8231: sedLoadTable.setHeight100(); andre@8231: sedLoadTable.setEmptyMessage(MSG.empty_table()); andre@8231: sedLoadTable.setCanReorderFields(false); andre@8231: sedLoadTable.setSelectionAppearance(SelectionAppearance.CHECKBOX); andre@8231: sedLoadTable.setSelectionType(SelectionStyle.SINGLE); andre@8231: andre@8234: ListGridField date = new ListGridField("sq_ti_date", MSG.year()); andre@8231: date.setType(ListGridFieldType.TEXT); andre@8231: date.setWidth(100); andre@8231: andre@8231: sedLoadTable.setFields(date); andre@8231: return sedLoadTable; andre@8231: } andre@8231: andre@8231: /** Get data via listgrid selection. */ andre@8231: @Override andre@8231: public Data[] getData() { andre@8231: List data = new ArrayList(); andre@8231: andre@8231: ListGridRecord[] lgr = sedLoadTable.getSelectedRecords(); andre@8231: if (lgr.length == 0) { andre@8231: GWT.log("returning empty data."); andre@8231: return new Data[0]; andre@8231: } andre@8231: String d = ""; andre@8234: String desc = ""; andre@8231: for (int i = 0; i < lgr.length; i++) { andre@8231: /* Should only be one item as this is single selection */ andre@8231: Record r = (Record) lgr[i]; andre@8231: d = r.getAttribute("sq_ti_id"); andre@8234: desc = r.getAttribute("sq_ti_date"); andre@8234: GWT.log("Got attribute sq_ti_id : " + d + " desc: " + desc); andre@8231: } andre@8234: DataItem item = new DefaultDataItem("sq_ti_id", "this is ignored", d); tom@8856: DataItem dateLabel = new DefaultDataItem( tom@8856: "sq_ti_date", "this is ignored", desc); andre@8234: data.add(new DefaultData( andre@8234: "sq_ti_id", andre@8234: null, andre@8234: null, andre@8234: new DataItem[] { item })); andre@8234: data.add(new DefaultData( andre@8234: "sq_ti_date", andre@8234: null, andre@8234: null, andre@8234: new DataItem[] { dateLabel })); andre@8231: return data.toArray(new Data[data.size()]); andre@8231: } andre@8231: andre@8231: /** Fetch load info from service and populate table. */ andre@8231: protected void fetchSedimentLoadData() { andre@8231: Config config = Config.getInstance(); andre@8231: String locale = config.getLocale (); andre@8231: andre@8231: ArtifactDescription adescr = artifact.getArtifactDescription(); andre@8231: DataList[] data = adescr.getOldData(); andre@8231: andre@8231: double[] km = artifact.getArtifactDescription().getKMRange(); andre@8231: String river = artifact.getArtifactDescription().getRiver(); andre@8231: tom@8856: sedLoadInfoService.getSedimentLoadInfo( tom@8856: locale, river, "sq_time_intervals", km[0], km[1], "", andre@8231: new AsyncCallback() { andre@8231: public void onFailure(Throwable caught) { andre@8231: GWT.log("Could not receive sediment load informations."); andre@8712: SC.warn(MSG.getString(caught.getMessage())); andre@8231: } andre@8231: andre@8231: public void onSuccess(SedimentLoadInfoObject[] sedLoad) { andre@8231: int num = sedLoad != null ? sedLoad.length :0; andre@8231: GWT.log("Received " + num + " sediment load informations."); andre@8231: andre@8231: if (num == 0) { andre@8231: return; andre@8231: } andre@8231: andre@8231: addSedimentLoadInfo(sedLoad); andre@8231: } andre@8231: } andre@8231: ); andre@8231: } andre@8234: andre@8234: /** Add record to input helper listgrid. */ andre@8234: protected void addSedimentLoadInfo (SedimentLoadInfoObject[] sedLoad) { andre@8234: for(SedimentLoadInfoObject sl: sedLoad) { andre@8234: SedimentLoadInfoRecord rec = new SedimentLoadInfoRecord(sl); andre@8234: sedLoadTable.addData(rec); andre@8234: } andre@8234: if (sedLoad.length == 1) { andre@8234: /* Preselect lists with only one load. */ andre@8234: sedLoadTable.selectRecords(new int[] {0}); andre@8234: } andre@8234: } andre@8231: } andre@8231: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :