KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > monitor > control > PropertyChangeHelper


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.monitor.control;
5
6 import java.beans.PropertyChangeListener JavaDoc;
7 import java.lang.reflect.InvocationTargetException JavaDoc;
8 import java.lang.reflect.Method JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import org.apache.log4j.Logger;
13
14 /**
15  *
16  */

17 public class PropertyChangeHelper {
18
19     private static final Logger logger = Logger
20             .getLogger(PropertyChangeHelper.class);
21
22     static final Map JavaDoc helpers = new HashMap JavaDoc();
23
24     /** Remeber the addListener method. */
25     private Method JavaDoc addPropListenerMethod;
26
27     /** Remeber the removeListener method. */
28     private Method JavaDoc removePropListenerMethod;
29
30     private PropertyChangeHelper(Class JavaDoc bean) {
31         Class JavaDoc beanClass = bean.getClass();
32         Class JavaDoc[] argClasses = { PropertyChangeListener JavaDoc.class };
33         try {
34             addPropListenerMethod = beanClass.getMethod(
35                     "addPropertyChangeListener", argClasses);
36
37             removePropListenerMethod = beanClass.getMethod(
38                     "removePropertyChangeListener", argClasses);
39         } catch (SecurityException JavaDoc e) {
40             logger.debug(e);
41         } catch (NoSuchMethodException JavaDoc e) {
42             // ignore
43
}
44
45     }
46
47     private static PropertyChangeHelper lookup(Class JavaDoc bean) {
48         synchronized (helpers) {
49             PropertyChangeHelper helper = (PropertyChangeHelper) helpers
50                     .get(bean);
51             if (helper == null) {
52                 helper = new PropertyChangeHelper(bean);
53                 helpers.put(bean, helper);
54             }
55             return helper;
56         }
57     }
58
59     public static void addPropertyChangeListener(Object JavaDoc obj,
60             PropertyChangeListener JavaDoc l) {
61         PropertyChangeHelper helper = lookup(obj.getClass());
62         if (helper.addPropListenerMethod == null) {
63             return;
64         }
65         Object JavaDoc[] args = { obj };
66         try {
67             helper.addPropListenerMethod.invoke(obj, args);
68         } catch (IllegalArgumentException JavaDoc e) {
69             logger.debug(e);
70         } catch (IllegalAccessException JavaDoc e) {
71             logger.debug(e);
72         } catch (InvocationTargetException JavaDoc e) {
73             logger.debug(e);
74         }
75     }
76
77     public static void removePropertyChangeListener(Object JavaDoc obj,
78             PropertyChangeListener JavaDoc l) {
79         PropertyChangeHelper helper = lookup(obj.getClass());
80         if (helper.removePropListenerMethod == null) {
81             return;
82         }
83         Object JavaDoc[] args = { obj };
84         try {
85             helper.removePropListenerMethod.invoke(obj, args);
86         } catch (IllegalArgumentException JavaDoc e) {
87             logger.debug(e);
88         } catch (IllegalAccessException JavaDoc e) {
89             logger.debug(e);
90         } catch (InvocationTargetException JavaDoc e) {
91             logger.debug(e);
92         }
93     }
94 }
95
Popular Tags