01: /*
02: * Copyright 2006 Ethan Nicholas. All rights reserved.
03: * Use is subject to license terms.
04: */
05: package jaxx.runtime.swing;
06:
07: import java.beans.*;
08: import java.util.*;
09: import javax.swing.*;
10: import javax.swing.event.*;
11:
12: public class JAXXButtonGroup extends ButtonGroup {
13: public static final String SELECTED_VALUE_PROPERTY = "selectedValue";
14:
15: private PropertyChangeSupport propertyChangeSupport;
16: private Object selectedValue;
17: private ChangeListener changeListener = new ChangeListener() {
18: public void stateChanged(ChangeEvent e) {
19: updateSelectedValue();
20: }
21: };
22:
23: public void add(AbstractButton button) {
24: super .add(button);
25: button.addChangeListener(changeListener);
26: updateSelectedValue();
27: }
28:
29: public void remove(AbstractButton button) {
30: super .remove(button);
31: button.removeChangeListener(changeListener);
32: updateSelectedValue();
33: }
34:
35: public void updateSelectedValue() {
36: Enumeration/*<AbstractButton>*/e = getElements();
37: while (e.hasMoreElements()) {
38: AbstractButton button = (AbstractButton) e.nextElement();
39: if (button.isSelected()) {
40: Object selectedValue = button
41: .getClientProperty("$value");
42: if (selectedValue != getSelectedValue())
43: setSelectedValue(selectedValue);
44: }
45: }
46: }
47:
48: public Object getSelectedValue() {
49: return selectedValue;
50: }
51:
52: public void setSelectedValue(Object value) {
53: Object oldValue = getSelectedValue();
54: this .selectedValue = value;
55: firePropertyChange(oldValue);
56: }
57:
58: protected PropertyChangeSupport getPropertyChangeSupport() {
59: if (propertyChangeSupport == null)
60: propertyChangeSupport = new PropertyChangeSupport(this );
61: return propertyChangeSupport;
62: }
63:
64: public void addPropertyChangeListener(
65: PropertyChangeListener listener) {
66: getPropertyChangeSupport().addPropertyChangeListener(listener);
67: }
68:
69: public void addPropertyChangeListener(String property,
70: PropertyChangeListener listener) {
71: getPropertyChangeSupport().addPropertyChangeListener(property,
72: listener);
73: }
74:
75: public void removePropertyChangeListener(
76: PropertyChangeListener listener) {
77: getPropertyChangeSupport().removePropertyChangeListener(
78: listener);
79: }
80:
81: public void removePropertyChangeListener(String property,
82: PropertyChangeListener listener) {
83: getPropertyChangeSupport().removePropertyChangeListener(
84: property, listener);
85: }
86:
87: private void firePropertyChange(Object oldValue) {
88: if (propertyChangeSupport != null)
89: getPropertyChangeSupport().firePropertyChange(
90: SELECTED_VALUE_PROPERTY, oldValue,
91: getSelectedValue());
92: }
93: }
|