be97c52d79399da76372c9377cfc5bc5cbfcc19b
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / events / callback / JavaCallBack.java
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2008-2008 - INRIA - Bruno JOFRET
4  *
5  *  This file must be used under the terms of the CeCILL.
6  *  This source file is licensed as described in the file COPYING, which
7  *  you should have received as part of this distribution.  The terms
8  *  are also available at
9  *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12 package org.scilab.modules.gui.events.callback;
13
14 import java.awt.event.ActionEvent;
15 import java.lang.reflect.Method;
16
17 /**
18  * Generic class to hide callback management
19  * @author bruno
20  *
21  */
22 public abstract class JavaCallBack extends CallBack {
23
24         /**
25          * @param command : the command to execute.
26          */
27         private JavaCallBack(String command) {
28                 super(command);
29         }
30
31         /**
32          * Callback Factory to easily create a callback
33          * just like in scilab.
34          * @param command : the command to execute.
35          * @return a usable Java callback
36          */
37         public static JavaCallBack create(String command) {
38                 return (new JavaCallBack(command) {
39                         public void callBack() {
40                                 try {
41                                         int lastPoint = command.lastIndexOf(".");
42                                         Class invokedClass = Class.forName(command.substring(0, lastPoint));
43                                         Method runMe = invokedClass.getMethod(command.substring(lastPoint + 1));
44                                         // Only able to launch method Class.
45                                         runMe.invoke(invokedClass.getClass(), (Object[]) null);
46                                 } catch (Exception e) {
47                                         // TODO Auto-generated catch block
48                                         e.printStackTrace();
49                                 }
50                         }
51                 });
52         }
53         
54         /**
55          * Callback Factory to easily create a callback
56          * just like in scilab.
57          * @param command : the command to execute.
58          * @return a usable Java callback
59          */
60         public static CallBack createOutOfXclickAndXgetmouse(String command) {
61                 return (new JavaCallBack(command) {
62                         public void callBack() {
63                                 try {
64                                         int lastPoint = command.lastIndexOf(".");
65                                         Class invokedClass = Class.forName(command.substring(0, lastPoint));
66                                         Method runMe = invokedClass.getMethod(command.substring(lastPoint + 1));
67                                         // Only able to launch method Class.
68                                         runMe.invoke(invokedClass.getClass(), (Object[]) null);
69                                 } catch (Exception e) {
70                                         // TODO Auto-generated catch block
71                                         e.printStackTrace();
72                                 }
73                         }
74                         
75                         /**
76                          * To match the standard Java Action management.
77                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
78                          * @param e The event that launch the callback.
79                          */
80                         public void actionPerformed(ActionEvent e) {
81                             callBack();
82                         } 
83                 });        
84         }
85 }