changeset 3717:3b4cef59836a

KMSpinner refactoring flys-client/trunk@5482 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Christian Lins <christian.lins@intevation.de>
date Sun, 16 Sep 2012 12:38:24 +0000
parents 4e33aa341e51
children 1598d324fbc2 e7f421156a93
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartOutputTab.java flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/CrossSectionChartThemePanel.java flys-client/src/main/java/de/intevation/flys/client/client/widgets/KMSpinner.java flys-client/src/main/java/de/intevation/flys/client/client/widgets/KMSpinnerChangeListener.java
diffstat 5 files changed, 190 insertions(+), 201 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Sat Sep 15 18:28:24 2012 +0000
+++ b/flys-client/ChangeLog	Sun Sep 16 12:38:24 2012 +0000
@@ -1,3 +1,11 @@
+2012-09-16	Christian Lins	<christian.lins@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/client/widgets/KMSpinnerChangeListener.java (NEW),
+	  src/main/java/de/intevation/flys/client/client/widgets/KMSpinner.java (NEW),
+	  src/main/java/de/intevation/flys/client/client/ui/chart/CrossSectionChartThemePanel.java,
+	  src/main/java/de/intevation/flys/client/client/ui/chart/ChartOutputTab.java:
+	  Refactored some KMSpinner code. KMSpinner has now a human-readable size (#853).
+
 2012-09-15	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/shared/model/DefaultRiverInfo.java,
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartOutputTab.java	Sat Sep 15 18:28:24 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ChartOutputTab.java	Sun Sep 16 12:38:24 2012 +0000
@@ -53,7 +53,7 @@
     public static final int DEFAULT_CHART_WIDTH  = 600;
     public static final int DEFAULT_CHART_HEIGHT = 500;
 
-    public static final int THEMEPANEL_MIN_WIDTH = 200;
+    public static final int THEMEPANEL_MIN_WIDTH = 250;
 
     /** The service that is used to fetch chart information. */
     protected ChartInfoServiceAsync info = GWT.create(ChartInfoService.class);
@@ -153,7 +153,7 @@
         tbarPanel = createChartToolbar(this);
         vLayout.addMember(tbarPanel);
         vLayout.addMember(hLayout);
-	vLayout.setOverflow(Overflow.HIDDEN);
+        vLayout.setOverflow(Overflow.HIDDEN);
 
         setPane(vLayout);
 
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/CrossSectionChartThemePanel.java	Sat Sep 15 18:28:24 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/CrossSectionChartThemePanel.java	Sun Sep 16 12:38:24 2012 +0000
@@ -1,60 +1,30 @@
 package de.intevation.flys.client.client.ui.chart;
 
 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.Button;
 import com.smartgwt.client.widgets.Canvas;
-import com.smartgwt.client.widgets.Label;
-
-import com.smartgwt.client.widgets.events.ClickEvent;
-
 import com.smartgwt.client.widgets.form.DynamicForm;
-import com.smartgwt.client.widgets.form.FormItemValueFormatter;
-import com.smartgwt.client.widgets.form.FormItemValueParser;
-
-import com.smartgwt.client.widgets.form.fields.FormItem;
 import com.smartgwt.client.widgets.form.fields.SelectItem;
-import com.smartgwt.client.widgets.form.fields.SpinnerItem;
-import com.smartgwt.client.widgets.form.fields.TextItem;
-
-import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
-import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
 import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
 import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
-import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
-import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
-
 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.HLayout;
 import com.smartgwt.client.widgets.layout.VLayout;
-
 import com.smartgwt.client.widgets.menu.Menu;
 import com.smartgwt.client.widgets.menu.MenuItem;
-
 import com.smartgwt.client.widgets.menu.events.ClickHandler;
 import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
 
 import de.intevation.flys.client.client.Config;
-
 import de.intevation.flys.client.client.services.CrossSectionKMServiceAsync;
 import de.intevation.flys.client.client.services.LoadArtifactService;
 import de.intevation.flys.client.client.services.LoadArtifactServiceAsync;
-
 import de.intevation.flys.client.client.ui.CollectionView;
-
+import de.intevation.flys.client.client.widgets.KMSpinner;
+import de.intevation.flys.client.client.widgets.KMSpinnerChangeListener;
 import de.intevation.flys.client.shared.model.Artifact;
 import de.intevation.flys.client.shared.model.Data;
 import de.intevation.flys.client.shared.model.DefaultArtifact;
@@ -80,7 +50,9 @@
  * Also can show 'area creation' context menus.
  */
 public class CrossSectionChartThemePanel
-extends      ChartThemePanel {
+extends ChartThemePanel
+implements KMSpinnerChangeListener
+{
     /** Artifact Clone/Creation service. */
     protected LoadArtifactServiceAsync loadService =
                 GWT.create(LoadArtifactService.class);
@@ -270,12 +242,14 @@
 
 
     /** Disable the UI (becomes gray, inresponsive to user input). */
+    @Override
     public void disable() {
         this.layout.setDisabled(true);
     }
 
 
     /** DisDisable the UI (becomes ungray, responsive to user input). */
+    @Override
     public void enable() {
         this.layout.setDisabled(false);
     }
@@ -422,9 +396,7 @@
      *                    two values in \param in are in the same distance to
      *                    \param to.
      */
-    public void spinnerValueEntered(final SpinnerItem item,
-        final double enteredKm, final FacetRecord facetRecord, final boolean up
-    ) {
+    public void spinnerValueEntered(KMSpinner spinner, final double enteredKm, final FacetRecord facetRecord, final boolean up) {
         disable();
         Config config       = Config.getInstance();
         final String locale = config.getLocale();
@@ -455,6 +427,7 @@
                     //updateGrid();
                     enable();
                 }
+
                 @Override
                 public void onSuccess(Map<Integer, Double[]> obj) {
                     Double[] kms = obj.get(dbid);
@@ -492,156 +465,13 @@
 
 
     /**
-     * Create a "kilometer spinner" for CrossSection Facets.
-     * @param facetRecord The respective Facet/Theme.
-     * @return label, intialized SpinnerItem.
-     */
-    public SpinnerItem createSpinnerItem(FacetRecord facetRecord) {
-        SpinnerItem spinnerItem = new SpinnerItem();
-        spinnerItem.setShowTitle(false);
-        spinnerItem.setTitle("Waterlevel-Spinner");
-        spinnerItem.setWidth(45);
-        spinnerItem.setDefaultValue(Double.valueOf(facetRecord.getTheme()
-            .getCollectionItem()
-            .getData().get(CS_KM)));
-
-        spinnerItem.setMin(0);
-        spinnerItem.setMax(2000);
-        spinnerItem.setStep(0.1d);
-        spinnerItem.setChangeOnKeypress(true);
-        return spinnerItem;
-    }
-
-
-    /**
-     * SpinnerItem-like element with text label and up/down buttons.
-     */
-    public class KmSpinner extends HLayout {
-        protected Label label;
-
-        protected FacetRecord facetRecord;
-
-        protected double currentValue;
-
-        public KmSpinner(FacetRecord facetRecord) {
-            super(2);
-            this.facetRecord = facetRecord;
-            final FacetRecord _facetRecord = facetRecord;
-            currentValue = Double.valueOf(facetRecord.getTheme()
-                .getCollectionItem().getData().get(CS_KM));
-            // Buttons and labels.
-            int height = 18;
-            // minusButton shall ask service for previous available cs.
-            Button minusButton = new Button("-");
-            minusButton.setWidth(10);
-            minusButton.setHeight(height);
-            minusButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
-                public void onClick(ClickEvent evt) {
-                    spinnerValueEntered(null, currentValue - 0.1d, _facetRecord, false);
-                }
-            });
-
-            DynamicForm form = new DynamicForm();
-            final TextItem kmField = new TextItem();
-            kmField.setValue(currentValue);
-            kmField.setWidth(35);
-            kmField.setTitle("");
-            kmField.setHeight(height);
-
-            FormItemValueFormatter doubleFormat = new FormItemValueFormatter() {
-                public String formatValue(Object value, Record record,
-                    DynamicForm form, FormItem item) {
-                        if (value != null) {
-                            NumberFormat nf = NumberFormat.getDecimalFormat();
-                            try {
-                                double d = Double.valueOf(value.toString()).doubleValue();
-                                return nf.format(d);
-                            } catch (Exception e) {
-                                return value.toString();
-                            }
-                        }
-                        else {
-                            return null;
-                        }
-                    }
-            };
-            kmField.setEditorValueFormatter(doubleFormat);
-
-            FormItemValueParser doubleParser = new FormItemValueParser() {
-                public Object parseValue(String value,
-                                  DynamicForm form,
-                                  FormItem item) {
-                    if (value == null)
-                        return null;
-                    try {
-                        NumberFormat nf = NumberFormat.getDecimalFormat();
-                        double d = nf.parse(value.toString());
-                        return (new Double(d)).toString();
-                    }
-                    catch(NumberFormatException nfe) {
-                        return value;
-                    }
-                }
-            };
-            kmField.setEditorValueParser(doubleParser);
-
-            // Update on focus lost and enter-pressed.
-            kmField.addBlurHandler(new BlurHandler() {
-                @Override
-                public void onBlur(BlurEvent be) {
-                    if (kmField.getValue() != null) {
-                        try {
-                            spinnerValueEntered(null,
-                                Double.parseDouble(kmField.getValue().toString()),
-                                _facetRecord, true);
-                        }
-                        catch(NumberFormatException nfe) {
-                            GWT.log("entered string cannot be parsed to double.");
-                        }
-                    }
-                }
-            });
-            kmField.addKeyPressHandler(new KeyPressHandler(){
-                @Override
-                public void onKeyPress(KeyPressEvent kpe) {
-                    if (kpe.getKeyName().equals("Enter")) {
-                        kmField.blurItem();
-                    }
-                }
-            });
-            // TODO: i18n Now add all the validators, formatters, editors/parsers  etc.
-            form.setFields(kmField);
-            form.setTitle("");
-            form.setTitlePrefix("");
-            form.setTitleSuffix("");
-            form.setTitleWidth(0);
-            form.setWidth(40);
-            form.setHeight(height);
-            // PlusButton shall ask service for next available cs.
-            Button plusButton = new Button("+");
-            plusButton.setWidth(10);
-            plusButton.setHeight(height);
-            plusButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
-                public void onClick(ClickEvent evt) {
-                    spinnerValueEntered(null, currentValue + 0.1d, _facetRecord, true);
-                }
-            });
-            this.addMember(minusButton);
-            this.addMember(form);
-            this.addMember(plusButton);
-
-            this.setHeight(height*2);
-            this.setWidth(60);
-        }
-    }
-
-
-    /**
      * Create and configure the Grid to display.
      * @return ListGrid with Themes and related controls inside.
      */
     @Override
     protected ListGrid createGrid() {
+        final CrossSectionChartThemePanel parent = this;
+
         ListGrid list = new ListGrid() {
             @Override
             protected Canvas createRecordComponent(
@@ -659,23 +489,11 @@
                     String fieldName = this.getFieldName(colNum);
 
                     if (fieldName.equals(GRID_FIELD_ACTIONS)) {
-                        /*
-                        TODO:
-                        if (facetRecord.getTheme().getActive() != 1) {
-                            spinnerItem.disable();
-                        }
-                        */
-
-                        /*
-                        // To have visual representation of synchronous
-                        // navigation or not per theme, snip:
-                        if (synchronCrossSectionThemes.contains (themeHash
-                            (facetRecord.getTheme()))) {
-                            spinnerItem.setTextBoxStyle("bgBlueDark");
-                        }
-                        */
-
-                        return new KmSpinner(facetRecord);
+                        double currentValue =
+                            Double.valueOf(facetRecord.getTheme().getCollectionItem().getData().get(CS_KM));
+                        KMSpinner kmSpinner = new KMSpinner(currentValue, facetRecord);
+                        kmSpinner.addChangeListener(parent);
+                        return kmSpinner;
                     }
                     else {
                         return null;
@@ -715,7 +533,7 @@
         name.setType(ListGridFieldType.TEXT);
 
         ListGridField actions = new ListGridField(GRID_FIELD_ACTIONS,
-             MSG.chart_themepanel_header_actions(), 65);
+             MSG.chart_themepanel_header_actions(), 100);
 
         list.setFields(active, name, actions);
     }
@@ -734,6 +552,7 @@
 
 
     /** Returns name of cross section area facets. */
+    @Override
     protected String getAreaFacetName() {
         return "cross_section.area";
     }
@@ -745,6 +564,7 @@
      * TODO join with canArea, generalize to allow easier modification
      *      in subclasses.
      */
+    @Override
     protected boolean areAreaCompatible(Theme a, Theme b) {
         if (a.equals(b)) {
             return false;
@@ -760,6 +580,7 @@
      * True if context menu should contain 'create area' submenu on
      * this theme.
      */
+    @Override
     protected boolean canArea(Theme a) {
         return a.getFacet().equals("cross_section")
             || a.getFacet().equals("cross_section_water_line")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/widgets/KMSpinner.java	Sun Sep 16 12:38:24 2012 +0000
@@ -0,0 +1,151 @@
+package de.intevation.flys.client.client.widgets;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.i18n.client.NumberFormat;
+import com.smartgwt.client.data.Record;
+import com.smartgwt.client.widgets.Button;
+import com.smartgwt.client.widgets.Label;
+import com.smartgwt.client.widgets.events.ClickEvent;
+import com.smartgwt.client.widgets.form.DynamicForm;
+import com.smartgwt.client.widgets.form.FormItemValueFormatter;
+import com.smartgwt.client.widgets.form.FormItemValueParser;
+import com.smartgwt.client.widgets.form.fields.FormItem;
+import com.smartgwt.client.widgets.form.fields.TextItem;
+import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
+import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
+import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
+import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
+import com.smartgwt.client.widgets.layout.HLayout;
+
+import de.intevation.flys.client.shared.model.FacetRecord;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * SpinnerItem-like element with text label and up/down buttons.
+ */
+public class KMSpinner extends HLayout {
+    protected List<KMSpinnerChangeListener> listeners = new ArrayList<KMSpinnerChangeListener>();
+
+    protected Label label;
+    protected FacetRecord facetRecord;
+    protected double value;
+
+    public KMSpinner(double initialValue, FacetRecord facetRecord) {
+        super(2);
+        this.facetRecord = facetRecord;
+        this.value = initialValue;
+
+        setWidth("99%");
+
+        // minusButton shall ask service for previous available cs.
+        Button minusButton = new Button("-");
+        minusButton.setWidth(18);
+        minusButton.setHeight(18);
+        minusButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
+            public void onClick(ClickEvent evt) {
+                fireChangedEvent(value - 0.1d, false);
+            }
+        });
+
+        DynamicForm form = new DynamicForm();
+        final TextItem kmField = new TextItem();
+        kmField.setValue(initialValue);
+        kmField.setWidth("*");
+        kmField.setTitle("");
+        kmField.setHeight(16);
+
+        FormItemValueFormatter doubleFormat = new FormItemValueFormatter() {
+            public String formatValue(Object value, Record record, DynamicForm form, FormItem item) {
+                if (value != null) {
+                    NumberFormat nf = NumberFormat.getDecimalFormat();
+                    try {
+                        double d = Double.valueOf(value.toString()).doubleValue();
+                        return nf.format(d);
+                    }
+                    catch (Exception e) {
+                        return value.toString();
+                    }
+                }
+                else {
+                    return null;
+                }
+            }
+        };
+        kmField.setEditorValueFormatter(doubleFormat);
+
+        FormItemValueParser doubleParser = new FormItemValueParser() {
+            public Object parseValue(String value, DynamicForm form, FormItem item) {
+                if (value == null)
+                    return null;
+                try {
+                    NumberFormat nf = NumberFormat.getDecimalFormat();
+                    double d = nf.parse(value.toString());
+                    return Double.toString(d);
+                }
+                catch(NumberFormatException nfe) {
+                    return value;
+                }
+            }
+        };
+        kmField.setEditorValueParser(doubleParser);
+
+        // Update on focus lost and enter-pressed.
+        kmField.addBlurHandler(new BlurHandler() {
+            @Override
+            public void onBlur(BlurEvent be) {
+                if (kmField.getValue() != null) {
+                    try {
+                        fireChangedEvent(Double.parseDouble(kmField.getValue().toString()), true);
+                    }
+                    catch(NumberFormatException nfe) {
+                        GWT.log("entered string cannot be parsed to double.");
+                    }
+                }
+            }
+        });
+        kmField.addKeyPressHandler(new KeyPressHandler() {
+            @Override
+            public void onKeyPress(KeyPressEvent kpe) {
+                if (kpe.getKeyName().equals("Enter")) {
+                    kmField.blurItem();
+                }
+            }
+        });
+
+        // TODO: i18n Now add all the validators, formatters, editors/parsers  etc.
+        form.setFields(kmField);
+        form.setTitle("");
+        form.setTitlePrefix("");
+        form.setTitleSuffix("");
+        form.setTitleWidth(0);
+        form.setWidth(50);
+        form.setHeight(18);
+
+        // PlusButton shall ask service for next available cs.
+        Button plusButton = new Button("+");
+        plusButton.setWidth(18);
+        plusButton.setHeight(18);
+        plusButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
+            public void onClick(ClickEvent evt) {
+                fireChangedEvent(value + 0.1d, true);
+            }
+        });
+
+        this.addMember(minusButton);
+        this.addMember(form);
+        this.addMember(plusButton);
+    }
+
+    public void addChangeListener(KMSpinnerChangeListener listener) {
+        this.listeners.add(listener);
+    }
+
+    protected void fireChangedEvent(double val, boolean up) {
+        for(KMSpinnerChangeListener listener : listeners) {
+            listener.spinnerValueEntered(this, val, facetRecord, up);
+        }
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/widgets/KMSpinnerChangeListener.java	Sun Sep 16 12:38:24 2012 +0000
@@ -0,0 +1,9 @@
+package de.intevation.flys.client.client.widgets;
+
+import de.intevation.flys.client.client.widgets.KMSpinner;
+
+import de.intevation.flys.client.shared.model.FacetRecord;
+
+public interface KMSpinnerChangeListener {
+    public void spinnerValueEntered(KMSpinner spinner, double km, FacetRecord facetRecord, boolean up);
+}

http://dive4elements.wald.intevation.org