changeset 8449:f61e2791ccdf

(issue1733) Fix locationdistancepanel There is now a central method to setup the listgrids and connect the Input completion pins. The old variant with three tables was broken and had did many duplicated things. Comments should also be clearer to point out which code path belongs to which input state.
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 27 Oct 2014 17:00:40 +0100
parents e98dbf72c9ec
children e304f947c5a1
files gwt-client/src/main/java/org/dive4elements/river/client/client/ui/LocationDistancePanel.java
diffstat 1 files changed, 173 insertions(+), 285 deletions(-) [+]
line wrap: on
line diff
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/LocationDistancePanel.java	Mon Oct 27 12:52:00 2014 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/LocationDistancePanel.java	Mon Oct 27 17:00:40 2014 +0100
@@ -145,9 +145,6 @@
     /** The locations table. */
     protected ListGrid locationsTable;
 
-    /** The locations table for distance input. */
-    protected ListGrid locationDistanceTable;
-
     /** The table data. */
     protected DistanceInfoObject[] tableData;
 
@@ -170,10 +167,6 @@
         locationsTable = new ListGrid();
         locationsTable.setAutoFetchData(true);
 
-        locationDistanceTable = new ListGrid();
-        locationDistanceTable.setAutoFetchData(true);
-
-        locationDistanceTable.setShowHeaderContextMenu(false);
         distanceTable.setShowHeaderContextMenu(false);
         locationsTable.setShowHeaderContextMenu(false);
     }
@@ -200,10 +193,6 @@
 
         initDefaults(data);
 
-        createLocationTableDistance ();
-        createDistanceTable();
-        createLocationTable();
-
         widget.setHeight(50);
         label.setHeight(25);
 
@@ -216,18 +205,29 @@
 
 
     /**
-     * This method creates a table that contains the distance values.
+     * Setup a table for a DistanceInfoDataSource.
+     *
+     * Sets up a table to for input completion. The table
+     * can be used either for single locations or distances.
+     * Depending on the value of isDistance the table will
+     * have a to and a from column or a single location column.
+     *
+     * @param table the ListGrid to set up.
+     * @param doublePins wether or not to have.
+     * @param isDistance wether or not to and from should be included.
      */
-    protected void createDistanceTable() {
+    protected void setupDistanceInfoTable(ListGrid table,
+                                          boolean doublePins,
+                                          boolean isDistance) {
 
         String baseUrl = GWT.getHostPageBaseURL();
 
-        distanceTable.setWidth100();
-        distanceTable.setShowRecordComponents(true);
-        distanceTable.setShowRecordComponentsByCell(true);
-        distanceTable.setHeight100();
-        distanceTable.setEmptyMessage(MESSAGES.empty_filter());
-        distanceTable.setCanReorderFields(false);
+        table.setWidth100();
+        table.setShowRecordComponents(true);
+        table.setShowRecordComponentsByCell(true);
+        table.setHeight100();
+        table.setEmptyMessage(MESSAGES.empty_filter());
+        table.setCanReorderFields(false);
 
         CellFormatter cf = new CellFormatter() {
             @Override
@@ -247,69 +247,112 @@
                     }
             }
         };
+        ListGridField pin1 = null;
+        ListGridField pin2 = null;
 
