1 package net.sf.logdistiller.gui;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.awt.event.ActionEvent;
18 import java.awt.event.ActionListener;
19 import java.lang.reflect.*;
20 import java.awt.event.ItemListener;
21 import java.awt.event.ItemEvent;
22
23 public class SwingAdapter
24 {
25 private SwingAdapter()
26 {
27 }
28
29 public static ActionListener getActionListener( Object obj, String methodName )
30 {
31 try
32 {
33 Class[] paramTypes = { ActionEvent.class };
34 Method method = obj.getClass().getMethod( methodName, paramTypes );
35 return new ActionAdapter( obj, method );
36 }
37 catch ( NoSuchMethodException nsme )
38 {
39 throw new RuntimeException( "method 'public void " + methodName + "(ActionEvent ae)' not found in "
40 + obj.getClass().getName(), nsme );
41 }
42 }
43
44 public static Runnable getRunnable( Object obj, String methodName )
45 {
46 try
47 {
48 Class[] paramTypes = {};
49 Method method = obj.getClass().getMethod( methodName, paramTypes );
50 return new RunnableAdapter( obj, method );
51 }
52 catch ( NoSuchMethodException nsme )
53 {
54 throw new RuntimeException( "method 'public void " + methodName + "()' not found in "
55 + obj.getClass().getName(), nsme );
56 }
57 }
58
59 public static ItemListener getItemListener( Object obj, String methodName )
60 {
61 try
62 {
63 Class[] paramTypes = { ItemEvent.class };
64 Method method = obj.getClass().getMethod( methodName, paramTypes );
65 return new ItemAdapter( obj, method );
66 }
67 catch ( NoSuchMethodException nsme )
68 {
69 throw new RuntimeException( "method 'public void " + methodName + "(ItemEvent ie)' not found in "
70 + obj.getClass().getName(), nsme );
71 }
72 }
73
74 private static class ActionAdapter
75 implements ActionListener
76 {
77 private final Object target;
78
79 private final Method method;
80
81 public ActionAdapter( Object target, Method method )
82 {
83 this.target = target;
84 this.method = method;
85 }
86
87 public void actionPerformed( ActionEvent ae )
88 {
89 try
90 {
91 method.invoke( target, new Object[] { ae } );
92 }
93 catch ( InvocationTargetException ite )
94 {
95 throw new RunException( ite );
96 }
97 catch ( IllegalArgumentException iae )
98 {
99 throw new RuntimeException( "IllegalArgumentException invoking method '" + method.getName() + "'", iae );
100 }
101 catch ( IllegalAccessException iae )
102 {
103 throw new RuntimeException( "IllegalAccessException invoking method '" + method.getName() + "'", iae );
104 }
105 }
106 }
107
108 private static class RunnableAdapter
109 implements Runnable
110 {
111 private final Object target;
112
113 private final Method method;
114
115 public RunnableAdapter( Object target, Method method )
116 {
117 this.target = target;
118 this.method = method;
119 }
120
121 public void run()
122 {
123 try
124 {
125 method.invoke( target, new Object[] {} );
126 }
127 catch ( InvocationTargetException ite )
128 {
129 throw new RunException( ite );
130 }
131 catch ( IllegalArgumentException iae )
132 {
133 throw new RuntimeException( "IllegalArgumentException invoking method '" + method.getName() + "'", iae );
134 }
135 catch ( IllegalAccessException iae )
136 {
137 throw new RuntimeException( "IllegalAccessException invoking method '" + method.getName() + "'", iae );
138 }
139 }
140 }
141
142 private static class ItemAdapter
143 implements ItemListener
144 {
145 private final Object target;
146
147 private final Method method;
148
149 public ItemAdapter( Object target, Method method )
150 {
151 this.target = target;
152 this.method = method;
153 }
154
155 public void itemStateChanged( ItemEvent ie )
156 {
157 try
158 {
159 method.invoke( target, new Object[] { ie } );
160 }
161 catch ( InvocationTargetException ite )
162 {
163 throw new RunException( ite );
164 }
165 catch ( IllegalArgumentException iae )
166 {
167 throw new RuntimeException( "IllegalArgumentException invoking method '" + method.getName() + "'", iae );
168 }
169 catch ( IllegalAccessException iae )
170 {
171 throw new RuntimeException( "IllegalAccessException invoking method '" + method.getName() + "'", iae );
172 }
173 }
174 }
175
176 public static class RunException
177 extends RuntimeException
178 {
179 private static final long serialVersionUID = 4688792600015649872L;
180
181 private RunException( Exception nestedException )
182 {
183 super( nestedException );
184 }
185 }
186 }