I've been having a little trouble with implementing traces for the widget
variables used in my Swank package (a Tk-like interface to Swing).  Below is
some example Java code that implements a trace on a variable (in this case
for a textual widget, like an entry).  The code works fine until you try to
use it from a Tcl proc where you use an "upvar" command..

For example, if you test with the following procedure and commands:

proc testtrace {varnm value} {
    upvar #0 $varnm panvar
    set panvar(name) $value
 }


entry .e -textvariable tvar(name)

testtrace tvar  value

you find that the arguments string1 and string2 of traceProc are "panvar"
and "name"

not "tvar" and "name" and an exception is thrown:
can't set panvar(name): can't read "panvar(name)" no such variable
Any thoughts?

Bruce

package swank;
import java.util.*;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import tcl.lang.*;

public class myDocumentListener implements DocumentListener,VarTrace
{
Interp interp;
String varName = null;
JTextComponent jtext;
boolean traceLock = false;

myDocumentListener(Interp interp,JTextComponent jtext) {
        this.interp = interp;
        this.jtext = jtext;
}
public void traceProc(Interp interp, String string1, String string2, int
flags) throws TclException
{
if (!traceLock) {
TclObject tobj = interp.getVar(string1,string2,TCL.GLOBAL_ONLY);
jtext.setText(tobj.toString());
}
traceLock = false;
}

public void setVarName(String name)  throws TclException {
        if ((varName != null) && (!varName.equals(""))) {
                interp.untraceVar(varName,this,TCL.TRACE_WRITES|
TCL.GLOBAL_ONLY);
        }
        try {
                TclObject tobj = interp.getVar(name,TCL.GLOBAL_ONLY);
        }
        catch (TclException tclException) 
        {
                TclObject tobj = TclString.newInstance("");
                interp.setVar(name,tobj,TCL.GLOBAL_ONLY);
        }       
        interp.traceVar(name,this,TCL.TRACE_WRITES| TCL.GLOBAL_ONLY);
        varName = new String(name);
}

public String getVarName() {
        return(varName);
}

public void updateVar(DocumentEvent docEvent)
{
        if (varName==null) {
                return;
        }
        Document doc = docEvent.getDocument();
        String string=null;
        try {
                string = doc.getText(0,doc.getLength());
        } 
        catch (BadLocationException e) {
                return;
        }

        TclObject tobj = TclString.newInstance(string);
        try {
                traceLock = true;
                interp.setVar(varName,tobj,TCL.GLOBAL_ONLY);
        } catch (TclException tclException) {
                return;
        }
}

public void     insertUpdate(DocumentEvent docEvent) {
        updateVar(docEvent);
}

public void     removeUpdate(DocumentEvent docEvent) {
        updateVar(docEvent);
}
public void     changedUpdate(DocumentEvent docEvent) {
        updateVar(docEvent);
}
}

----------------------------------------------------------------
The TclJava mailing list is sponsored by WebNet Technologies.
To subscribe:    send mail to [EMAIL PROTECTED]  
                 with the word SUBSCRIBE as the subject.
To unsubscribe:  send mail to [EMAIL PROTECTED] 
                 with the word UNSUBSCRIBE as the subject.
To send to the list, send email to '[EMAIL PROTECTED]'. 
A list archive is at: http://www.findmail.com/listsaver/tcldallas/

Reply via email to