001: /*
002: * Created on 30.11.2004
003: */
004: package de.jwic.controls;
005:
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.Map;
009: import java.util.StringTokenizer;
010:
011: import de.jwic.base.Field;
012: import de.jwic.base.IControlContainer;
013: import de.jwic.base.ValueChangedQueue;
014: import de.jwic.events.ValueChangedEvent;
015: import de.jwic.events.ValueChangedListener;
016:
017: /**
018: * CheckboxControl creates one or more checkboxes that can be multi selected.
019: *
020: * $Id: CheckboxControl.java,v 1.7 2007/04/03 09:35:28 cosote Exp $
021: * @version $Revision: 1.7 $
022: * @author JBornemann
023: */
024: public class CheckboxControl extends ListControl {
025:
026: private static final long serialVersionUID = 2L;
027:
028: private int columns = 0;
029:
030: public final static String FIELD_ELEMENT = "ELEMENT";
031: protected boolean changed = false;
032: protected String keyList = null;
033: protected String lastSelection = "";
034:
035: protected Map fieldKeyMap = new HashMap();
036: private ValueChangedListener valueChangedListener = null;
037:
038: /**
039: * @param container
040: */
041: public CheckboxControl(IControlContainer container) {
042: super (container, null);
043: init();
044: }
045:
046: /**
047: * @param container
048: * @param name
049: */
050: public CheckboxControl(IControlContainer container, String name) {
051: super (container, name);
052: init();
053: }
054:
055: /* (non-Javadoc)
056: * @see de.jwic.controls.ListControl#init()
057: */
058: private void init() {
059:
060: valueChangedListener = new ValueChangedListener() {
061: private static final long serialVersionUID = 1L;
062:
063: public void valueChanged(ValueChangedEvent event) {
064: // setting an empty string to replace 'null' does not invoke a selection.
065: if (!(event.getOldValues() == null && "".equals(event
066: .getNewValue()))) {
067: changed = true;
068: String selection = getSelectedKey();
069: if (!selection.equals(lastSelection)) {
070: lastSelection = selection;
071: sendElementSelectedEvent();
072: }
073: }
074: }
075: };
076: // remove the default field and recreate it to remove the listener.
077:
078: String name = field.getName();
079: field.destroy();
080: field = new Field(this , name);
081: field.setValue("");
082:
083: }
084:
085: /* (non-Javadoc)
086: * @see de.jwic.controls.ListControl#addElement(java.lang.String, java.lang.String)
087: */
088: public void addElement(String title, String key) {
089:
090: if (fieldKeyMap.containsKey(key)) {
091: throw new IllegalArgumentException("Dupplicate key: " + key);
092: }
093:
094: Field fld = new Field(this );
095: fld.addValueChangedListener(valueChangedListener);
096: fieldKeyMap.put(key, fld);
097:
098: super .addElement(title, key);
099:
100: }
101:
102: /* (non-Javadoc)
103: * @see de.jwic.controls.ListControl#clear()
104: */
105: public void clear() {
106: // remove all fields
107: for (Iterator it = fieldKeyMap.values().iterator(); it
108: .hasNext();) {
109: removeField((Field) it.next());
110: }
111: fieldKeyMap.clear();
112: changed = true;
113: sendElementSelectedEvent();
114: super .clear();
115: }
116:
117: /**
118: * Returns the field associated with the key.
119: */
120: public Field getFieldByKey(String key) {
121: return (Field) fieldKeyMap.get(key);
122: }
123:
124: /* (non-Javadoc)
125: * @see de.jwic.controls.ListControl#setSelectedKey(java.lang.String)
126: */
127: public void setSelectedKey(String newKey) {
128: // deselect all
129: // FLI: Use batch update to prevent listener invocation
130: ValueChangedQueue queue = new ValueChangedQueue();
131: for (Iterator it = getElements().iterator(); it.hasNext();) {
132: String key = ((ListEntry) it.next()).getKey();
133: Field fld = getFieldByKey(key);
134: if (fld != null) {
135: fld.batchUpdate(new String[] { "" }, queue);
136: }
137: }
138: if (!(newKey == null || newKey.length() == 0)) {
139: // regular selection
140: StringTokenizer st = new StringTokenizer(newKey, ";");
141: while (st.hasMoreElements()) {
142: String key = st.nextToken();
143: Field fld = getFieldByKey(key);
144: if (fld != null) {
145: fld.batchUpdate(new String[] { key }, queue);
146: }
147: }
148: }
149: // now send the event
150: changed = true;
151: lastSelection = newKey;
152: sendElementSelectedEvent();
153: requireRedraw();
154: }
155:
156: /* (non-Javadoc)
157: * @see de.jwic.controls.ListControl#getSelectedKey()
158: */
159: public String getSelectedKey() {
160: if (keyList == null || changed) {
161: String key = "";
162: for (Iterator it = getElements().iterator(); it.hasNext();) {
163: ListEntry entry = (ListEntry) it.next();
164: Field fld = getFieldByKey(entry.key);
165: if (fld != null && entry.key.equals(fld.getValue())) {
166: // element is selected
167: if (key.length() > 0) {
168: key += ";";
169: }
170: key += entry.key;
171: }
172: }
173: keyList = key;
174: changed = false;
175: }
176: return keyList;
177: }
178:
179: /* (non-Javadoc)
180: * @see de.jwic.controls.ListControl#getSelectedKeys()
181: */
182: public String[] getSelectedKeys() {
183:
184: String[] all = new String[getElements().size()];
185: int count = 0;
186: for (Iterator it = getElements().iterator(); it.hasNext();) {
187: ListEntry entry = (ListEntry) it.next();
188: Field fld = getFieldByKey(entry.key);
189: if (fld != null && entry.key.equals(fld.getValue())) {
190: // element is selected
191: all[count++] = entry.key;
192: }
193: }
194: String[] result = new String[count];
195: if (count > 0) {
196: System.arraycopy(all, 0, result, 0, count);
197: }
198: return result;
199:
200: }
201:
202: /* (non-Javadoc)
203: * @see de.jwic.controls.ListControl#removeElement(java.lang.String)
204: */
205: public int removeElement(String key) {
206:
207: boolean selectionChanged = false;
208: for (Iterator it = fieldKeyMap.keySet().iterator(); it
209: .hasNext();) {
210: String fKey = (String) it.next();
211: if (fKey.equals(key)) {
212: Field f = (Field) fieldKeyMap.get(fKey);
213: if (f.getValue().equals(fKey)) { // element was selectecd
214: selectionChanged = true;
215: }
216: it.remove();
217: }
218: }
219: changed = true;
220: if (selectionChanged) {
221: sendElementSelectedEvent();
222: }
223: requireRedraw();
224: return super .removeElement(key);
225: }
226:
227: /**
228: * Used by the velocity template to determine if a new line is required.
229: * @param count
230: * @return
231: */
232: public boolean isDoBreak(int count) {
233: return columns != 0 && count % columns == 0;
234: }
235:
236: /**
237: * @return Returns the columns.
238: */
239: public int getColumns() {
240: return columns;
241: }
242:
243: /**
244: * @param columns The columns to set.
245: */
246: public void setColumns(int columns) {
247: this .columns = columns;
248: requireRedraw();
249: }
250:
251: /* (non-Javadoc)
252: * @see de.jwic.controls.ListControl#isKeySelected(java.lang.String)
253: */
254: public boolean isKeySelected(String key) {
255: Field fld = getFieldByKey(key);
256: boolean selected = fld != null && key.equals(fld.getValue());
257: return selected;
258: }
259:
260: /* (non-Javadoc)
261: * @see de.jwic.controls.ListControl#getForceFocusElement()
262: */
263: public String getForceFocusElement() {
264: if (getElements().size() == 0) {
265: return null;
266: }
267: String[] selected = getSelectedKeys();
268: String key = ((ListEntry) getElements().iterator().next()).key;
269:
270: if (selected != null && selected.length > 0) {
271: key = selected[0];
272: }
273: Field fld = getFieldByKey(key);
274: return fld != null ? "chk_" + getControlID() + fld.getName()
275: : null;
276: }
277: }
|