From 18feee13c3ddaeee8e05ed7971de591bc4eadd14 Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Wed, 2 Dec 2009 12:26:58 +0000 Subject: [PATCH] fixes #3786, focus and blur events for (textual) datefields and combobox svn changeset:10131/svn branch:6.2 --- .../terminal/gwt/client/ui/VFilterSelect.java | 12 ++++ .../terminal/gwt/client/ui/VTextualDate.java | 15 +++- src/com/vaadin/ui/DateField.java | 38 +++++++++- src/com/vaadin/ui/Select.java | 66 ++++++++++++----- .../components/FocusAndBlurListeners.java | 70 +++++++++++++++++++ 5 files changed, 183 insertions(+), 18 deletions(-) create mode 100644 tests/src/com/vaadin/tests/components/FocusAndBlurListeners.java diff --git a/src/com/vaadin/terminal/gwt/client/ui/VFilterSelect.java b/src/com/vaadin/terminal/gwt/client/ui/VFilterSelect.java index ed0c338ce9..2dcf7562ae 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VFilterSelect.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VFilterSelect.java @@ -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() { diff --git a/src/com/vaadin/terminal/gwt/client/ui/VTextualDate.java b/src/com/vaadin/terminal/gwt/client/ui/VTextualDate.java index ea91c5eac1..e71dd5c140 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VTextualDate.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VTextualDate.java @@ -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); } + } diff --git a/src/com/vaadin/ui/DateField.java b/src/com/vaadin/ui/DateField.java index 425040a016..0800537fae 100644 --- a/src/com/vaadin/ui/DateField.java +++ b/src/com/vaadin/ui/DateField.java @@ -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; /** *

@@ -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); + } + } diff --git a/src/com/vaadin/ui/Select.java b/src/com/vaadin/ui/Select.java index 48877d00f1..e8e0dfebaa 100644 --- a/src/com/vaadin/ui/Select.java +++ b/src/com/vaadin/ui/Select.java @@ -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}. *

- * + * *

* A Select component may be in single- or multiselect mode. * Multiselect mode means that more than one item can be selected * simultaneously. *

- * + * * @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 index 0000000000..32f1de752a --- /dev/null +++ b/tests/src/com/vaadin/tests/components/FocusAndBlurListeners.java @@ -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; + } + +} -- 2.39.5