01: package net.sourceforge.squirrel_sql.fw.gui;
02:
03: import javax.swing.*;
04: import java.util.ArrayList;
05: import java.beans.PropertyChangeListener;
06: import java.awt.event.ActionEvent;
07:
08: public class ToggleComponentHolder {
09: private ArrayList<AbstractButton> _toggleables = new ArrayList<AbstractButton>();
10: private boolean _selected;
11:
12: public boolean isSelected() {
13: return _selected;
14: }
15:
16: public void setSelected(boolean b) {
17: _selected = b;
18:
19: for (int i = 0; i < _toggleables.size(); i++) {
20: final AbstractButton abstractButton = _toggleables.get(i);
21: GUIUtils.processOnSwingEventThread(new Runnable() {
22: public void run() {
23: abstractButton.setSelected(_selected);
24: }
25: });
26: }
27: }
28:
29: public void addToggleableComponent(AbstractButton toggleable) {
30: Action origAction = toggleable.getAction();
31:
32: if (null == origAction) {
33: throw new RuntimeException("No Action attached to button");
34: }
35:
36: toggleable.setAction(new ActionProxy(origAction));
37:
38: _toggleables.add(toggleable);
39: }
40:
41: private void selectionChanged() {
42: _selected = !_selected;
43:
44: for (int i = 0; i < _toggleables.size(); i++) {
45: AbstractButton abstractButton = _toggleables.get(i);
46: abstractButton.setSelected(_selected);
47: }
48: }
49:
50: private class ActionProxy implements Action {
51: private Action _delegate;
52:
53: ActionProxy(Action delegate) {
54: _delegate = delegate;
55: }
56:
57: public boolean isEnabled() {
58: return _delegate.isEnabled();
59: }
60:
61: public void setEnabled(boolean b) {
62: _delegate.setEnabled(b);
63: }
64:
65: public void addPropertyChangeListener(
66: PropertyChangeListener listener) {
67: _delegate.addPropertyChangeListener(listener);
68: }
69:
70: public void removePropertyChangeListener(
71: PropertyChangeListener listener) {
72: _delegate.removePropertyChangeListener(listener);
73: }
74:
75: public Object getValue(String key) {
76: return _delegate.getValue(key);
77: }
78:
79: public void putValue(String key, Object value) {
80: _delegate.putValue(key, value);
81: }
82:
83: public void actionPerformed(ActionEvent e) {
84: selectionChanged();
85: _delegate.actionPerformed(e);
86: }
87: }
88:
89: }
|