teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5993: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.client.ui; raimund@256: raimund@256: import com.google.gwt.core.client.GWT; christian@4184: import com.google.gwt.i18n.client.NumberFormat; raimund@256: import com.google.gwt.user.client.rpc.AsyncCallback; raimund@256: christian@4184: import com.smartgwt.client.types.ListGridFieldType; ingo@550: import com.smartgwt.client.util.SC; raimund@256: import com.smartgwt.client.widgets.Canvas; raimund@256: import com.smartgwt.client.widgets.grid.ListGrid; raimund@256: import com.smartgwt.client.widgets.grid.ListGridField; raimund@256: import com.smartgwt.client.widgets.grid.ListGridRecord; christian@4184: import com.smartgwt.client.widgets.layout.VLayout; raimund@256: teichmann@5835: import org.dive4elements.river.client.client.Config; teichmann@5835: import org.dive4elements.river.client.client.FLYSConstants; teichmann@5835: import org.dive4elements.river.client.client.services.CSVExportService; teichmann@5835: import org.dive4elements.river.client.client.services.CSVExportServiceAsync; teichmann@5835: import org.dive4elements.river.client.shared.model.DataList; raimund@256: christian@4184: import java.util.List; raimund@256: raimund@256: /** raimund@256: * This UIProvider creates a widget that displays calculated data in a table. raimund@256: * raimund@256: * @author Raimund Renkert raimund@256: */ raimund@256: public class TableDataPanel raimund@256: { felix@810: /** The message class that provides i18n strings. */ raimund@256: protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); raimund@256: raimund@256: protected CSVExportServiceAsync exportService = raimund@256: GWT.create(CSVExportService.class); raimund@256: felix@810: /** A container that will contain the location or the distance panel. */ raimund@256: protected VLayout container; raimund@256: felix@810: /** The export type. */ raimund@256: protected String name; raimund@256: felix@810: /** The UUID of the collection. */ raimund@256: protected String uuid; raimund@256: felix@810: /** The table. */ raimund@256: protected ListGrid dataTable; raimund@256: raimund@256: raimund@256: /** raimund@256: * Creates a new LocationDistancePanel instance. raimund@256: */ raimund@256: public TableDataPanel() { raimund@256: container = new VLayout(); raimund@256: dataTable = new ListGrid(); raimund@256: name = ""; raimund@256: } raimund@256: raimund@256: raimund@256: /** felix@810: * This method creates a widget that contains a table. raimund@256: * raimund@256: * @return a panel. raimund@256: */ raimund@256: public Canvas create() { raimund@256: Config config = Config.getInstance(); raimund@256: String locale = config.getLocale (); raimund@263: dataTable.setEmptyMessage(MESSAGES.empty_table()); raimund@284: dataTable.setShowHeaderContextMenu(false); raimund@879: dataTable.setCanDragSelectText(true); raimund@256: raimund@1425: exportService.getCSV(locale, uuid, name, ingo@550: new AsyncCallback>() { christian@4184: @Override raimund@256: public void onFailure(Throwable caught) { raimund@256: GWT.log("Could not recieve csv."); ingo@550: SC.warn(caught.getMessage()); raimund@256: } raimund@256: christian@4184: @Override ingo@550: public void onSuccess(List l) { raimund@256: GWT.log("Recieved csv with " + l.size() + " lines."); raimund@256: setData(l); raimund@256: } raimund@256: } raimund@256: ); raimund@256: raimund@256: container.addMember(dataTable); raimund@256: raimund@256: return container; raimund@256: } raimund@256: raimund@256: raimund@256: public void setName(String name) { raimund@256: this.name = name; raimund@256: } raimund@256: raimund@256: public void setUuid(String uuid) { raimund@256: this.uuid = uuid; raimund@256: } raimund@256: raimund@256: raimund@256: public Canvas createOld(DataList dataList) { raimund@256: return null; raimund@256: } raimund@256: raimund@256: raimund@256: protected Canvas createWidget(DataList data) { raimund@256: return null; raimund@256: } raimund@256: raimund@259: raimund@259: /** christian@4184: * This method sets the data to a dynamic table. raimund@259: * felix@7601: * @param list List of String[] containing the data. raimund@259: */ christian@4184: public void setData(List list) { ingo@1289: if (list == null || list.size() < 2) { ingo@1289: dataTable.setEmptyMessage(MESSAGES.error_no_calc_result()); ingo@1293: dataTable.redraw(); ingo@1289: return; ingo@1289: } ingo@1289: ingo@1486: Config config = Config.getInstance(); ingo@1486: String locale = config.getLocale(); ingo@1486: ingo@1486: NumberFormat nf; ingo@1486: if (locale.equals("de")) { ingo@1486: nf = NumberFormat.getFormat("#,##"); ingo@1486: } ingo@1486: else { ingo@1486: nf = NumberFormat.getFormat("#.##"); ingo@1486: } ingo@1486: christian@4184: String[] header = list.get(0); christian@4184: String[] firstValues = list.get(1); felix@7602: String[] displayField = new String[header.length]; ingo@1289: raimund@259: ListGridField[] fields = new ListGridField[header.length]; ingo@1486: raimund@259: for(int i = 0; i < header.length; i++) { raimund@259: ListGridField f = new ListGridField(String.valueOf(i)); raimund@259: fields[i] = f; raimund@259: f.setTitle(header[i]); raimund@906: raimund@906: try { raimund@906: nf.parse(firstValues[i]); raimund@906: f.setType(ListGridFieldType.FLOAT); raimund@906: } raimund@906: catch (NumberFormatException nfe) { raimund@906: f.setType(ListGridFieldType.TEXT); raimund@906: } felix@7602: // To keep server-side formatting and i18n also of felix@7602: // float values, we will store the value once formatted 'as is' felix@7602: // to be displayed and once as e.g. float to allow functions like felix@7602: // sorting on it. felix@7602: displayField[i] = i + "_displayField"; felix@7602: f.setDisplayField(displayField[i]); felix@7602: f.setValueField(String.valueOf(i)); felix@7602: f.setSortByDisplayField(false); raimund@259: } raimund@259: ingo@1486: dataTable.setFields(fields); raimund@259: raimund@259: for(int i = 1; i < list.size(); i++) { christian@4184: String[] sItem = list.get(i); raimund@256: ListGridRecord r = new ListGridRecord(); raimund@259: for(int j = 0; j < sItem.length; j++) { felix@7602: // See above, display 'as is' from server, but keep value felix@7602: // in machine-usable way (float), to allow numeric sorting. felix@7602: r.setAttribute(displayField[j], sItem[j]); felix@7602: if (fields[j].getType() == ListGridFieldType.TEXT) { felix@7602: r.setAttribute(String.valueOf(j), sItem[j]); felix@7602: } felix@7602: else { felix@7602: r.setAttribute(String.valueOf(j), nf.parse(sItem[j])); felix@7602: } raimund@256: } raimund@256: dataTable.addData(r); raimund@256: } raimund@256: } raimund@256: } raimund@256: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :