]> source.dussan.org Git - vaadin-framework.git/commitdiff
fixes #3786, focus and blur events for (textual) datefields and combobox
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Wed, 2 Dec 2009 12:26:58 +0000 (12:26 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Wed, 2 Dec 2009 12:26:58 +0000 (12:26 +0000)
svn changeset:10131/svn branch:6.2

src/com/vaadin/terminal/gwt/client/ui/VFilterSelect.java
src/com/vaadin/terminal/gwt/client/ui/VTextualDate.java
src/com/vaadin/ui/DateField.java
src/com/vaadin/ui/Select.java
tests/src/com/vaadin/tests/components/FocusAndBlurListeners.java [new file with mode: 0644]

index ed0c338ce9646a63a81cddd91a556a8dd2135f04..2dcf7562ae7b0d55b1ff3d79056d7c32a8041b2c 100644 (file)
@@ -542,6 +542,8 @@ public class VFilterSelect extends Composite implements Paintable, Field,
     public static final int FILTERINGMODE_CONTAINS = 2;
 
     private static final String CLASSNAME = "v-filterselect";
+    public static final String FOCUS_EVENT_IDENTIFIER = "focus";
+    public static final String BLUR_EVENT_IDENTIFIER = "blur";
 
     protected int pageLength = 10;
 
@@ -1061,6 +1063,12 @@ public class VFilterSelect extends Composite implements Paintable, Field,
             setPromptingOff("");
         }
         addStyleDependentName("focus");
+
+        if (client.hasEventListeners(this, FOCUS_EVENT_IDENTIFIER)) {
+            client
+                    .updateVariable(paintableId, FOCUS_EVENT_IDENTIFIER, "",
+                            true);
+        }
     }
 
     public void onBlur(BlurEvent event) {
@@ -1080,6 +1088,10 @@ public class VFilterSelect extends Composite implements Paintable, Field,
             }
         }
         removeStyleDependentName("focus");
+
+        if (client.hasEventListeners(this, BLUR_EVENT_IDENTIFIER)) {
+            client.updateVariable(paintableId, BLUR_EVENT_IDENTIFIER, "", true);
+        }
     }
 
     public void focus() {
index ea91c5eac1fd4e3b7f6587b5f6bdc8698a245115..e71dd5c1400d377fdf1c102d6c5a6908292b1b46 100644 (file)
@@ -30,6 +30,8 @@ public class VTextualDate extends VDateField implements Paintable, Field,
 
     private static final String PARSE_ERROR_CLASSNAME = CLASSNAME
             + "-parseerror";
+    public static final String FOCUS_EVENT_IDENTIFIER = "focus";
+    public static final String BLUR_EVENT_IDENTIFIER = "blur";
 
     private final TextBox text;
 
@@ -56,12 +58,22 @@ public class VTextualDate extends VDateField implements Paintable, Field,
             public void onFocus(FocusEvent event) {
                 text.addStyleName(VTextField.CLASSNAME + "-"
                         + VTextField.CLASSNAME_FOCUS);
+                if (client != null
+                        && client.hasEventListeners(VTextualDate.this,
+                                FOCUS_EVENT_IDENTIFIER)) {
+                    client.updateVariable(id, FOCUS_EVENT_IDENTIFIER, "", true);
+                }
             }
         });
         text.addBlurHandler(new BlurHandler() {
             public void onBlur(BlurEvent event) {
                 text.removeStyleName(VTextField.CLASSNAME + "-"
                         + VTextField.CLASSNAME_FOCUS);
+                if (client != null
+                        && client.hasEventListeners(VTextualDate.this,
+                                BLUR_EVENT_IDENTIFIER)) {
+                    client.updateVariable(id, BLUR_EVENT_IDENTIFIER, "", true);
+                }
             }
         });
         add(text);
@@ -69,7 +81,6 @@ public class VTextualDate extends VDateField implements Paintable, Field,
 
     @Override
     public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
-
         int origRes = currentResolution;
         super.updateFromUIDL(uidl, client);
         if (origRes != currentResolution) {
@@ -93,6 +104,7 @@ public class VTextualDate extends VDateField implements Paintable, Field,
         } else {
             text.removeStyleDependentName("readonly");
         }
+
     }
 
     protected String getFormatString() {
@@ -343,4 +355,5 @@ public class VTextualDate extends VDateField implements Paintable, Field,
     public void focus() {
         text.setFocus(true);
     }
+
 }
index 425040a01697c0c509bd6ae4194831481c06426a..0800537fae0e0b3d72fcf1a02eac3e28432fefce 100644 (file)
@@ -12,9 +12,15 @@ import java.util.Locale;
 import java.util.Map;
 
 import com.vaadin.data.Property;
+import com.vaadin.event.FieldEvents;
+import com.vaadin.event.FieldEvents.BlurEvent;
+import com.vaadin.event.FieldEvents.BlurListener;
+import com.vaadin.event.FieldEvents.FocusEvent;
+import com.vaadin.event.FieldEvents.FocusListener;
 import com.vaadin.terminal.PaintException;
 import com.vaadin.terminal.PaintTarget;
 import com.vaadin.terminal.gwt.client.ui.VPopupCalendar;
