001: /*
002: * de.jwic.controls.InputBoxControl
003: * $Id: InputBoxControl.java,v 1.3 2006/08/09 14:52:40 lordsam Exp $
004: */
005: package de.jwic.controls;
006:
007: import java.util.ArrayList;
008: import java.util.Iterator;
009: import java.util.List;
010:
011: import de.jwic.base.Control;
012: import de.jwic.base.Field;
013: import de.jwic.base.IControlContainer;
014: import de.jwic.base.Page;
015: import de.jwic.base.SessionContext;
016: import de.jwic.events.KeyEvent;
017: import de.jwic.events.KeyListener;
018: import de.jwic.events.ValueChangedListener;
019:
020: /**
021: * An InputBox is a control where the user can enter data.
022: * @author Florian Lippisch
023: * @version $Revision: 1.3 $
024: */
025: public class InputBoxControl extends HTMLElement {
026:
027: private static final long serialVersionUID = 1L;
028: private final static String ACTION_KEYPRESSED = "keyPressed";
029:
030: protected int maxLength = 0;
031: protected int rows = 1;
032: protected int cols = 20;
033: protected boolean multiLine = false;
034: protected boolean readonly = false;
035: protected boolean password = false;
036: protected int listenKeyCode = 0; // 0 == dont listen to any keycode
037:
038: protected Field field = null;
039:
040: private List keyListeners = null;
041:
042: /**
043: * @param container
044: */
045: public InputBoxControl(IControlContainer container) {
046: super (container, null);
047: init();
048: }
049:
050: /**
051: * @param container
052: * @param name
053: */
054: public InputBoxControl(IControlContainer container, String name) {
055: super (container, name);
056: init();
057: }
058:
059: /* (non-Javadoc)
060: * @see de.jwic.base.IControl#actionPerformed(java.lang.String, java.lang.String)
061: */
062: public void actionPerformed(String actionId, String parameter) {
063:
064: if (actionId.equals(ACTION_KEYPRESSED)) {
065: // notify listeners
066: if (keyListeners != null) {
067: KeyEvent event = new KeyEvent(this , Integer
068: .parseInt(parameter));
069: for (Iterator it = keyListeners.iterator(); it
070: .hasNext();) {
071: KeyListener listener = (KeyListener) it.next();
072: listener.keyPressed(event);
073: }
074: }
075: }
076: }
077:
078: /**
079: * Add a value changed listener to the <b>field</b> used by this
080: * input box control.
081: * @param listener
082: */
083: public void addValueChangedListener(ValueChangedListener listener) {
084: field.addValueChangedListener(listener);
085: }
086:
087: /**
088: * Add a keyListener. The listener(s) are invoked when the key that is specified
089: * in the <code>listenKeyCode</code> property is pressed. If no <code>listenKeyCode</code>
090: * is specified, the listener will never be invoked.
091: * @param keyListener
092: */
093: public void addKeyListener(KeyListener keyListener) {
094: if (keyListeners == null) {
095: keyListeners = new ArrayList();
096: }
097: keyListeners.add(keyListener);
098: }
099:
100: /* (non-Javadoc)
101: * @see de.jwic.base.Control#init()
102: */
103: private void init() {
104: field = new Field(this );
105: }
106:
107: /**
108: * Forces focus for this field. Returns <code>true</code> if the
109: * field Id could have been set.
110: */
111: public boolean forceFocus() {
112:
113: // Check if the current top-control is a parent of this input box.
114: // if so, force focus.
115: SessionContext context = getSessionContext();
116: Control topCtrl = context.getTopControl();
117: if (topCtrl == null) {
118: // initialization phase -> walk up the control hieracy to find a Page control
119: IControlContainer container = getContainer();
120: while (container != null
121: && !(container instanceof SessionContext)) {
122: Control ctrl = (Control) container;
123: if (ctrl instanceof Page) {
124: topCtrl = ctrl;
125: break;
126: }
127: container = ctrl.getContainer();
128: }
129: }
130: if (topCtrl != null
131: && getControlID().startsWith(
132: topCtrl.getControlID() + ".")) {
133: if (topCtrl instanceof Page) {
134: Page page = (Page) topCtrl;
135: page.setForceFocusElement(field.getId());
136: return true;
137: }
138: }
139: return false;
140: }
141:
142: /**
143: * The field representation used by the InputBoxControl on the HTML form.
144: * This property is used by the renderer to generate the proper HTML code.
145: * Use the <code>text</code> property to change the InputBoxControls data.
146: * @return
147: */
148: public Field getField() {
149: return field;
150: }
151:
152: /**
153: * Returns the text in the textbox.
154: * @return
155: */
156: public String getText() {
157: return field.getValue();
158: }
159:
160: /**
161: * Set the text of the control.
162: * @param newText
163: */
164: public void setText(String newText) {
165: field.setValue(newText);
166: setRequireRedraw(true);
167: }
168:
169: /**
170: * @return Returns the maxLength.
171: */
172: public int getMaxLength() {
173: return maxLength;
174: }
175:
176: /**
177: * Set to 0 if the length should be unlimited.
178: * @param maxLength The maxLength to set.
179: */
180: public void setMaxLength(int maxLength) {
181: this .maxLength = maxLength;
182: requireRedraw();
183: }
184:
185: /**
186: * @return Returns the multiLine.
187: */
188: public boolean isMultiLine() {
189: return multiLine;
190: }
191:
192: /**
193: * @param multiLine The multiLine to set.
194: */
195: public void setMultiLine(boolean multiLine) {
196: this .multiLine = multiLine;
197: requireRedraw();
198: }
199:
200: /**
201: * @return Returns the rows.
202: */
203: public int getRows() {
204: return rows;
205: }
206:
207: /**
208: * @param rows The rows to set.
209: */
210: public void setRows(int rows) {
211: this .rows = rows;
212: requireRedraw();
213: }
214:
215: /**
216: * @return Returns the cols.
217: */
218: public int getCols() {
219: return cols;
220: }
221:
222: /**
223: * @param cols The cols to set.
224: */
225: public void setCols(int cols) {
226: this .cols = cols;
227: requireRedraw();
228: }
229:
230: /**
231: * @return Returns true if the input field is of type "password".
232: */
233: public boolean isPassword() {
234: return password;
235: }
236:
237: /**
238: * Set to true if the input field should be of type "password". This property
239: * applies only to non-multiline fields.
240: * @param password boolean
241: */
242: public void setPassword(boolean password) {
243: this .password = password;
244: requireRedraw();
245: }
246:
247: /**
248: * Removes the specified listener from the <b>field</b> used by this input box control.
249: * @param listener
250: */
251: public void removeValueChangedListener(ValueChangedListener listener) {
252: field.removeValueChangedListener(listener);
253: }
254:
255: /**
256: * Removes the specified listener from the <b>field</b> used by this input box control.
257: * @param listener
258: */
259: public void removeKeyListener(KeyListener listener) {
260: if (keyListeners != null) {
261: keyListeners.remove(listener);
262: }
263: }
264:
265: /**
266: * @return Returns the listenKeyCode.
267: */
268: public int getListenKeyCode() {
269: return listenKeyCode;
270: }
271:
272: /**
273: * @param listenKeyCode The listenKeyCode to set.
274: */
275: public void setListenKeyCode(int listenKeyCode) {
276: this .listenKeyCode = listenKeyCode;
277: }
278:
279: /**
280: * @return Returns the readonly.
281: */
282: public boolean isReadonly() {
283: return readonly;
284: }
285:
286: /**
287: * @param readonly The readonly to set.
288: */
289: public void setReadonly(boolean readonly) {
290: this.readonly = readonly;
291: requireRedraw();
292: }
293:
294: }
|