01: /*
02: * ReflectiveAction.java
03: *
04: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU General Public License
08: * as published by the Free Software Foundation; either version 2
09: * of the License, or any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: *
20: */
21:
22: package org.underworldlabs.swing.actions;
23:
24: import java.awt.event.ActionEvent;
25: import java.awt.event.ActionListener;
26: import java.lang.reflect.Method;
27:
28: /* ----------------------------------------------------------
29: * CVS NOTE: Changes to the CVS repository prior to the
30: * release of version 3.0.0beta1 has meant a
31: * resetting of CVS revision numbers.
32: * ----------------------------------------------------------
33: */
34:
35: /**
36: *
37: * @author Takis Diakoumis
38: * @version $Revision: 1.4 $
39: * @date $Date: 2006/05/14 06:56:07 $
40: */
41: public class ReflectiveAction implements ActionListener {
42:
43: private Object target;
44:
45: public ReflectiveAction() {
46: }
47:
48: public ReflectiveAction(Object target) {
49: this .target = target;
50: }
51:
52: public void actionPerformed(ActionEvent e) {
53: String command = e.getActionCommand();
54: try {
55:
56: Class[] argTypes = { e.getClass() };
57:
58: Method method = null;
59:
60: if (target == null) {
61: target = this ;
62: method = getClass().getMethod(command, argTypes);
63: } else {
64: method = target.getClass().getMethod(command, argTypes);
65: }
66:
67: if (method == null) {
68: return;
69: }
70:
71: Object[] args = { e };
72: method.invoke(target, args);
73:
74: } catch (Exception ex) {
75: ex.printStackTrace();
76: }
77: }
78:
79: public Object getTarget() {
80: return target;
81: }
82:
83: public void setTarget(Object target) {
84: this.target = target;
85: }
86:
87: }
|