KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > base > ButtonStateAdapter


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
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 JavaDoc;
19 import java.beans.PropertyChangeListener JavaDoc;
20 import java.lang.reflect.InvocationHandler JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22
23 import javax.swing.AbstractButton JavaDoc;
24
25
26 /**
27  * This class implements a proxy behavior for a PropertyChangeListener registered
28  * on an action by an AbstractButton instance. Every selectable action peer should
29  * put this proxy in between its underlying action and the PropertyChangeListener
30  * it registeres on the action. This guarantees that action state changes will be
31  * propagated to the peer which will then be updated accordingly.
32  */

33 public class ButtonStateAdapter implements InvocationHandler JavaDoc {
34     protected AbstractButton JavaDoc button;
35     protected PropertyChangeListener JavaDoc listener;
36
37     public ButtonStateAdapter(AbstractButton JavaDoc button,
38         PropertyChangeListener JavaDoc listener) {
39         this.button = button;
40         this.listener = listener;
41     }
42
43     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
44         throws Throwable JavaDoc {
45         if (method.getName().equals("propertyChange") && (args.length > 0)) {
46             PropertyChangeEvent JavaDoc e = (PropertyChangeEvent JavaDoc) args[0];
47
48             if ("selected".equals(e.getPropertyName())) {
49                 button.setSelected(((Boolean JavaDoc) e.getNewValue()).booleanValue());
50             }
51         }
52
53         return method.invoke(listener, args);
54     }
55 }
56
Popular Tags