-        ListGridField addDistance = new ListGridField ("", "");
-        addDistance.setType (ListGridFieldType.ICON);
-        addDistance.setWidth (30);
-        addDistance.addRecordClickHandler (new RecordClickHandler () {
-            @Override
-            public void onRecordClick (RecordClickEvent e) {
-                if (!isLocationMode ()) {
+        if (doublePins) {
+            pin1 = new ListGridField ("fromIcon", MESSAGES.from());
+            pin1.setWidth (30);
+        } else {
+            pin1 = new ListGridField ("fromIcon", MESSAGES.selection());
+            pin1.setWidth (60);
+        }
+        pin1.setType (ListGridFieldType.ICON);
+        pin1.setCellIcon(baseUrl + MESSAGES.markerGreen());
+
+        if (doublePins) {
+            pin2 = new ListGridField ("toIcon", MESSAGES.to());
+            pin2.setType (ListGridFieldType.ICON);
+            pin2.setWidth (30);
+            pin2.setCellIcon(baseUrl + MESSAGES.markerRed());
+        }
+
+        if (isDistance) {
+            /* We have from / to fields */
+            pin1.addRecordClickHandler (new RecordClickHandler () {
+                @Override
+                public void onRecordClick (RecordClickEvent e) {
                     Record r = e.getRecord();
-                    try {
-                        double min = Double.parseDouble(r.getAttribute("from"));
-                        double max = Double.parseDouble(r.getAttribute("to"));
-                        setDistanceValues(min, max);
-                    }
-                    catch(NumberFormatException nfe) {
-                        // Is there anything to do?
+                    if (!isLocationMode ()) {
+                        /* distance panel and distance mode */
+                        setFrom(r.getAttribute("from"));
+                        setTo(r.getAttribute("to"));
+                    } else {
+                        /* distance panel and location mode */
+                        /* Pin 1 is the "from" pin */
+                        appendLocation(r.getAttribute("from"));
                     }
                 }
-                else {
-                    double[] selected;
-                    Record r = e.getRecord();
-                    double min = 0, max = 0;
-                    try {
-                        min = Double.parseDouble(r.getAttribute("from"));
-                        max = Double.parseDouble(r.getAttribute("to"));
-                    }
-                    catch(NumberFormatException nfe) {
-                        // Is there anything to do?
+            });
+            if (doublePins) {
+                pin2.addRecordClickHandler (new RecordClickHandler () {
+                    @Override
+                    public void onRecordClick (RecordClickEvent e) {
+                        Record r = e.getRecord();
+                        if (isLocationMode ()) {
+                            appendLocation(r.getAttribute("to"));
+                        } else {
+                            /* Distance and double pin behavior is only defined for
+                             * location mode. */
+                            GWT.log("Unhandled input state.");
+                        }
                     }
-                    if (getLocationValues() != null) {
-                        double[] val = getLocationValues();
-                        selected = new double[val.length + 2];
-                        for(int i = 0; i < val.length; i++){
-                            selected[i] = val[i];
-                        }
-                        selected[val.length] = min;
-                        selected[val.length + 1] = max;
+                });
+            }
+        } else {
+            /* We only have the from field */
+            pin1.addRecordClickHandler (new RecordClickHandler () {
+                @Override
+                public void onRecordClick (RecordClickEvent e) {
+                    Record r = e.getRecord();
+                    if (!isLocationMode ()) {
+                        /* Location panel and distance mode */
+                        setFrom(r.getAttribute("from"));
+                    } else {
+                        /* Location panel and location mode */
+                        appendLocation(r.getAttribute("from"));
                     }
-                    else {
-                        selected = new double[2];
-                        selected[0] = min;
-                        selected[1] = max;
+                }
+            });
+            if (doublePins) {
+                pin2.addRecordClickHandler (new RecordClickHandler () {
+                    @Override
+                    public void onRecordClick (RecordClickEvent e) {
+                        Record r = e.getRecord();
+                        if (!isLocationMode ()) {
+                            setTo(r.getAttribute("from"));
+                        } else {
+                            /* Distance and double pin behavior is only defined for
+                             * location mode. */
+                            GWT.log("Unhandled input state.");
+                        }
                     }
-                setLocationValues(selected);
-                }
+                });
             }
-        });
-        addDistance.setCellIcon(baseUrl + MESSAGES.markerGreen());
+        }
 
         ListGridField ddescr = new ListGridField("description",
                 MESSAGES.description());
         ddescr.setType(ListGridFieldType.TEXT);
         ddescr.setWidth("*");
-        ListGridField from = new ListGridField("from", MESSAGES.from());
+
+        ListGridField from;
+        ListGridField to = null;
+
+        if (isDistance) {
+            from = new ListGridField("from", MESSAGES.from());
+            to = new ListGridField("to", MESSAGES.to());
+            to.setType(ListGridFieldType.FLOAT);
+            to.setCellFormatter(cf);
+
+            to.setWidth("12%");
+            to.setAlign(Alignment.LEFT);
+        } else {
+            from = new ListGridField("from", MESSAGES.locations());
+        }
         from.setCellFormatter(cf);
-
         from.setWidth("12%");
-        ListGridField to = new ListGridField("to", MESSAGES.to());
-        to.setType(ListGridFieldType.FLOAT);
-        to.setCellFormatter(cf);
 
-        to.setWidth("12%");
-        to.setAlign(Alignment.LEFT);
         ListGridField dside = new ListGridField("riverside",
                 MESSAGES.riverside());
         dside.setType(ListGridFieldType.TEXT);
@@ -327,212 +370,17 @@
         top.setWidth("10%");
         top.setCellFormatter(cf);
 
-        distanceTable.setFields(
-            addDistance, ddescr, from, to, dside, bottom, top);
-    }
-
-
-    /**
-     * This method creates a table that contains the location values.
-     */
-    protected void createLocationTable() {
-
-        String baseUrl = GWT.getHostPageBaseURL();
-
-        locationsTable.setWidth100();
-        locationsTable.setShowRecordComponents(true);
-        locationsTable.setShowRecordComponentsByCell(true);
-        locationsTable.setHeight100();
-        locationsTable.setEmptyMessage(MESSAGES.empty_filter());
-        locationsTable.setCanReorderFields(false);
-
-        CellFormatter cf = new CellFormatter() {
-            @Override
-            public String format(
-                Object value,
-                ListGridRecord record,
-                int rowNum, int colNum) {
-                    if (value == null) return null;
-                    try {
-                        NumberFormat nf;
-                        double v = Double.parseDouble((String)value);
-                        nf = NumberFormat.getFormat("###0.00##");
-                        return nf.format(v);
-                    }
-                    catch (Exception e) {
-                        return value.toString();
-                    }
-                }
-            };
-
-
-
-        ListGridField addLocation = new ListGridField ("", "");
-        addLocation.setType (ListGridFieldType.ICON);
-        addLocation.setWidth (20);
-
-        addLocation.addRecordClickHandler (new RecordClickHandler () {
-            @Override
-            public void onRecordClick (RecordClickEvent e) {
-                Record record = e.getRecord();
-                double[] selected;
-                if (getLocationValues() != null) {
-                    double[] val = getLocationValues();
-                    selected = new double[val.length + 1];
-                    for(int i = 0; i < val.length; i++){
-                        selected[i] = val[i];
-                    }
-                    try {
-                        selected[val.length] =
-                            Double.parseDouble(record.getAttribute("from"));
-                    }
-                    catch(NumberFormatException nfe) {
-                        // Is there anything to do here?
-                    }
-                }
-                else {
-                    selected = new double[1];
-                    selected[0] =
-                        Double.parseDouble(record.getAttribute("from"));
-                }
-                setLocationValues(selected);
-            }
-        });
-        addLocation.setCellIcon (baseUrl + MESSAGES.markerGreen ());
-
-        ListGridField ldescr = new ListGridField("description",
-                MESSAGES.description());
-        ldescr.setType(ListGridFieldType.TEXT);
-        ldescr.setWidth("*");
-        ListGridField lside = new ListGridField("riverside",
-                MESSAGES.riverside());
-        lside.setType(ListGridFieldType.TEXT);
-        lside.setWidth("12%");
-        ListGridField loc = new ListGridField("from", MESSAGES.locations());
-        loc.setAlign(Alignment.LEFT);
-        loc.setType(ListGridFieldType.FLOAT);
-        loc.setWidth("12%");
-        loc.setCellFormatter(cf);
-
-        ListGridField bottom =
-            new ListGridField("bottom", MESSAGES.bottom_edge());
-        bottom.setType(ListGridFieldType.TEXT);
-        bottom.setWidth("10%");
-        bottom.setCellFormatter(cf);
-
-        ListGridField top =
-            new ListGridField("top", MESSAGES.top_edge());
-        top.setType(ListGridFieldType.TEXT);
-        top.setWidth("10%");
-        top.setCellFormatter(cf);
-
-        locationsTable.setFields(addLocation, ldescr, loc, lside, bottom, top);
+        if (doublePins && isDistance) {
+            table.setFields(pin1, pin2, ddescr, from, to, dside, bottom, top);
+        } else if (doublePins) {
+            table.setFields(pin1, pin2, ddescr, from, dside, bottom, top);
+        } else if (isDistance) {
+            table.setFields(pin1, ddescr, from, to, dside, bottom, top);
+        } else {
+            table.setFields(pin1, ddescr, from, dside, bottom, top);
+        }
     }
 
-
-    /**
-     * This method creates a table that contains the location values.
-     */
-    protected void createLocationTableDistance (){
-
-        String baseUrl = GWT.getHostPageBaseURL();
-
-        locationDistanceTable.setWidth100();
-        locationDistanceTable.setShowRecordComponents(true);
-        locationDistanceTable.setShowRecordComponentsByCell(true);
-        locationDistanceTable.setHeight100();
-        locationDistanceTable.setEmptyMessage(MESSAGES.empty_filter());
-        locationDistanceTable.setCanReorderFields(false);
-
-        CellFormatter cf = new CellFormatter() {
-            @Override
-            public String format(
-                Object value,
-                ListGridRecord record,
-                int rowNum, int colNum) {
-                    if (value == null) return null;
-                    try {
-                        NumberFormat nf;
-                        double v = Double.parseDouble((String)value);
-                        nf = NumberFormat.getFormat("###0.00##");
-                        return nf.format(v);
-                    }
-                    catch (Exception e) {
-                        return value.toString();
-                    }
-            }
-        };
-
-        ListGridField addfrom = new ListGridField ("fromIcon", MESSAGES.from());
-        addfrom.setType (ListGridFieldType.ICON);
-        addfrom.setWidth (30);
-        addfrom.setCellIcon(baseUrl + MESSAGES.markerGreen());
-
-        ListGridField addto2 = new ListGridField ("toIcon", MESSAGES.to());
-        addto2.setType (ListGridFieldType.ICON);
-        addto2.setWidth (30);
-        addto2.setCellIcon(baseUrl + MESSAGES.markerRed());
-
-        locationDistanceTable.addCellClickHandler (new CellClickHandler () {
-            @Override
-            public void onCellClick (CellClickEvent e) {
-                if (e.getColNum() == 0) {
-                    Record r = e.getRecord ();
-                    try {
-                        double fromvalue =
-                             Double.parseDouble(r.getAttribute("from"));
-                        double tovalue = getTo ();
-                        setDistanceValues (fromvalue, tovalue);
-                    }
-                    catch(NumberFormatException nfe) {
-                        // Is there anything to do in here?
-                    }
-                }
-                else if (e.getColNum() == 1) {
-                    Record r = e.getRecord ();
-                    try {
-                        double fromvalue = getFrom ();
-                        double tovalue =
-                            Double.parseDouble(r.getAttribute("from"));
-                        setDistanceValues (fromvalue, tovalue);
-                    }
-                    catch(NumberFormatException nfe) {
-                        // Is there anything to do in here?
-                    }
-                }
-            }
-        });
-        ListGridField bottom =
-            new ListGridField("bottom", MESSAGES.bottom_edge());
-        bottom.setType(ListGridFieldType.TEXT);
-        bottom.setWidth("10%");
-        bottom.setCellFormatter(cf);
-
-        ListGridField top =
-            new ListGridField("top", MESSAGES.top_edge());
-        top.setType(ListGridFieldType.TEXT);
-        top.setWidth("10%");
-        top.setCellFormatter(cf);
-
-        ListGridField ldescr = new ListGridField("description",
-                MESSAGES.description());
-        ldescr.setType(ListGridFieldType.TEXT);
-        ldescr.setWidth("*");
-        ListGridField lside = new ListGridField("riverside",
-                MESSAGES.riverside());
-        lside.setType(ListGridFieldType.TEXT);
-        lside.setWidth("12%");
-        ListGridField loc = new ListGridField("from", MESSAGES.locations());
-        loc.setType(ListGridFieldType.FLOAT);
-        loc.setAlign(Alignment.LEFT);
-        loc.setWidth("12%");
-        loc.setCellFormatter(cf);
-
-        locationDistanceTable.setFields(
-            addfrom, addto2, ldescr, loc, lside, bottom, top);
-    }
-
-
     @Override
     public Canvas createOld(DataList dataList) {
         List<Data> items = dataList.getAll();
@@ -694,6 +542,8 @@
         if (theMode.equals(FIELD_VALUE_DISTANCE)) {
             enableDistanceMode();
             inputTables.selectTab(1);
+        } else {
+            enableLocationMode();
         }
         currentFiltered = (ListGrid)inputTables.getSelectedTab().getPane();
 
@@ -735,11 +585,6 @@
                 filterResultCount.setValue("");
 
                 // The assumption is that location is tab 0 and distance tab 1
-                if (inputTables.getSelectedTabNumber() == 0) {
-                    enableLocationMode();
-                } else {
-                    enableDistanceMode();
-                }
 
                 Canvas c = evt.getTabPane();
                 if(c instanceof ListGrid) {
@@ -754,7 +599,7 @@
         inputTables.setWidth100();
         inputTables.setHeight100();
 
-        locations.setPane(locationDistanceTable);
+        locations.setPane(locationsTable);
         distances.setPane(distanceTable);
 
         inputTables.addTab(locations);
@@ -836,13 +681,11 @@
 
             locationsTable.filterData(c);
             distanceTable.filterData(c);
-            locationDistanceTable.filterData(c);
             filterResultCount.setValue(currentFiltered.getRecords().length);
         }
         else {
             locationsTable.clearCriteria();
             distanceTable.clearCriteria();
-            locationDistanceTable.clearCriteria();
             filterResultCount.setValue("");
         }
     }
@@ -860,7 +703,6 @@
         if (from.equals(Float.NaN) && to.equals(Float.NaN)) {
             locationsTable.clearCriteria();
             distanceTable.clearCriteria();
-            locationDistanceTable.clearCriteria();
             filterResultCount.setValue("");
             return;
         }
@@ -870,7 +712,6 @@
                 new Criterion("from", OperatorId.LESS_OR_EQUAL, to);
             locationsTable.filterData(locationFilter);
             distanceTable.filterData(combinedFilter);
-            locationDistanceTable.filterData(combinedFilter);
             filterResultCount.setValue(currentFiltered.getRecords().length);
             return;
         }
@@ -879,7 +720,6 @@
                 new Criterion("from", OperatorId.GREATER_OR_EQUAL, from);
              locationsTable.filterData(combinedFilter);
             distanceTable.filterData(combinedFilter);
-            locationDistanceTable.filterData(combinedFilter);
         }
         else {
             AdvancedCriteria c1 =
@@ -907,7 +747,6 @@
         }
         locationsTable.filterData(combinedFilter);
         distanceTable.filterData(combinedFilter);
-        locationDistanceTable.filterData(combinedFilter);
         filterResultCount.setValue(currentFiltered.getRecords().length);
     }
 
@@ -1168,6 +1007,10 @@
         mode.setValue(FIELD_MODE, FIELD_VALUE_LOCATION);
         container.hideMember(distancePanel);
         container.showMember(locationPanel);
+        setupDistanceInfoTable(locationsTable, false, false);
+        setupDistanceInfoTable(distanceTable, true, true);
+        inputTables.updateTab(0, locationsTable);
+        inputTables.updateTab(1, distanceTable);
     }
 
 
@@ -1178,6 +1021,10 @@
         mode.setValue(FIELD_MODE, FIELD_VALUE_DISTANCE);
         container.hideMember(locationPanel);
         container.showMember(distancePanel);
+        setupDistanceInfoTable(locationsTable, true, false);
+        setupDistanceInfoTable(distanceTable, false, true);
+        inputTables.updateTab(0, locationsTable);
+        inputTables.updateTab(1, distanceTable);
     }
 
 
