01: /* RadiogroupSelectedItemConverter.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Mon Mar 12 11:05:43 2007, Created by Henri
10: }}IS_NOTE
11:
12: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: }}IS_RIGHT
16: */
17: package org.zkoss.zkplus.databind;
18:
19: import org.zkoss.zul.Radiogroup;
20: import org.zkoss.zul.Radio;
21:
22: import org.zkoss.zk.ui.Component;
23: import org.zkoss.zk.ui.UiException;
24: import org.zkoss.zk.ui.event.Event;
25: import org.zkoss.zk.ui.event.Events;
26: import org.zkoss.zk.ui.event.SelectEvent;
27:
28: import java.util.Set;
29: import java.util.HashSet;
30: import java.util.Iterator;
31:
32: /**
33: * Convert Radiogroup selected item to radio value and vice versa.
34: *
35: * @author Henri
36: */
37: public class RadiogroupSelectedItemConverter implements TypeConverter {
38: public Object coerceToUi(Object val, Component comp) { //load
39: if (val != null) {
40: //iterate to find the selected radio via the value
41: for (Iterator it = comp.getChildren().iterator(); it
42: .hasNext();) {
43: final Component child = (Component) it.next();
44: if (child instanceof Radio) {
45: if (val.equals(((Radio) child).getValue())) {
46: return child;
47: }
48: } else if (!(child instanceof Radiogroup)) { //skip nested radiogroup
49: return coerceToUi(val, comp);
50: }
51: }
52: }
53: return null;
54: }
55:
56: public Object coerceToBean(Object val, Component comp) { //save
57: return val != null ? ((Radio) val).getValue() : null;
58: }
59: }
|