001: /*
002: * Copyright 2005 jWic group (http://www.jwic.de)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: * de.jwic.base.Field
017: * Created on 28.08.2005
018: * $Id: Field.java,v 1.2 2006/08/14 09:34:59 lordsam Exp $
019: */
020: package de.jwic.base;
021:
022: import java.io.Serializable;
023: import java.util.Arrays;
024:
025: import de.jwic.events.ValueChangedEvent;
026: import de.jwic.events.ValueChangedListener;
027: import de.jwic.util.StringTool;
028:
029: /**
030: * Represents a field on an HTML form that contains a string value or an array
031: * of string values. A field is created for a control and is linked to it with
032: * its name.<p>
033: * The field offers an ValueChangedEvent.
034: *
035: * @author Florian Lippisch
036: * @version $Revision: 1.2 $
037: */
038: public class Field implements Serializable {
039:
040: private static final long serialVersionUID = 1L;
041:
042: private String name = null;
043: private Control control = null;
044: private String[] values = null;
045:
046: private ValueChangedListener[] listeners = null;
047:
048: /**
049: * Creates a new field with an autogenerated name.
050: * @param parent
051: */
052: public Field(Control parent) {
053: this (parent, null);
054: }
055:
056: /**
057: * Creates a new field with the given name.
058: * @param parent
059: * @param name
060: */
061: public Field(Control parent, String name) {
062: this .control = parent;
063: this .name = name;
064: control.addField(this );
065: }
066:
067: /**
068: * Removes the field from its container.
069: *
070: */
071: public void destroy() {
072: if (control != null) {
073: control.removeField(this );
074: }
075: }
076:
077: /**
078: * Add a ValueChangedListener to this field.
079: * @param listener
080: */
081: public synchronized void addValueChangedListener(
082: ValueChangedListener listener) {
083: if (listeners == null) {
084: listeners = new ValueChangedListener[] { listener };
085: } else {
086: ValueChangedListener[] tmp = new ValueChangedListener[listeners.length + 1];
087: System.arraycopy(listeners, 0, tmp, 0, listeners.length);
088: tmp[listeners.length] = listener;
089: listeners = tmp;
090: }
091: }
092:
093: /**
094: * Removes the specified listener from the field.
095: * @param listener
096: */
097: public synchronized void removeValueChangedListener(
098: ValueChangedListener listener) {
099: if (listeners != null && listeners.length > 0) {
100: ValueChangedListener[] tmp = new ValueChangedListener[listeners.length - 1];
101: int idx = 0;
102: for (int i = 0; i < listeners.length; i++) {
103: if (listeners[i] != listener) {
104: if (idx == tmp.length) {
105: // the listener was not registerd
106: return; // early exit
107: }
108: tmp[idx++] = listeners[i];
109: }
110: }
111: listeners = tmp;
112: }
113: }
114:
115: /**
116: * Fires the value changed event.
117: * @param event
118: */
119: protected void fireValueChangedEvent(ValueChangedEvent event) {
120: if (listeners != null) {
121: for (int i = 0; i < listeners.length; i++) {
122: listeners[i].valueChanged(event);
123: }
124: }
125: }
126:
127: /**
128: * @return Returns the name.
129: */
130: public String getName() {
131: return name;
132: }
133:
134: /**
135: * @param name The name to set.
136: */
137: void setName(String name) {
138: this .name = name;
139: }
140:
141: /**
142: * Returns the id of the field. This property must return a unique id of
143: * the field that can be used to identify the field when it is submited
144: * by the browser.
145: * @return
146: */
147: public String getId() {
148: return "fld_" + control.getControlID() + "." + name;
149: }
150:
151: /**
152: * Returns the values as a string. If the value is an array, the values
153: * are seperated by a semmicolon.
154: * @return
155: */
156: public String getValue() {
157: return StringTool.getSingleString(values);
158: }
159:
160: /**
161: * Returns the values as array.
162: * @return Returns the values.
163: */
164: public String[] getValues() {
165: return values;
166: }
167:
168: /**
169: * Set the value of the field.
170: * @param value
171: */
172: public void setValue(String value) {
173: setValues(new String[] { value });
174: }
175:
176: /**
177: * Sets the values as array.
178: * @param values The values to set.
179: */
180: public void setValues(String[] values) {
181: if (!Arrays.equals(this .values, values)) {
182: ValueChangedEvent event = new ValueChangedEvent(this ,
183: this .values, values);
184: this .values = values;
185: fireValueChangedEvent(event);
186: }
187: }
188:
189: /**
190: * Set the values of the field as array without fireing the ValueChangedEvent. The
191: * event is added to the ValueChangedQueue for later processing.
192: * @param values
193: * @param queue
194: */
195: public void batchUpdate(String[] newValues, ValueChangedQueue queue) {
196: if (!Arrays.equals(this .values, newValues)) {
197: ValueChangedEvent event = new ValueChangedEvent(this,
198: this.values, newValues);
199: this.values = newValues;
200: if (listeners != null) {
201: queue.valueChanged(listeners, event);
202: }
203: }
204: }
205:
206: }
|