@@ -1359,18 +1206,36 @@
             url, river, "distances"));
         locationsTable.setDataSource(new DistanceInfoDataSource(
             url, river, "locations"));
-        locationDistanceTable.setDataSource(new DistanceInfoDataSource(
-            url, river, "locations"));
     }
 
-
     protected double getFrom() {
         return from;
     }
 
+    protected void setTo(String to) {
+       try {
+            double toValue = Double.parseDouble(to);
+            setTo(toValue);
+        }
+        catch(NumberFormatException nfe) {
+            // Is there anything to do?
+        }
+    }
+
+    protected void setFrom(String from) {
+       try {
+            double fromValue = Double.parseDouble(from);
+            setFrom(fromValue);
+        }
+        catch(NumberFormatException nfe) {
+            // Is there anything to do?
+        }
+    }
 
     protected void setFrom(double from) {
         this.from = from;
+        /* The doubling should be removed and this.from abolished */
+        distancePanel.setFrom(from);
     }
 
 
@@ -1381,6 +1246,8 @@
 
     protected void setTo(double to) {
         this.to = to;
+        /* The doubling should be removed and this.to abolished */
+        distancePanel.setTo(to);
     }
 
 
@@ -1398,6 +1265,27 @@
         return values;
     }
 
+    protected void appendLocation(String loc) {
+        double[] selected;
+        if (getLocationValues() != null) {
+            double[] val = getLocationValues();
+            selected = new double[val.length + 1];
+            for(int i = 0; i < val.length; i++){
+                selected[i] = val[i];
+            }
+            try {
+                selected[val.length] = Double.parseDouble(loc);
+            }
+            catch(NumberFormatException nfe) {
+                // Is there anything to do here?
+            }
+        }
+        else {
+            selected = new double[1];
+            selected[0] = Double.parseDouble(loc);
+        }
+        setLocationValues(selected);
+    }
 
     protected void setLocationValues(double[] values) {
         this.values = values;

http://dive4elements.wald.intevation.org