Avoid the callbacks to be interrupted by an other callback on the same object for...
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / events / callback / ScilabCallBack.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
16 import org.scilab.modules.action_binding.InterpreterManagement;
17 import org.scilab.modules.graphic_objects.graphicObject.CallBack;
18
19 /**
20  * ScilabCallback abstract class to easily manage callbacks
21  * that throws commands to Scilab.
22  *
23  * @author Bruno JOFRET
24  *
25  */
26 public abstract class ScilabCallBack extends CommonCallBack {
27
28     private static final long serialVersionUID = -4923246233703990342L;
29
30     /**
31      * Constructor
32      * @param command : the command to execute.
33      */
34     private ScilabCallBack(String command) {
35         super(command, CallBack.UNTYPED);
36     }
37
38     /**
39      * Callback Factory to easily create a callback
40      * just like in scilab.
41      * @param command : the command to execute.
42      * @return a usable Scilab callback
43      */
44     public static ScilabCallBack create(String command) {
45         return (new ScilabCallBack(command) {
46
47             private static final long serialVersionUID = -7286803341046313407L;
48
49             public void callBack() {
50                 Thread launchMe = new Thread() {
51                     public void run() {
52                         InterpreterManagement.requestScilabExec(getCommand());
53                     }
54                 };
55                 launchMe.start();
56             }
57         });
58     }
59
60     /**
61      * Callback Factory to easily create a callback
62      * just like in scilab.
63      * WARNING : this callback will be ignored by xclick & xgetmouse
64      * @param command : the command to execute.
65      * @return a usable Scilab callback
66      */
67     public static ScilabCallBack createOutOfXclickAndXgetmouse(String command) {
68         return (new ScilabCallBack(command) {
69
70             private static final long serialVersionUID = -7286803341046313407L;
71
72             public void callBack() {
73                 Thread launchMe = new Thread() {
74                     public void run() {
75                         InterpreterManagement.putCommandInScilabQueue(getCommand());
76                     }
77                 };
78                 launchMe.start();
79             }
80
81             /**
82              * To match the standard Java Action management.
83              * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
84              * @param e The event that launch the callback.
85              */
86             public void actionPerformed(ActionEvent e) {
87                 callBack();
88             }
89         });
90     }
91
92 }