Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
157 kaklik 1
/*
2
    AVRcamVIEW: A PC application to test out the functionallity of the
3
     AVRcam real-time image processing engine.
4
    Copyright (C) 2004    Brent A. Taylor
5
 
6
    This program is free software; you can redistribute it and/or
7
    modify it under the terms of the GNU General Public
8
    License as published by the Free Software Foundation; either
9
    version 2 of the License, or (at your option) 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 GNU
14
    General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public
17
    License 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
   For more information on the AVRcamVIEW, please contact:
21
 
22
   taylorba@comcast.net
23
 
24
   or go to www.jrobot.net for more details regarding the system.
25
*/
26
 
27
package avr.swing;
28
 
29
import java.awt.event.*;
30
import java.lang.reflect.*;
31
import javax.swing.*;
32
 
33
/**
34
 * Utility class for defining Actions that can be attached as an ActionListener.
35
 * It also provides many convience methods for setting the Icon, Keystroke,
36
 * and Mnemonic.
37
 */
38
public class ProxyAction extends AbstractAction {
39
 
40
   /**
41
    * The object to which the action is to be invoked upon.
42
    */
43
   private Object target;
44
 
45
   /**
46
    * The method of the target to call when the action is invoked.
47
    */
48
   private Method method;
49
 
50
   /**
51
    * Create a Proxy Action.
52
    * @param target The object to which the action is to be invoked upon.
53
    * @param methodName The method to call.
54
    * @param icon The icon to display on the control this action is attached to.
55
    * in the object "target".
56
    */
57
   public ProxyAction(Object target,
58
                      String methodName,
59
                      Icon icon) {
60
      this(target, methodName, null, icon);
61
   }
62
 
63
   /**
64
    * Create a Proxy Action.
65
    * @param target The object to which the action is to be invoked upon.
66
    * @param methodName The method to call.
67
    * @param name The text string to display on the control this action is
68
    * attached to.
69
    * in the object "target".
70
    */
71
   public ProxyAction(Object target,
72
                      String methodName,
73
                      String name) {
74
      this(target, methodName, name, null);
75
   }
76
 
77
   /**
78
    * Create a Proxy Action.
79
    * @param target The object to which the action is to be invoked upon.
80
    * @param methodName The method to call.
81
    * @param name The text string to display on the control this action is
82
    * attached to.
83
    * @param key The mnemonic key used to invoke this action. (Alt + key)
84
    * in the object "target".
85
    */
86
   public ProxyAction(Object target,
87
                      String methodName,
88
                      String name,
89
                      int key) {
90
      this(target, methodName, name, key, null);
91
   }
92
 
93
   /**
94
    * Create a Proxy Action.
95
    * @param target The object to which the action is to be invoked upon.
96
    * @param methodName The method to call.
97
    * @param name The text string to display on the control this action is
98
    * attached to.
99
    * @param icon The icon to display on the control this action is attached to.
100
    * in the object "target".
101
    */
102
   public ProxyAction(Object target,
103
                      String methodName,
104
                      String name,
105
                      Icon icon) {
106
      this(target, methodName, false, name, -1, icon);
107
   }
108
 
109
   /**
110
    * Create a Proxy Action.
111
    * @param target The object to which the action is to be invoked upon.
112
    * @param methodName The method to call.
113
    * @param name The text string to display on the control this action is
114
    * attached to.
115
    * @param key The mnemonic key used to invoke this action. (Alt + key)
116
    * @param icon The icon to display on the control this action is attached to.
117
    * in the object "target".
118
    */
119
   public ProxyAction(Object target,
120
                      String methodName,
121
                      String name,
122
                      int key,
123
                      Icon icon) {
124
      this(target, methodName, false, name, key, icon);
125
   }
126
 
127
   /**
128
    * Create a Proxy Action.
129
    * @param target The object to which the action is to be invoked upon.
130
    * @param methodName The method to call.
131
    * @param passEvent True if the ActionEvent is to be passed to the method
132
    * defined by "methodName"
133
    * @param name The text string to display on the control this action is
134
    * attached to.
135
    */
136
   public ProxyAction(Object target,
137
                      String methodName,
138
                      boolean passEvent,
139
                      String name) {
140
      this(target, methodName, passEvent, name, -1, null);
141
   }
142
 
143
   /**
144
    * Create a Proxy Action.
145
    * @param target The object to which the action is to be invoked upon.
146
    * @param methodName The method to call.
147
    * @param passEvent True if the ActionEvent is to be passed to the method
148
    * defined by "methodName"
149
    * @param name The text string to display on the control this action is
150
    * attached to.
151
    * @param key The mnemonic key used to invoke this action. (Alt + key)
152
    * in the object "target".
153
    */
154
   public ProxyAction(Object target,
155
                      String methodName,
156
                      boolean passEvent,
157
                      String name,
158
                      int key) {
159
      this(target, methodName, passEvent, name, key, null);
160
   }
161
 
162
   /**
163
    * Create a Proxy Action.
164
    * @param target The object to which the action is to be invoked upon.
165
    * @param methodName The method to call.
166
    * @param passEvent True if the ActionEvent is to be passed to the method
167
    * defined by "methodName"
168
    * @param name The text string to display on the control this action is
169
    * attached to.
170
    * @param key The mnemonic key used to invoke this action. (Alt + key)
171
    * @param icon The icon to display on the control this action is attached to.
172
    * in the object "target".
173
    */
174
   public ProxyAction(Object target,
175
                      String methodName,
176
                      boolean passEvent,
177
                      String name,
178
                      int key,
179
                      Icon icon) {
180
      super(name);
181
//      this(target,
182
//           target.getClass().getMethod(methodName,
183
//                                       (passEvent) ? new Class[] { ActionEvent.class }
184
//                                                   : null),
185
//           name,
186
//           key,
187
//           icon);
188
 
189
      Method method = null;
190
 
191
      try {
192
         method = target.getClass().getMethod(methodName,
193
                                              (passEvent) ? new Class[] {ActionEvent.class}
194
                                              : null);
195
      } catch(NoSuchMethodException ex) {
196
         throw new NoSuchMethodError(ex.getMessage());
197
      }
198
 
199
      if(method.getParameterTypes().length > 1) {
200
         throw new IllegalArgumentException(
201
            "Method can have only one ActionEvent argument.");
202
 
203
      } else if(method.getParameterTypes().length == 1) {
204
         if(!method.getParameterTypes()[0].getName().equals(
205
               ActionEvent.class.getName())) {
206
 
207
            throw new IllegalArgumentException(
208
               method.getParameterTypes()[0].getName() + " != " +
209
               ActionEvent.class.getName() +
210
               " Parameter must be an ActionEvent.");
211
         }
212
      }
213
 
214
      this.target = target;
215
      this.method = method;
216
 
217
      if(key != -1) {
218
         setMnemonic(key);
219
      }
220
 
221
      if(icon != null) {
222
         setIcon(icon);
223
      }
224
 
225
   }
226
 
227
   /**
228
    * Set the mnemonic key to be the given key.
229
    * @param key The key identifier used to set the Mnemonic to
230
    * @see KeyEvent
231
    */
232
   public void setMnemonic(int key) {
233
      putValue(MNEMONIC_KEY, new Integer(key));
234
   }
235
 
236
   /**
237
    * Set the Shortcut KeyStroke to attach to this action.
238
    * @param keyCode The key identifier to use.
239
    * @param modifiers Any modifiers that need to be used along with the key.
240
    * @see KeyStroke
241
    * @see KeyEvent
242
    */
243
   public void setKeyStroke(int keyCode, int modifiers) {
244
      setKeyStroke(KeyStroke.getKeyStroke(keyCode, modifiers));
245
   }
246
 
247
   /**
248
    * Set the Shortcut KeyStroke to attach to this action.
249
    * @param keystroke The KeyStroke to use
250
    */
251
   public void setKeyStroke(KeyStroke keystroke) {
252
      putValue(ACCELERATOR_KEY, keystroke);
253
   }
254
 
255
   /**
256
    * Set the Icon of this Action
257
    * @param icon The icon to use.
258
    */
259
   public void setIcon(Icon icon) {
260
      putValue(SMALL_ICON, icon);
261
 
262
   }
263
 
264
   /**
265
    * Sets the Tool Tip Text for this Action
266
    *
267
    * @param text String
268
    */
269
   public void setToolTipText(String text) {
270
      putValue(SHORT_DESCRIPTION, text);
271
   }
272
 
273
   /**
274
    * This method is called when an Action Event occurs on the control this
275
    * action is attached to.
276
    * @param ae The ActionEvent created.
277
    */
278
   public void actionPerformed(ActionEvent ae) {
279
 
280
      try {
281
         if(method.getParameterTypes().length == 0) {
282
            // invoke the given method on the given target with no parameters
283
            method.invoke(target, null);
284
         } else {
285
            // invoke the given method on the given target with the ActionEvent
286
            // as a parameter
287
            method.invoke(target, new Object[] {ae});
288
         }
289
      } catch(Exception e) {
290
         // should never happen
291
         e.printStackTrace(System.err);
292
      }
293
 
294
   }
295
 
296
}