01: package fr.aliacom.form.common;
02:
03: import java.util.ArrayList;
04:
05: import fr.aliacom.common.ui.IRadioButton;
06: import fr.aliacom.form.common.events.IGroupListener;
07: import fr.aliacom.form.common.events.IRadioSelectionListener;
08:
09: /**
10: * @author tom
11: *
12: * (C) 2001, 2003 Thomas Cataldo
13: */
14: public final class ButtonGroup implements IFormComponent,
15: IRadioSelectionListener {
16:
17: private ArrayList buttons;
18: private ArrayList listeners;
19:
20: public ButtonGroup() {
21: buttons = new ArrayList();
22: listeners = new ArrayList();
23: }
24:
25: public void add(IRadioButton irb) {
26: buttons.add(irb);
27: if (buttons.size() == 1) {
28: irb.setSelected(true);
29: } else {
30: irb.setSelected(false);
31: }
32: irb.addSelectionListener(this );
33: }
34:
35: public void addGroupListener(IGroupListener listener) {
36: listeners.add(listener);
37: }
38:
39: public void removeGroupListener(IGroupListener listener) {
40: listeners.remove(listener);
41: }
42:
43: private void fireGroupEvent(String action) {
44: for (int i = 0, n = listeners.size(); i < n; i++) {
45: ((IGroupListener) listeners.get(i)).buttonSelected(action);
46: }
47: }
48:
49: /**
50: * @see fr.aliacom.form.common.IFormComponent#getNativeWidget()
51: */
52: public Object getNativeWidget() {
53: return null;
54: }
55:
56: /**
57: * @see fr.aliacom.form.common.IFormComponent#reset()
58: */
59: public void reset() {
60: }
61:
62: /**
63: * @see fr.aliacom.form.common.IFormComponent#setValueBean(java.lang.Object)
64: */
65: public void setValueBean(Object bean) {
66: }
67:
68: /**
69: * @see fr.aliacom.form.common.events.IRadioSelectionListener#selected(IRadioButton)
70: */
71: public void selected(IRadioButton source) {
72: for (int i = 0; i < buttons.size(); i++) {
73: IRadioButton button = (IRadioButton) buttons.get(i);
74: if (button != source) {
75: button.setSelected(false);
76: }
77: }
78: if (source.getAction() != null) {
79: fireGroupEvent(source.getAction());
80: }
81: }
82:
83: }
|