Create new callback types for uninterruptible callbacks.
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / events / callback / CommonCallBack.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.1-en.txt
10  *
11  */
12 package org.scilab.modules.gui.events.callback;
13
14 import java.awt.event.ActionEvent;
15
16 import javax.swing.AbstractAction;
17
18 import org.scilab.modules.graphic_objects.graphicObject.CallBack;
19 import org.scilab.modules.gui.events.GlobalEventFilter;
20 import org.scilab.modules.gui.events.GlobalEventWatcher;
21
22 /**
23  * Abstract class to manage all callbacks.
24  * Those that need Java code, and those that need Scilab execution.
25  *
26  * @author bruno
27  *
28  */
29 public abstract class CommonCallBack extends AbstractAction {
30
31     private static final long serialVersionUID = -919820581757579168L;
32
33     private CallBack callback;
34
35     /**
36      * @param command the callback command
37      * @param callbackType the callback command type
38      */
39     public CommonCallBack(String command) {
40         callback = new CallBack(command, CallBack.UNTYPED);
41     }
42     /**
43      * @param command the callback command
44      * @param callbackType the callback command type
45      */
46     public CommonCallBack(String command, int callbackType) {
47         callback = new CallBack(command, callbackType);
48     }
49
50     /**
51      * Callback function that will be call on some event.
52      */
53     public abstract void callBack();
54
55     /**
56      * Create a Callback from Scilab data
57      * @param command the instruction
58      * @param callbackType the type of the instruction
59      * @return the Callback
60      */
61     public static CommonCallBack createCallback(String command, int callbackType) {
62         CommonCallBack callback = null;
63         switch (callbackType) {
64             case CallBack.JAVA :
65                 callback = JavaCallBack.create(command);
66                 break;
67             case CallBack.JAVA_OUT_OF_XCLICK_AND_XGETMOUSE :
68                 callback = JavaCallBack.createOutOfXclickAndXgetmouse(command);
69                 break;
70             case CallBack.SCILAB_INSTRUCTION_WITHOUT_GCBO :
71                 callback = ScilabCallBack.create(command);
72                 break;
73             case CallBack.SCILAB_OUT_OF_XCLICK_AND_XGETMOUSE :
74                 callback = ScilabCallBack.createOutOfXclickAndXgetmouse(command);
75                 break;
76             case CallBack.SCILAB_NOT_INTERRUPTIBLE_FUNCTION:
77             case CallBack.SCILAB_NOT_INTERRUPTIBLE_INSTRUCTION:
78                 callback = ScilabCallBack.create(command, false);
79                 break;
80             default:
81                 callback = ScilabCallBack.create(command);
82                 break;
83         }
84         return callback;
85     }
86
87     /**
88      * Create a Callback from Scilab data
89      * @param command the instruction
90      * @param callbackType the type of the instruction
91      * @param objectUID the object UID
92      * @return the Callback
93      */
94     public static CommonCallBack createCallback(String command, int callbackType, int objectUID) {
95         CommonCallBack callback = null;
96         switch (callbackType) {
97             case CallBack.JAVA :
98                 callback = JavaCallBack.create(command);
99                 break;
100             case CallBack.JAVA_OUT_OF_XCLICK_AND_XGETMOUSE :
101                 callback = JavaCallBack.createOutOfXclickAndXgetmouse(command);
102                 break;
103             case CallBack.SCILAB_INSTRUCTION_WITHOUT_GCBO :
104                 callback = ScilabCallBack.create(command);
105                 break;
106             case CallBack.SCILAB_OUT_OF_XCLICK_AND_XGETMOUSE :
107                 callback = ScilabCallBack.createOutOfXclickAndXgetmouse(command);
108                 break;
109             default:
110                 callback = ScilabCallBack.create("if exists(\"gcbo\") then %oldgcbo = gcbo; end;"
111                                                  + "gcbo = getcallbackobject(" + objectUID + ");"
112                                                  + command
113                                                  + ";if exists(\"%oldgcbo\") then gcbo = %oldgcbo; else clear gcbo; end;");
114                 break;
115         }
116         return callback;
117     }
118
119     /**
120      * To match the standard Java Action management.
121      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
122      * @param e The event that launch the callback.
123      */
124     public void actionPerformed(ActionEvent e) {
125         if (!GlobalEventWatcher.isCatchingCallback()) {
126             callBack();
127         } else {
128             if (this.callback.getCommand() != null) {
129                 GlobalEventFilter.filterCallback(this.callback.getCommand());
130             }
131         }
132     }
133
134     /**
135      * @return the command of the callback
136      */
137     public String getCommand() {
138         return callback.getCommand();
139     }
140 }