+import com.vaadin.terminal.gwt.client.ui.VTextualDate;
 
 /**
  * <p>
@@ -36,7 +42,8 @@ import com.vaadin.terminal.gwt.client.ui.VPopupCalendar;
  */
 @SuppressWarnings("serial")
 @ClientWidget(VPopupCalendar.class)
-public class DateField extends AbstractField {
+public class DateField extends AbstractField implements
+        FieldEvents.BlurNotifier, FieldEvents.FocusNotifier {
 
     /* Private members */
 
@@ -85,6 +92,9 @@ public class DateField extends AbstractField {
      */
     protected static final String TYPE_INLINE = "inline";
 
+    private static final String BLUR_EVENT = VTextualDate.BLUR_EVENT_IDENTIFIER;
+    private static final String FOCUS_EVENT = VTextualDate.FOCUS_EVENT_IDENTIFIER;
+
     /**
      * Specified widget type.
      */
@@ -347,6 +357,14 @@ public class DateField extends AbstractField {
                 // updates itself
             }
         }
+
+        if (variables.containsKey(FOCUS_EVENT)) {
+            fireEvent(new FocusEvent(this));
+        }
+
+        if (variables.containsKey(BLUR_EVENT)) {
+            fireEvent(new BlurEvent(this));
+        }
     }
 
     /**
@@ -545,4 +563,22 @@ public class DateField extends AbstractField {
         return lenient;
     }
 
+    public void addListener(FocusListener listener) {
+        addListener(FOCUS_EVENT, FocusEvent.class, listener,
+                FocusListener.focusMethod);
+    }
+
+    public void removeListener(FocusListener listener) {
+        removeListener(FOCUS_EVENT, FocusEvent.class, listener);
+    }
+
+    public void addListener(BlurListener listener) {
+        addListener(BLUR_EVENT, BlurEvent.class, listener,
+                BlurListener.blurMethod);
+    }
+
+    public void removeListener(BlurListener listener) {
+        removeListener(BLUR_EVENT, BlurEvent.class, listener);
+    }
+
 }
index 48877d00f10b50dcd6c909d684b749fe89139993..e8e0dfebaab94a7220fb4a9143b404d6cf75ef1d 100644 (file)
@@ -13,6 +13,11 @@ import java.util.Map;
 import java.util.Set;
 
 import com.vaadin.data.Container;
+import com.vaadin.event.FieldEvents;
+import com.vaadin.event.FieldEvents.BlurEvent;
+import com.vaadin.event.FieldEvents.BlurListener;
+import com.vaadin.event.FieldEvents.FocusEvent;
+import com.vaadin.event.FieldEvents.FocusListener;
 import com.vaadin.terminal.PaintException;
 import com.vaadin.terminal.PaintTarget;
 import com.vaadin.terminal.Resource;
@@ -24,13 +29,13 @@ import com.vaadin.terminal.gwt.client.ui.VFilterSelect;
  * set of choices is presented as a set of {@link com.vaadin.data.Item}s in a
  * {@link com.vaadin.data.Container}.
  * </p>
- *
+ * 
  * <p>
  * A <code>Select</code> component may be in single- or multiselect mode.
  * Multiselect mode means that more than one item can be selected
  * simultaneously.
  * </p>
- *
+ * 
  * @author IT Mill Ltd.
  * @version
  * @VERSION@
@@ -38,7 +43,11 @@ import com.vaadin.terminal.gwt.client.ui.VFilterSelect;
  */
 @SuppressWarnings("serial")
 @ClientWidget(VFilterSelect.class)
-public class Select extends AbstractSelect implements AbstractSelect.Filtering {
+public class Select extends AbstractSelect implements AbstractSelect.Filtering,
+        FieldEvents.BlurNotifier, FieldEvents.FocusNotifier {
+
+    private static final String BLUR_EVENT_ID = VFilterSelect.BLUR_EVENT_IDENTIFIER;
+    private static final String FOCUS_EVENT_ID = VFilterSelect.FOCUS_EVENT_IDENTIFIER;
 
     /**
      * Holds value of property pageLength. 0 disables paging.
@@ -83,7 +92,7 @@ public class Select extends AbstractSelect implements AbstractSelect.Filtering {
 
     /**
      * Paints the content of this component.
-     *
+     * 
      * @param target
      *            the Paint Event.
      * @throws PaintException
@@ -229,14 +238,14 @@ public class Select extends AbstractSelect implements AbstractSelect.Filtering {
 
     /**
      * Makes correct sublist of given list of options.
-     *
+     * 
      * If paint is not an option request (affected by page or filter change),
      * page will be the one where possible selection exists.
-     *
+     * 
      * Detects proper first and last item in list to return right page of
      * options. Also, if the current page is beyond the end of the list, it will
      * be adjusted.
-     *
+     * 
      * @param options
      * @param needNullSelectOption
      *            flag to indicate if nullselect option needs to be taken into
@@ -335,12 +344,14 @@ public class Select extends AbstractSelect implements AbstractSelect.Filtering {
 
     /**
      * Invoked when the value of a variable has changed.
-     *
+     * 
      * @see com.vaadin.ui.AbstractComponent#changeVariables(java.lang.Object,
      *      java.util.Map)
      */
     @Override
     public void changeVariables(Object source, Map variables) {
+        // Not calling super.changeVariables due the history of select
+        // component hierarchy
 
         // Selection change
         if (variables.containsKey("selected")) {
@@ -404,11 +415,8 @@ public class Select extends AbstractSelect implements AbstractSelect.Filtering {
                 filterstring = filterstring.toLowerCase();
             }
             optionRepaint();
-            return;
-        }
-
-        // New option entered (and it is allowed)
-        if (isNewItemsAllowed()) {
+        } else if (isNewItemsAllowed()) {
+            // New option entered (and it is allowed)
             final String newitem = (String) variables.get("newitem");
             if (newitem != null && newitem.length() > 0) {
                 getNewItemHandler().addNewItem(newitem);
@@ -418,6 +426,13 @@ public class Select extends AbstractSelect implements AbstractSelect.Filtering {
             }
         }
 
+        if (variables.containsKey(FOCUS_EVENT_ID)) {
+            fireEvent(new FocusEvent(this));
+        }
+        if (variables.containsKey(BLUR_EVENT_ID)) {
+            fireEvent(new BlurEvent(this));
+        }
+
     }
 
     @Override
@@ -443,13 +458,13 @@ public class Select extends AbstractSelect implements AbstractSelect.Filtering {
     /**
      * Note, one should use more generic setWidth(String) method instead of
      * this. This now days actually converts columns to width with em css unit.
-     *
+     * 
      * Sets the number of columns in the editor. If the number of columns is set
      * 0, the actual number of displayed columns is determined implicitly by the
      * adapter.
-     *
+     * 
      * @deprecated
-     *
+     * 
      * @param columns
      *            the number of columns to set.
      */
@@ -474,4 +489,23 @@ public class Select extends AbstractSelect implements AbstractSelect.Filtering {
         return columns;
     }
 
+    public void addListener(BlurListener listener) {
+        addListener(BLUR_EVENT_ID, BlurEvent.class, listener,
+                BlurListener.blurMethod);
+    }
+
+    public void removeListener(BlurListener listener) {
+        removeListener(BLUR_EVENT_ID, BlurEvent.class, listener);
+    }
+
+    public void addListener(FocusListener listener) {
+        addListener(FOCUS_EVENT_ID, FocusEvent.class, listener,
+                FocusListener.focusMethod);
+    }
+
+    public void removeListener(FocusListener listener) {
+        removeListener(FOCUS_EVENT_ID, FocusEvent.class, listener);
+
+    }
+
 }
diff --git a/tests/src/com/vaadin/tests/components/FocusAndBlurListeners.java b/tests/src/com/vaadin/tests/components/FocusAndBlurListeners.java
new file mode 100644 (file)
index 0000000..32f1de7
--- /dev/null
@@ -0,0 +1,70 @@
+package com.vaadin.tests.components;
+
+import java.util.Date;
+
+import com.vaadin.event.FieldEvents.BlurEvent;
+import com.vaadin.event.FieldEvents.BlurListener;
+import com.vaadin.event.FieldEvents.FocusEvent;
+import com.vaadin.event.FieldEvents.FocusListener;
+import com.vaadin.ui.ComboBox;
+import com.vaadin.ui.DateField;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.Layout;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.VerticalLayout;
+
+public class FocusAndBlurListeners extends TestBase {
+
+    private FocusListener focusListener = new FocusListener() {
+
+        public void focus(FocusEvent event) {
+            Label msg = new Label(new Date() + " Focused "
+                    + event.getComponent().getCaption());
+            messages.addComponentAsFirst(msg);
+        }
+    };
+    private BlurListener blurListener = new BlurListener() {
+
+        public void blur(BlurEvent event) {
+            Label msg = new Label(new Date() + " Blurred "
+                    + event.getComponent().getCaption());
+            messages.addComponentAsFirst(msg);
+
+        }
+    };
+    private VerticalLayout messages = new VerticalLayout();
+
+    @Override
+    protected void setup() {
+        Layout l = getLayout();
+        TextField tf = new TextField("TextField");
+        l.addComponent(tf);
+        DateField df = new DateField("DateField");
+        l.addComponent(df);
+
+        ComboBox cb = new ComboBox("ComboBox");
+
+        l.addComponent(cb);
+
+        tf.addListener(focusListener);
+        tf.addListener(blurListener);
+        df.addListener(focusListener);
+        df.addListener(blurListener);
+        cb.addListener(focusListener);
+        cb.addListener(blurListener);
+
+        l.addComponent(messages);
+
+    }
+
+    @Override
+    protected String getDescription() {
+        return "Testing blur and focus listeners added in 6.2";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return null;
+    }
+
+}