diff flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/CrossSectionChartThemePanel.java @ 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 8e6b1df7c3b0
children bbd82bd8e541
line wrap: on
line diff
--- 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")

http://dive4elements.wald.intevation.org