From abc5dac876a43dc3f625ba0578c7c36a67320114 Mon Sep 17 00:00:00 2001 From: Martin Fick Date: Wed, 1 Sep 2010 16:06:35 -0600 Subject: [PATCH] Fix 2 HintTextBox bugs The first bug is manifested while adding a second reviewer by pressing enter, the hint is replaced and is edited. Do not assume that the textfield is blurred when the setText() method is called on HintTextBox. This prevents the hint text from being reinserted and edited if the field had focus. The second bug is one where pressing ESC while in the text filed without modifying it places the hint text in the field and stays in edit mode. This seems to be a bug in the GWT implementation and is worked around with a hack. Change-Id: If615352819cc0cf2f461b2d49829ca44fa888c2f --- .../google/gerrit/client/ui/HintTextBox.java | 90 +++++++++++++------ 1 file changed, 65 insertions(+), 25 deletions(-) diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/HintTextBox.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/HintTextBox.java index 245ea2fbcc..9656e233bf 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/HintTextBox.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/HintTextBox.java @@ -23,12 +23,30 @@ import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwtexpui.globalkey.client.NpTextBox; public class HintTextBox extends NpTextBox { - private String hintText; - private String hintStyleName = Gerrit.RESOURCES.css().inputFieldTypeHint(); - private boolean hintOn; private HandlerRegistration hintFocusHandler; private HandlerRegistration hintBlurHandler; + private String hintText; + private String hintStyleName = Gerrit.RESOURCES.css().inputFieldTypeHint(); + + private boolean hintOn; + private boolean isFocused; + + /* + * There seems to be a strange bug (at least on firefox 3.5.9 ubuntu) with + * the textbox under the following circumstances: + * 1) The field is not focused with BText in it. + * 2) The field receives focus and a focus listener changes the text to FText + * 3) The ESC key is pressed and the value of the field has not changed + * (ever) from FText + * 4) BUG: The text value gets reset to BText! + * + * To counter this bug, force all focus events to first force a blur event. + * For some reason, this seems to prevent the bug. + */ + + private boolean bugStopping = false; + public String getText() { if (hintOn) { return ""; @@ -38,8 +56,12 @@ public class HintTextBox extends NpTextBox { public void setText(String text) { focusHint(); + super.setText(text); - blurHint(); + + if (! isFocused) { + blurHint(); + } } public String getHintText() { @@ -48,17 +70,18 @@ public class HintTextBox extends NpTextBox { public void setHintText(String text) { if (text == null) { - - // Clearing Hints - - if (hintText != null) { // was set - hintFocusHandler.removeHandler(); - hintFocusHandler = null; - hintBlurHandler.removeHandler(); - hintBlurHandler = null; - hintText = null; - focusHint(); + if (hintText == null) { // was not set, still not set, no change. + return; } + + // Clearing a previously set Hint + hintFocusHandler.removeHandler(); + hintFocusHandler = null; + hintBlurHandler.removeHandler(); + hintBlurHandler = null; + hintText = null; + focusHint(); + return; } @@ -70,26 +93,25 @@ public class HintTextBox extends NpTextBox { hintFocusHandler = addFocusHandler(new FocusHandler() { @Override public void onFocus(FocusEvent event) { - focusHint(); + setFocus(true); } }); hintBlurHandler = addBlurHandler(new BlurHandler() { @Override public void onBlur(BlurEvent event) { - blurHint(); + setFocus(false); } }); - blurHint(); - return; + } else { // Changing an already set Hint + + focusHint(); + hintText = text; } - // Changing Hint - - hintText = text; - if (hintOn) { - super.setText(text); + if (! isFocused) { + blurHint(); } } @@ -111,8 +133,8 @@ public class HintTextBox extends NpTextBox { protected void blurHint() { if (! hintOn && getHintText() != null && "".equals(super.getText())) { - super.setText(getHintText()); hintOn = true; + super.setText(getHintText()); if (getHintStyleName() != null) { addStyleName(getHintStyleName()); } @@ -122,10 +144,28 @@ public class HintTextBox extends NpTextBox { protected void focusHint() { if (hintOn) { super.setText(""); - hintOn = false; if (getHintStyleName() != null) { removeStyleName(getHintStyleName()); } + hintOn = false; } } + + public void setFocus(boolean focus) { + if (focus) { + bugStopping = true; + super.setFocus(!focus); + bugStopping = false; + } + + super.setFocus(focus); + + if (focus) { + focusHint(); + } else if (!bugStopping) { + blurHint(); + } + + isFocused = focus; + } }