Modify gcbo in callback
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / events / callback / CallBack.java
1 package org.scilab.modules.gui.events.callback;
2
3
4 import javax.swing.AbstractAction;
5
6 /**
7  * Abstract class to manage all callbacks.
8  * Those that need Java code, and those that need Scilab execution.
9  * 
10  * @author bruno
11  *
12  */
13 public abstract class CallBack extends AbstractAction {
14         
15         /**
16          * Unmanaged command type constant
17          */
18         public static final int UNTYPED = -1;
19         /**
20          * Scilab instruction command type constant
21          */
22         public static final int SCILAB_INSTRUCTION = 0;
23         /**
24          * C or Fortran function type constant
25          */
26         public static final int C_FORTRAN = 1;
27         /**
28          * Scilab function type constant
29          */
30         public static final int SCILAB_FUNCTION = 2;
31
32         /**
33          * Java function type constant
34          */
35         public static final int JAVA = 3;       
36         
37         /**
38          * Scilab instruction without GCBO setting (old addmenu compatibility)
39          */
40         public static final int SCILAB_INSTRUCTION_WITHOUT_GCBO = 4;    
41         
42         /**
43          * The Command to Store and remember.
44          */
45         protected String command;
46         
47         /**
48          * Constructor to be seen by specifics CallBack.
49          * @param command the command associated to the ccallback
50          */
51         public CallBack(String command) {
52                 this.command = command;
53         }
54         
55         /**
56          * @return the command if it's a Scilab instruction.
57          */
58         public String getCommand() {
59                 return command;
60         }
61         
62         /**
63          * Callback function that will be call on some event.
64          */
65         public abstract void callBack();
66                 
67         /**
68          * Create a Callback from Scilab data
69          * @param command the instruction
70          * @param callbackType the type of the instruction
71          * @return the Callback
72          */
73         public static CallBack createCallback(String command, int callbackType) {
74                 if (callbackType == CallBack.JAVA) {
75                         return JavaCallBack.create(command);
76                 } else {
77                         return ScilabCallBack.create(command);
78                 }
79         }
80
81         /**
82          * Create a Callback from Scilab data
83          * @param command the instruction
84          * @param callbackType the type of the instruction
85          * @param objectIndex the index of the object in the UIElementMapper
86          * @return the Callback
87          */
88         public static CallBack createCallback(String command, int callbackType, int objectIndex) {
89                 if (callbackType == CallBack.JAVA) {
90                         return JavaCallBack.create(command);
91                 } else if (callbackType == CallBack.SCILAB_INSTRUCTION_WITHOUT_GCBO) {
92                         return ScilabCallBack.create(command);
93                 } else {
94                         return ScilabCallBack.create("if exists(\"gcbo\") then %oldgcbo = gcbo; end;"
95                                         + "gcbo = getcallbackobject(" + objectIndex + ");"
96                                         + command 
97                                         + ";if exists(\"%oldgcbo\") then gcbo = %oldgcbo; else clear gcbo; end;");
98                 }
99         }
100
101 }