changeset 9167:cb3a91dc4e3b

Result-Tabs (for collisions)
author gernotbelger
date Wed, 20 Jun 2018 18:25:58 +0200
parents 3ead6937ae7c
children d2472018740c
files gwt-client/src/main/java/org/dive4elements/river/client/client/ui/TableDataPanel.java
diffstat 1 files changed, 69 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/TableDataPanel.java	Wed Jun 20 13:08:14 2018 +0200
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/TableDataPanel.java	Wed Jun 20 18:25:58 2018 +0200
@@ -8,6 +8,7 @@
 
 package org.dive4elements.river.client.client.ui;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import org.dive4elements.river.client.client.Config;
@@ -19,13 +20,15 @@
 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.grid.ListGrid;
 import com.smartgwt.client.widgets.grid.ListGridField;
-import com.smartgwt.client.widgets.grid.ListGridRecord;
 import com.smartgwt.client.widgets.layout.VLayout;
+import com.smartgwt.client.widgets.tab.Tab;
+import com.smartgwt.client.widgets.tab.TabSet;
 
 /**
  * This UIProvider creates a widget that displays calculated data in a table.
@@ -50,12 +53,15 @@
     /** The table. */
     protected ListGrid dataTable;
 
+    private final TabSet tabSet;
+
     /**
      * Creates a new TableDataPanel instance.
      */
     public TableDataPanel() {
         this.container = new VLayout();
         this.dataTable = new ListGrid();
+        this.tabSet = new TabSet();
         this.name = "";
     }
 
@@ -84,7 +90,6 @@
                 setData(l);
             }
         });
-
         this.container.addMember(this.dataTable);
 
         return this.container;
@@ -112,6 +117,7 @@
      * @param list
      *            List of String[] containing the data.
      */
+
     public void setData(final List<String[]> list) {
         if (list == null || list.size() < 2) {
             this.dataTable.setEmptyMessage(this.MESSAGES.error_no_calc_result());
@@ -128,6 +134,32 @@
         } else {
             nf = NumberFormat.getFormat("#.##");
         }
+        final List<List<String[]>> tabData = getTabData(list);
+        if (tabData.size() == 1) {
+            createTable(this.dataTable, tabData.get(0), nf, 0);
+
+        } else if (tabData.size() > 1) {
+
+            for (int i = 0; i < tabData.size(); i++) {
+                final List<String[]> items = tabData.get(i);
+
+                final ListGrid table = new ListGrid();
+
+                createTable(table, items, nf, i);
+                final Tab tab = new Tab();
+                tab.setTitle("Ergebnis: " + i); // TODO: get Title from Server
+                tab.setPane(table);
+                this.tabSet.addTab(tab);
+            }
+            this.tabSet.selectTab(0);
+            this.container.removeChild(this.dataTable);
+            this.container.addChild(this.tabSet);
+            this.tabSet.setWidth100();
+            this.tabSet.setHeight100();
+        }
+    }
+
+    private void createTable(final ListGrid dataTableTab, final List<String[]> list, final NumberFormat nf, final int tabNumber) {
 
         final String[] header = list.get(0);
         final String[] displayField = new String[header.length];
@@ -142,12 +174,13 @@
             try {
                 /*
                  * Try to determine the type with the first
-                 * non empty element.
+                 * non empty element. -> WARUM??! OK, nur Formatierung/Sortierung
                  */
                 for (int j = 1; j < list.size(); j++) {
                     if (!list.get(j)[i].isEmpty()) {
                         nf.parse(list.get(j)[i]);
                         f.setType(ListGridFieldType.FLOAT);
+
                         break;
                     }
                 }
@@ -160,34 +193,60 @@
             // float values, we will store the value once formatted 'as is'
             // to be displayed and once as e.g. float to allow functions like
             // sorting on it.
-            displayField[i] = i + "_displayField";
+            displayField[i] = "$" + i + "_displayField";
             f.setDisplayField(displayField[i]);
             f.setValueField(String.valueOf(i));
             f.setSortByDisplayField(false);
-        }
 
-        this.dataTable.setFields(fields);
+        }
+        dataTableTab.setFields(fields);
 
-        for (int i = 1; i < list.size(); i++) {
+        for (int i = 1; i < list.size(); i++) { // index bei 1, da erster Eintrag Header ist
+
             final String[] sItem = list.get(i);
-            final ListGridRecord r = new ListGridRecord();
+            final Record r = new Record();
             for (int j = 0; j < sItem.length; j++) {
                 // See above, display 'as is' from server, but keep value
                 // in machine-usable way (float), to allow numeric sorting.
                 r.setAttribute(displayField[j], sItem[j]);
+                // r.setAttribute(String.valueOf(j), sItem[j]);
                 if (fields[j].getType() == ListGridFieldType.TEXT) {
                     r.setAttribute(String.valueOf(j), sItem[j]);
                 } else {
                     try {
-                        r.setAttribute(String.valueOf(j), nf.parse(sItem[j]));
+                        final Float valueFloatSortable = (float) nf.parse(sItem[j]);
+                        r.setAttribute(String.valueOf(j), valueFloatSortable);
                     }
                     catch (final NumberFormatException nfe) {
                         r.setAttribute(String.valueOf(j), sItem[j]);
                     }
                 }
+
             }
-            this.dataTable.addData(r);
+
+            dataTableTab.addData(r);
         }
     }
+
+    private List<List<String[]>> getTabData(final List<String[]> raw) {
+        final List<List<String[]>> tabs = new ArrayList<List<String[]>>();
+        String[] compare = raw.get(0);
+        int lastIndex = 0;
+
+        for (int i = 0; i < raw.size(); i++) {
+            final String[] row = raw.get(i);
+            if (compare.length != row.length) {
+                tabs.add(raw.subList(lastIndex, i));
+                lastIndex = i;
+            }
+
+            if (i == (raw.size() - 1)) {
+                tabs.add(raw.subList(lastIndex, i + 1)); // toIndex exclusive, fromIndex inclusive
+            }
+            compare = row;
+        }
+
+        return tabs;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org