Hi,
Attached you find 3 tiny files, showing howto invoking Java from TCL,
and
howto invoke a callback TCL procedure.
I still dont understand everyting, but if you feel the examples are
correct, it would be
a good idea to add a link to the
tclBlend1.2.2/docs/TclJava/contents.html
docu, and adding the three files to the docu.
TclJavaGlue.java is a base class providing the basic TclBlend glue
stuff.
Just inherit your application from TclJavaGlue.java and you are ready to
go!
As an example how to use it, i added MyApp.java and MyApp.tcl.
Compile:
javac *.java
Invoke:
jtclsh MyApp.tcl
thanks for your help,
Marcel
MyApp.tcl
/*------------------------------------------------------------------------------
Name: TclJavaGlue.java
Comment: Helper to connect TCL to Java using TclBlend, allowing callbacks
from Java to TCL
------------------------------------------------------------------------------*/
import tcl.lang.*;
/**
This is a little wrapper for the TCLBlend interface <br>
Thru inheritance from Extension you get the interpreter of the calling tcl script
This can be used for callbacks back into the TCL script
*/
public class TclJavaGlue extends tcl.lang.Extension
{
private String myName = "TclJavaGlue: ";
protected tcl.lang.Interp tclInterpreter = null;
/**
Example how to call from a tcl script:<br>
<code>java::load TclJavaGlue</code>
@param the TCL interpreter from the calling TCL script (tcl.lang.Extension)
*/
public void init(tcl.lang.Interp interp)
{
tclInterpreter = interp;
System.out.println(myName + "init(): Successfully initialized TclJavaGlue ...");
}
/**
executing a TCL script <br>
Callback to TCL, this uses the identical TCL interpreter which startet Java <br>
Example: eval("puts $env(CLASSPATH)");
@param tcl command
@return true: no error
*/
public boolean eval(String tclCmd)
{
try {
EventProcessor ep = new EventProcessor(tclInterpreter, tclCmd);
tclInterpreter.getNotifier().queueEvent(ep, TCL.QUEUE_TAIL);
ep = null;
return true;
}
catch (Exception e) {
System.out.println(myName + "TCL interpreter internal error when evaluating " +
tclCmd + ", EXCEPTION=" + e.toString());
return false;
}
}
}
/**
Threadsave EventProcessor for TCL scripts called from Java <br>
@see http://www.cs.umn.edu/~dejong/tcl/paper.html
*/
class EventProcessor extends TclEvent
{
private final String tclCmd;
private final Interp tclInterpreter;
EventProcessor(Interp interp, String cmd)
{
tclInterpreter = interp;
tclCmd = cmd;
}
public int processEvent(int flags)
{
try {
tclInterpreter.eval(tclCmd, flags);
}
catch (TclException e) {
System.out.println(e.toString());
}
return 1;
}
}
/*------------------------------------------------------------------------------
Name: MyApp.java
Comment: This may be a nice Swing application, started from a TCL script
Invoke: jtclsh MyApp.tcl
Compile: javac *.java
------------------------------------------------------------------------------*/
import tcl.lang.*;
public class MyApp extends TclJavaGlue
{
public void init(tcl.lang.Interp interp)
{
super.init(interp);
// initialize your swing application (or whatever) here...
System.out.println("init(): Successfully initialized MyApp");
System.out.println("Testing callbacks ...\n");
eval("puts $env(CLASSPATH)");
eval("tclCallback \"Hello\" \"world\"");
}
}