summaryrefslogtreecommitdiffstats
path: root/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/HintTextBox.java
blob: 09550a6819ecc0f4503a226fe32e12857569a090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.client.ui;

import com.google.gerrit.client.Gerrit;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.SuggestBox.DefaultSuggestionDisplay;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwtexpui.globalkey.client.NpTextBox;


public class HintTextBox extends NpTextBox {
  private HandlerRegistration hintFocusHandler;
  private HandlerRegistration hintBlurHandler;
  private HandlerRegistration keyDownHandler;

  private String hintText;
  private String hintStyleName = Gerrit.RESOURCES.css().inputFieldTypeHint();

  private String prevText;

  private boolean hintOn;
  private boolean isFocused;


  public String getText() {
    if (hintOn) {
      return "";
    }
    return super.getText();
  }

  public void setText(String text) {
    focusHint();

    super.setText(text);
    prevText = text;

    if (! isFocused) {
      blurHint();
    }
  }

  public String getHintText() {
    return hintText;
  }

  public void setHintText(String text) {
    if (text == null) {
      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;
      keyDownHandler.removeHandler();
      keyDownHandler = null;
      hintText = null;
      focusHint();

      return;
    }

    // Setting Hints

    if (hintText == null) { // first time (was not already set)
      hintText = text;

      hintFocusHandler = addFocusHandler(new FocusHandler() {
          @Override
          public void onFocus(FocusEvent event) {
            focusHint();
            prevText = getText();
            isFocused = true;
          }
        });

      hintBlurHandler = addBlurHandler(new BlurHandler() {
          @Override
          public void onBlur(BlurEvent event) {
            blurHint();
            isFocused = false;
          }
        });

      /*
      * 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!
      *
      *  A counter to this bug seems to be to force setFocus(false) on ESC.
      */

      /* Chrome does not create a KeyPressEvent on ESC, so use KeyDownEvents */
      keyDownHandler = addKeyDownHandler(new KeyDownHandler() {
          @Override
          public void onKeyDown(final KeyDownEvent event) {
            onKey(event.getNativeKeyCode());
          }
        });

    } else { // Changing an already set Hint

      focusHint();
      hintText = text;
    }

    if (! isFocused) {
      blurHint();
    }
  }

  private void onKey(int key) {
    if (key == KeyCodes.KEY_ESCAPE) {
      setText(prevText);

      Widget p = getParent();
      if (p instanceof SuggestBox) {
        // Since the text was changed, ensure that the SuggestBox is
        // aware of this change so that it will refresh properly on
        // the next keystroke.  Without this, if the first keystroke
        // recreates the same string as before ESC was pressed, the
        // SuggestBox will think that the string has not changed, and
        // it will not yet provide any Suggestions.
        ((SuggestBox)p).showSuggestionList();

        // The suggestion list lingers if we don't hide it.
        ((DefaultSuggestionDisplay) ((SuggestBox) p).getSuggestionDisplay())
            .hideSuggestions();
      }

      setFocus(false);
    }
  }

  public void setHintStyleName(String styleName) {
    if (hintStyleName != null && hintOn) {
      removeStyleName(hintStyleName);
    }

    hintStyleName = styleName;

    if (styleName != null && hintOn) {
      addStyleName(styleName);
    }
  }

  public String getHintStyleName() {
    return hintStyleName;
  }

  protected void blurHint() {
    if (! hintOn && getHintText() != null && "".equals(super.getText())) {
      hintOn = true;
      super.setText(getHintText());
      if (getHintStyleName() != null) {
        addStyleName(getHintStyleName());
      }
    }
  }

  protected void focusHint() {
    if (hintOn) {
      super.setText("");
      if (getHintStyleName() != null) {
        removeStyleName(getHintStyleName());
      }
      hintOn = false;
    }
  }

  public void setFocus(boolean focus) {
    super.setFocus(focus);

    if (focus != isFocused) {
      if (focus) {
        focusHint();
      } else {
        blurHint();
      }
    }

    isFocused = focus;
  }
}