/
InputBox.java
334 lines (307 loc) · 7.87 KB
/
InputBox.java
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* de.jwic.controls.InputBoxControl
* $Id: InputBoxControl.java,v 1.10 2010/01/25 11:03:10 lordsam Exp $
*/
package de.jwic.controls;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import de.jwic.base.Control;
import de.jwic.base.Field;
import de.jwic.base.IControlContainer;
import de.jwic.base.JavaScriptSupport;
import de.jwic.base.Page;
import de.jwic.base.SessionContext;
import de.jwic.events.KeyEvent;
import de.jwic.events.KeyListener;
import de.jwic.events.ValueChangedListener;
/**
* An InputBox is a control where the user can enter data.
* @author Florian Lippisch
* @version $Revision: 1.10 $
*/
@JavaScriptSupport
public class InputBox extends HTMLElement {
private static final long serialVersionUID = 1L;
private final static String ACTION_KEYPRESSED = "keyPressed";
protected int maxLength = 0;
protected int rows = 1;
protected int cols = 20;
protected boolean multiLine = false;
protected boolean readonly = false;
protected boolean password = false;
protected boolean updateOnBlur = false;
protected int listenKeyCode = 0; // 0 == dont listen to any keycode
protected String emptyInfoText = null;
protected boolean flagAsError = false;
protected Field field = null;
private List<KeyListener> keyListeners = null;
/**
* @param container
*/
public InputBox(IControlContainer container) {
super(container, null);
init();
}
/**
* @param container
* @param name
*/
public InputBox(IControlContainer container, String name) {
super(container, name);
init();
}
/* (non-Javadoc)
* @see de.jwic.base.IControl#actionPerformed(java.lang.String, java.lang.String)
*/
public void actionPerformed(String actionId, String parameter) {
if (actionId.equals(ACTION_KEYPRESSED)) {
// notify listeners
if (keyListeners != null) {
KeyEvent event = new KeyEvent(this, Integer.parseInt(parameter));
for (Iterator<KeyListener> it = keyListeners.iterator(); it.hasNext(); ) {
KeyListener listener = it.next();
listener.keyPressed(event);
}
}
}
}
/**
* Add a value changed listener to the <b>field</b> used by this
* input box control.
* @param listener
*/
public void addValueChangedListener(ValueChangedListener listener) {
field.addValueChangedListener(listener);
}
/**
* Add a keyListener. The listener(s) are invoked when the key that is specified
* in the <code>listenKeyCode</code> property is pressed. If no <code>listenKeyCode</code>
* is specified, the listener will never be invoked.
* @param keyListener
*/
public void addKeyListener(KeyListener keyListener) {
if (keyListeners == null) {
keyListeners = new ArrayList<KeyListener>();
}
keyListeners.add(keyListener);
}
/* (non-Javadoc)
* @see de.jwic.base.Control#init()
*/
private void init() {
setCssClass("ui-widget ui-widget-content j-inputbox");
field = new Field(this);
}
/**
* Forces focus for this field. Returns <code>true</code> if the
* field Id could have been set.
*/
public boolean forceFocus() {
// Check if the current top-control is a parent of this input box.
// if so, force focus.
SessionContext context = getSessionContext();
Control topCtrl = context.getTopControl();
if (topCtrl == null) {
// initialization phase -> walk up the control hieracy to find a Page control
IControlContainer container = getContainer();
while (container != null && !(container instanceof SessionContext)) {
Control ctrl = (Control)container;
if (ctrl instanceof Page) {
topCtrl = ctrl;
break;
}
container = ctrl.getContainer();
}
}
if (topCtrl != null && getControlID().startsWith(topCtrl.getControlID() + ".")) {
if (topCtrl instanceof Page) {
Page page = (Page)topCtrl;
page.setForceFocusElement(field.getId());
return true;
}
}
return false;
}
/**
* The field representation used by the InputBoxControl on the HTML form.
* This property is used by the renderer to generate the proper HTML code.
* Use the <code>text</code> property to change the InputBoxControls data.
* @return
*/
public Field getField() {
return field;
}
/**
* Returns the text in the textbox.
* @return
*/
public String getText() {
return field.getValue();
}
/**
* Set the text of the control.
* @param newText
*/
public void setText(String newText) {
field.setValue(newText);
setRequireRedraw(true);
}
/**
* @return Returns the maxLength.
*/
public int getMaxLength() {
return maxLength;
}
/**
* Set to 0 if the length should be unlimited.
* @param maxLength The maxLength to set.
*/
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
requireRedraw();
}
/**
* @return Returns the multiLine.
*/
public boolean isMultiLine() {
return multiLine;
}
/**
* @param multiLine The multiLine to set.
*/
public void setMultiLine(boolean multiLine) {
this.multiLine = multiLine;
requireRedraw();
}
/**
* @return Returns the rows.
*/
public int getRows() {
return rows;
}
/**
* @param rows The rows to set.
*/
public void setRows(int rows) {
this.rows = rows;
requireRedraw();
}
/**
* @return Returns the cols.
*/
public int getCols() {
return cols;
}
/**
* @param cols The cols to set.
*/
public void setCols(int cols) {
this.cols = cols;
requireRedraw();
}
/**
* @return Returns true if the input field is of type "password".
*/
public boolean isPassword() {
return password;
}
/**
* Set to true if the input field should be of type "password". This property
* applies only to non-multiline fields.
* @param password boolean
*/
public void setPassword(boolean password) {
this.password = password;
requireRedraw();
}
/**
* Removes the specified listener from the <b>field</b> used by this input box control.
* @param listener
*/
public void removeValueChangedListener(ValueChangedListener listener) {
field.removeValueChangedListener(listener);
}
/**
* Removes the specified listener from the <b>field</b> used by this input box control.
* @param listener
*/
public void removeKeyListener(KeyListener listener) {
if (keyListeners != null) {
keyListeners.remove(listener);
}
}
/**
* @return Returns the listenKeyCode.
*/
public int getListenKeyCode() {
return listenKeyCode;
}
/**
* @param listenKeyCode The listenKeyCode to set.
*/
public void setListenKeyCode(int listenKeyCode) {
this.listenKeyCode = listenKeyCode;
}
/**
* @return Returns the readonly.
*/
public boolean isReadonly() {
return readonly;
}
/**
* @param readonly The readonly to set.
*/
public void setReadonly(boolean readonly) {
this.readonly = readonly;
requireRedraw();
}
/**
* @see #setUpdateOnBlur(boolean)
* @return the updateOnBlur
*/
public boolean isUpdateOnBlur() {
return updateOnBlur;
}
/**
* The control initiates a submit of the form wich updates the field value when the
* focus on the control is lost (OnBlur).
* @param updateOnBlur the updateOnBlur to set
*/
public void setUpdateOnBlur(boolean updateOnBlur) {
if (updateOnBlur != this.updateOnBlur) {
this.updateOnBlur = updateOnBlur;
requireRedraw();
}
}
/**
* @return the emptyInfoText
*/
public String getEmptyInfoText() {
return emptyInfoText;
}
/**
* Set a text that is displayed when the text field is empty. If empty, the
* css class 'empty' is added to indicate visually that the field is empty.
* @param emptyInfoText the emptyInfoText to set
*/
public void setEmptyInfoText(String emptyInfoText) {
this.emptyInfoText = emptyInfoText;
requireRedraw();
}
/**
* @return the flagAsError
*/
public boolean isFlagAsError() {
return flagAsError;
}
/**
* @param flagAsError the flagAsError to set
*/
public void setFlagAsError(boolean flagAsError) {
if (this.flagAsError != flagAsError) {
this.flagAsError = flagAsError;
requireRedraw();
}
}
}