01: /*
02: * MyGWT Widget Library
03: * Copyright(c) 2007, MyGWT.
04: * licensing@mygwt.net
05: *
06: * http://mygwt.net/license
07: */
08: package net.mygwt.ui.client.widget;
09:
10: import net.mygwt.ui.client.event.BaseEvent;
11: import net.mygwt.ui.client.event.Listener;
12: import net.mygwt.ui.client.util.DelayedTask;
13:
14: import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
15: import com.google.gwt.user.client.ui.TextBox;
16: import com.google.gwt.user.client.ui.Widget;
17:
18: /**
19: * A base class for text box's that monitor key presses using a
20: * <code>DelayedTask</code>.
21: */
22: public class KeyPressTextBox extends TextBox {
23:
24: private int delay = 300;
25: private DelayedTask task;
26:
27: /**
28: * Creates a new key press text box.
29: */
30: public KeyPressTextBox() {
31: task = new DelayedTask(new Listener() {
32: public void handleEvent(BaseEvent be) {
33: onChange();
34: setFocus(true);
35: }
36: });
37:
38: addKeyboardListener(new KeyboardListenerAdapter() {
39: public void onKeyUp(Widget sender, char keyCode,
40: int modifiers) {
41: task.delay(delay);
42: }
43: });
44: }
45:
46: /**
47: * Returns the delay.
48: *
49: * @return the delay in milliseconds
50: */
51: public int getDelay() {
52: return delay;
53: }
54:
55: /**
56: * Sets the delay. Default value is 300.
57: *
58: * @param delay the delay in milliseconds
59: */
60: public void setDelay(int delay) {
61: this .delay = delay;
62: }
63:
64: /**
65: * Subclasses should override as needed.
66: */
67: protected void onChange() {
68:
69: }
70:
71: }
|