raimund@256: package de.intevation.flys.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:
christian@4184: import de.intevation.flys.client.client.Config;
christian@4184: import de.intevation.flys.client.client.FLYSConstants;
christian@4184: import de.intevation.flys.client.client.services.CSVExportService;
christian@4184: import de.intevation.flys.client.client.services.CSVExportServiceAsync;
raimund@256: import de.intevation.flys.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: *
raimund@259: * @param list List if 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);
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: }
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++) {
raimund@259: r.setAttribute(String.valueOf(j), sItem[j]);
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 :