01: //The contents of this file are subject to the Mozilla Public License Version 1.1
02: //(the "License"); you may not use this file except in compliance with the
03: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
04: //
05: //Software distributed under the License is distributed on an "AS IS" basis,
06: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
07: //for the specific language governing rights and
08: //limitations under the License.
09: //
10: //The Original Code is "The Columba Project"
11: //
12: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14: //
15: //All Rights Reserved.
16: package org.columba.core.gui.base;
17:
18: import java.beans.PropertyChangeEvent;
19: import java.beans.PropertyChangeListener;
20: import java.lang.reflect.InvocationHandler;
21: import java.lang.reflect.Method;
22:
23: import javax.swing.AbstractButton;
24:
25: /**
26: * This class implements a proxy behavior for a PropertyChangeListener registered
27: * on an action by an AbstractButton instance. Every selectable action peer should
28: * put this proxy in between its underlying action and the PropertyChangeListener
29: * it registeres on the action. This guarantees that action state changes will be
30: * propagated to the peer which will then be updated accordingly.
31: */
32: public class ButtonStateAdapter implements InvocationHandler {
33: protected AbstractButton button;
34: protected PropertyChangeListener listener;
35:
36: public ButtonStateAdapter(AbstractButton button,
37: PropertyChangeListener listener) {
38: this .button = button;
39: this .listener = listener;
40: }
41:
42: public Object invoke(Object proxy, Method method, Object[] args)
43: throws Throwable {
44: if (method.getName().equals("propertyChange")
45: && (args.length > 0)) {
46: PropertyChangeEvent e = (PropertyChangeEvent) args[0];
47:
48: if ("selected".equals(e.getPropertyName())) {
49: button.setSelected(((Boolean) e.getNewValue())
50: .booleanValue());
51: }
52: }
53:
54: return method.invoke(listener, args);
55: }
56: }
|