changeset 9075:89740fe82196

Extract Table from Panel
author gernotbelger
date Fri, 18 May 2018 17:42:34 +0200
parents 766890addcb2
children 22d8928895a1
files gwt-client/src/main/java/org/dive4elements/river/client/client/ui/TableHelper.java gwt-client/src/main/java/org/dive4elements/river/client/client/ui/sinfo/CollisionLoadYearPanel.java
diffstat 2 files changed, 183 insertions(+), 222 deletions(-) [+]
line wrap: on
line diff
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/TableHelper.java	Fri May 18 17:26:26 2018 +0200
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/TableHelper.java	Fri May 18 17:42:34 2018 +0200
@@ -9,7 +9,29 @@
  */
 package org.dive4elements.river.client.client.ui;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+import org.dive4elements.river.client.client.FLYSConstants;
+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.MultiAttributeData;
+import org.dive4elements.river.client.shared.model.MultiDataItem;
+
+import com.google.gwt.core.client.GWT;
+import com.smartgwt.client.data.Record;
+import com.smartgwt.client.types.Alignment;
+import com.smartgwt.client.types.ListGridFieldType;
+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.events.RecordClickEvent;
+import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
 
 /**
  * @author Domenico Nardi Tironi
@@ -17,14 +39,161 @@
  */
 public class TableHelper {
 
+    public interface IColumnClickHandler {
+        void columnClicked(String value);
+    }
+
     private enum PinType {
         none, from, to
     }
 
-    private ListGrid table;
+    private final ListGrid table;
+    private final DataList dataList;
+    final FLYSConstants flys;
+    private final List<String> keyColEntries = new ArrayList<String>();
 
-    public TableHelper() {
+    public TableHelper(final DataList dataList, final String definitionStr, final FLYSConstants flys, final IColumnClickHandler fromHandler) {
+        this.flys = flys;
+        this.dataList = dataList;
+        this.table = new ListGrid();
+        this.table.setShowHeaderContextMenu(false);
+        this.table.setWidth100();
+        this.table.setShowRecordComponents(true);
+        this.table.setShowRecordComponentsByCell(true);
+        this.table.setHeight100();
+        this.table.setEmptyMessage(flys.empty_table());
+        this.table.setCanReorderFields(false);
+
+        final MultiAttributeData mData = findDefinition(definitionStr);
+        final Map<String, Map<String, String>> meta = mData.getMeta();
+
+        final Map<String, String> tableInfo = meta.get("meta_tableinfo");
+        final String keyColumnName = tableInfo.get("keycol");
+
+        final SortedMap<Integer, ListGridField> fields = new TreeMap<Integer, ListGridField>();
+
+        for (final Entry<String, Map<String, String>> entry : meta.entrySet()) {
+
+            final String colName = entry.getKey();
+            if (colName.startsWith("meta_"))
+                continue;
+
+            final Map<String, String> values = entry.getValue();
+
+            final int order = Integer.parseInt(values.get("order"));
+
+            final ListGridField field = createField(colName, keyColumnName, values, fromHandler);
+
+            fields.put(order, field);
+        }
+
+        for (final DataItem dataItem : mData.opts) {
+            final MultiDataItem item = (MultiDataItem) dataItem;
+
+            final Map<String, String> valueMap = item.getValue();
+            // final String columnName = mData.getLabel();
+            final Record newRecord = new Record();
+            for (final Entry<String, String> entry : valueMap.entrySet()) {
+                final String key = entry.getKey();
+                final String value = entry.getValue();
+                newRecord.setAttribute(key, value);
+
+                if (key.equals(keyColumnName))
+                    this.keyColEntries.add(value);
+            }
+
+            this.table.addData(newRecord);
+        }
+
+        this.table.setFields(fields.values().toArray(new ListGridField[fields.size()]));
 
     }
 
+    public Canvas getTable() {
+        return this.table;
+    }
+
+    private MultiAttributeData findDefinition(final String paranemterName) {
+
+        for (final Data data : this.dataList.getAll()) {
+
+            final String label = data.getLabel();
+            if (label.equals(paranemterName) && data instanceof MultiAttributeData)
+                return (MultiAttributeData) data;
+        }
+
+        return null;
+    }
+
+    private ListGridField createField(final String colName, final String keyColumnName, final Map<String, String> values,
+            final IColumnClickHandler fromHandler) {
+
+        final String translation = getMeta(values, "translation", colName);
+
+        final ListGridField field = new ListGridField(colName, translation);
+
+        final String width = getMeta(values, "width", "99");
+        field.setWidth(width);
+
+        final ListGridFieldType type = getMeta(values, "type", ListGridFieldType.TEXT, ListGridFieldType.class);
+        field.setType(type);
+
+        final Alignment alignment = getMeta(values, "alignment", Alignment.LEFT, Alignment.class);
+        field.setAlign(alignment);
+
+        final PinType pinType = getMeta(values, "pin", PinType.none, PinType.class);
+        switch (pinType) {
+        case from: {
+            final String baseUrl = GWT.getHostPageBaseURL();
+            field.setCellIcon(baseUrl + this.flys.markerGreen());
+            field.addRecordClickHandler(new RecordClickHandler() {
+                @Override
+                public void onRecordClick(final RecordClickEvent e) {
+                    final Record r = e.getRecord();
+                    fromHandler.columnClicked(r.getAttribute(keyColumnName));
+                }
+            });
+        }
+            break;
+
+        case to:
+            break;
+
+        case none:
+        default:
+            break;
+        }
+
+        return field;
+    }
+
+    private String getMeta(final Map<String, String> values, final String key, final String defaultValue) {
+
+        final String value = values.get(key);
+        if (value == null || value.isEmpty())
+            return defaultValue;
+
+        return value;
+    }
+
+    private <TYPE extends Enum<TYPE>> TYPE getMeta(final Map<String, String> values, final String key, final TYPE defaultValue, final Class<TYPE> enumType) {
+
+        final String value = values.get(key);
+        if (value == null || value.isEmpty())
+            return defaultValue;
+
+        try {
+            return Enum.valueOf(enumType, value);
+        }
+        catch (final Exception e) {
+            e.printStackTrace();
+            return defaultValue;
+        }
+    }
+
+    public List<String> getKeycolEntries() {
+
+        return this.keyColEntries;
+    }
+
 }
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/sinfo/CollisionLoadYearPanel.java	Fri May 18 17:26:26 2018 +0200
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/sinfo/CollisionLoadYearPanel.java	Fri May 18 17:42:34 2018 +0200
@@ -9,56 +9,36 @@
 package org.dive4elements.river.client.client.ui.sinfo;
 
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.SortedMap;
-import java.util.TreeMap;
 
 import org.dive4elements.river.client.client.FLYSConstants;
 import org.dive4elements.river.client.client.ui.AbstractUIProvider;
-import org.dive4elements.river.client.client.ui.ParameterMatrix.Column;
+import org.dive4elements.river.client.client.ui.TableHelper;
+import org.dive4elements.river.client.client.ui.TableHelper.IColumnClickHandler;
 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.MultiAttributeData;
-import org.dive4elements.river.client.shared.model.MultiDataItem;
 
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.i18n.client.NumberFormat;
-import com.smartgwt.client.data.Record;
-import com.smartgwt.client.types.Alignment;
-import com.smartgwt.client.types.ListGridFieldType;
 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 CollisionLoadYearPanel extends AbstractUIProvider {
 
-    public interface IColumnClickHandler {
-        void columnClicked(String value);
-    }
-
-    private enum PinType {
-        none, from, to
-    }
-
     private final FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);
 
     private TextItem yearsItem;
 
-    private final List<String> validYears = new ArrayList<String>();
+    private List<String> validYears = new ArrayList<String>();
 
     private ListGrid table;
 
@@ -97,36 +77,6 @@
     @Override
     public Canvas create(final DataList data) {
         final VLayout layout = new VLayout();
-
-        final Canvas helper = createHelper(data);
-
-        this.helperContainer.addMember(helper);
-
-        final Canvas submit = getNextButton();
-        final Canvas widget = createWidget(data);
-
-        layout.addMember(widget);
-        layout.addMember(submit);
-
-        return layout;
-    }
-
-    private Canvas createHelper(final DataList dataList) {
-        this.table = new ListGrid();
-        this.table.setShowHeaderContextMenu(false);
-        this.table.setWidth100();
-        this.table.setShowRecordComponents(true);
-        this.table.setShowRecordComponentsByCell(true);
-        this.table.setHeight100();
-        this.table.setEmptyMessage(this.MSG.empty_table());
-        this.table.setCanReorderFields(false);
-
-        final MultiAttributeData mData = findDefinition(dataList, "years");
-        final Map<String, Map<String, String>> meta = mData.getMeta();
-
-        final Map<String, String> tableInfo = meta.get("meta_tableinfo");
-        final String keyColumnName = tableInfo.get("keycol");
-
         final IColumnClickHandler fromHandler = new IColumnClickHandler() { // add to external class
 
             @Override
@@ -135,177 +85,19 @@
             }
         };
 
-        final SortedMap<Integer, ListGridField> fields = new TreeMap<Integer, ListGridField>();
-
-        for (final Entry<String, Map<String, String>> entry : meta.entrySet()) {
-
-            final String colName = entry.getKey();
-            if (colName.startsWith("meta_"))
-                continue;
-
-            final Map<String, String> values = entry.getValue();
-
-            final int order = Integer.parseInt(values.get("order"));
-
-            final ListGridField field = createField(colName, keyColumnName, values, fromHandler);
-
-            fields.put(order, field);
-        }
-
-        for (final DataItem dataItem : mData.opts) {
-            final MultiDataItem item = (MultiDataItem) dataItem;
-
-            final Map<String, String> valueMap = item.getValue();
-            // final String columnName = mData.getLabel();
-            final Record newRecord = new Record();
-            for (final Entry<String, String> entry : valueMap.entrySet()) {
-                final String key = entry.getKey();
-                final String value = entry.getValue();
-                newRecord.setAttribute(key, value);
-
-                if (key.equals(keyColumnName))
-                    this.validYears.add(value);
-            }
-
-            this.table.addData(newRecord);
-        }
-
-        this.table.setFields(fields.values().toArray(new ListGridField[fields.size()]));
-
-        return this.table;
-    }
-
-    private MultiAttributeData findDefinition(final DataList dataList, final String paranemterName) {
-
-        for (final Data data : dataList.getAll()) {
-
-            final String label = data.getLabel();
-            if (label.equals(paranemterName) && data instanceof MultiAttributeData)
-                return (MultiAttributeData) data;
-        }
-
-        return null;
-    }
-
-    private ListGridField createField(final String colName, final String keyColumnName, final Map<String, String> values,
-            final IColumnClickHandler fromHandler) {
-
-        final String translation = getMeta(values, "translation", colName);
-
-        final ListGridField field = new ListGridField(colName, translation);
-
-        final String width = getMeta(values, "width", "99");
-        field.setWidth(width);
-
-        final ListGridFieldType type = getMeta(values, "type", ListGridFieldType.TEXT, ListGridFieldType.class);
-        field.setType(type);
-
-        final Alignment alignment = getMeta(values, "alignment", Alignment.LEFT, Alignment.class);
-        field.setAlign(alignment);
-
-        final PinType pinType = getMeta(values, "pin", PinType.none, PinType.class);
-        switch (pinType) {
-        case from: {
-            final String baseUrl = GWT.getHostPageBaseURL();
-            field.setCellIcon(baseUrl + this.MESSAGES.markerGreen());
-            field.addRecordClickHandler(new RecordClickHandler() {
-                @Override
-                public void onRecordClick(final RecordClickEvent e) {
-                    final Record r = e.getRecord();
-                    fromHandler.columnClicked(r.getAttribute(keyColumnName));
-                }
-            });
-        }
-            break;
+        final TableHelper helper = new TableHelper(data, "years", this.MSG, fromHandler);
+        final Canvas table = helper.getTable();
+        this.validYears = helper.getKeycolEntries();
 
-        case to:
-            break;
-
-        case none:
-        default:
-            break;
-        }
-
-        return field;
-    }
-
-    private String getMeta(final Map<String, String> values, final String key, final String defaultValue) {
-
-        final String value = values.get(key);
-        if (value == null || value.isEmpty())
-            return defaultValue;
-
-        return value;
-    }
-
-    private <TYPE extends Enum<TYPE>> TYPE getMeta(final Map<String, String> values, final String key, final TYPE defaultValue, final Class<TYPE> enumType) {
-
-        final String value = values.get(key);
-        if (value == null || value.isEmpty())
-            return defaultValue;
-
-        try {
-            return Enum.valueOf(enumType, value);
-        }
-        catch (final Exception e) {
-            e.printStackTrace();
-            return defaultValue;
-        }
-    }
-
-    private Map<String, Column> columns;
-    private List<String> columnNames;
-    private List<String> valueNames;
-    private Map<String, List<String>> attributes;
-    private Map<String, List<String>> selected;
-
-    public void addColumn(final MultiAttributeData options) {
-        GWT.log("Add Columns for MultiAttribute data");
+        this.helperContainer.addMember(table);
 
-        this.columns = new HashMap<String, Column>();
-        this.columnNames = new ArrayList<String>();
-        this.valueNames = new ArrayList<String>();
-        this.selected = new HashMap<String, List<String>>();
-        this.attributes = new HashMap<String, List<String>>();
-
-        final String groupTitle = options.getLabel();
-
-        final Column col = new Column(groupTitle);
-        final DataItem[] items = options.getItems();
-
-        if (items == null) {
-            GWT.log("No items found in StringOptionsData '" + groupTitle + "'");
-            return;
-        }
+        final Canvas submit = getNextButton();
+        final Canvas widget = createWidget(data);
 
-        final MultiDataItem mItem = (MultiDataItem) items[0];
-        for (final Map.Entry<String, String> entry : mItem.getValue().entrySet()) {
-            if (entry.getKey().equals("art:value") || entry.getKey().equals("art:label")) {
-                continue;
-            }
-            this.attributes.put(entry.getKey(), new ArrayList<String>());
-        }
-        for (final DataItem item : items) {
-            GWT.log("multidataitem: " + item.getLabel());
-            final String title = item.getLabel();
+        layout.addMember(widget);
+        layout.addMember(submit);
 
-            if (this.valueNames.indexOf(title) < 0) {
-                this.valueNames.add(title);
-            }
-            final MultiDataItem mi = (MultiDataItem) item;
-            final Map<String, String> vs = mi.getValue();
-            for (final Map.Entry<String, String> e : vs.entrySet()) {
-                if (e.getKey().equals("art:value") || e.getKey().equals("art:label")) {
-                    continue;
-                }
-                final List<String> data = this.attributes.get(e.getKey());
-                data.add(e.getValue());
-            }
-            col.addValue(item.getLabel(), mi.getValue().get("art:value"));
-        }
-
-        this.columnNames.add(groupTitle);
-        this.columns.put(groupTitle, col);
+        return layout;
     }
 
     public Canvas createWidget(final DataList data) {

http://dive4elements.wald.intevation.org