Hi Thomas

Thanks alot for your excellent response.  Once I got a better understanding
of how the setDocument(svgDoc) API works I was able to add a listener to
create the binding.  All works now.  I have included the source of my
example in case anyone else encounters the same problem.  Thanks again...


public class SVGScriptApplet extends Applet {
        private static class MyCanvas extends JSVGCanvas {              
                public BridgeContext getBridgeContext() {
            return bridgeContext;
        }
    }
        
    public void init() {
        String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
            try{                
                final MyCanvas canvas = new MyCanvas();
                URL url = new URL(getCodeBase(), "3D.svg");
                SVGOMDocument svgDoc =
(SVGOMDocument)f.createDocument(url.toString());                
                canvas.addSVGLoadEventDispatcherListener(new            
SVGLoadEventDispatcherAdapter() {
                        public void svgLoadEventDispatchStarted
                            (SVGLoadEventDispatcherEvent e) {
                                        
                                        Interpreter i =                         
                
canvas.getBridgeContext().getInterpreter("text/ecmascript");            
                                i.bindObject("appletHost", 
SVGScriptApplet.this);                                                          
     
                        }
                    });
                canvas.setDocument(svgDoc);
                this.add(canvas);
                    this.setVisible(true);              
            }catch (Exception e){
                System.out.println(e);
            }
    }        
    public void doSomething() {
        System.out.println("Do something!");
    }
}

Regards

Pat


thomas.deweese wrote:
> 
> Hi Pat,
> 
> patc <[EMAIL PROTECTED]> wrote on 09/04/2007 10:52:06 AM:
> 
>    The problem with the following code is that when you
> call 'setDocument' the canvas will create a whole new
> BridgeContext, and Ecmascript interpreter so your hard
> work is all forgotten.
> 
>    To do this right you should register an SVGLoad event
> listener with the canvas.  When that is called you should
> be able to bind your object.
> 
>>     public void init() {
>>        String parser = XMLResourceDescriptor.getXMLParserClassName();
>>        SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
>>        try{ 
>>            MyCanvas canvas = new MyCanvas();
>>            URL url = new URL(getCodeBase(), "3D.svg");
>>            SVGOMDocument svgDoc =
>> (SVGOMDocument)f.createDocument(url.toString());
>>            canvas.setBridgeContext(svgDoc); 
>>            // 'Unknown document' exception encountered if the following 
> 2
>> lines are not included
>>            GVTBuilder builder   = new GVTBuilder();
>>            builder.build(canvas.getBridgeContext(), svgDoc); 
>>            Interpreter i =
>> canvas.getBridgeContext().getInterpreter("text/ecmascript"); 
>>            i.bindObject("appletHost", this);
>>            canvas.setDocument(svgDoc);
>>        }catch (Exception e){
>>           System.out.println(e);
>>        }
>>     }
>>     public void doSomething() {
>>        System.out.println("Do something!");
>>     }
>> }
>> 
>> This applet loads the 3D.svg that comes in samples directory of Batik 
> 1.7
>> release (in order to keep things simple and easily reproducable).
>> 
>> I have updated the 3D.svg file to reference the applet and call
>> 'doSomething()' method:
>> 
>> <svg xmlns="http://www.w3.org/2000/svg"; 
>>      xmlns:xlink="http://www.w3.org/1999/xlink";
>>      id="body" width="640" height="480" viewBox="0 0 640 480">
>> 
>>     <script type="text/ecmascript"><![CDATA[
>> 
>>      .....
>>      // Global initialization
>>      function startAnimation(evt) {
>>           appletHost.doSomething();
>>           .....
>>      }
>>      ....
>> 
>> Unfortunately the interpreter does not seem to take note of the mapping 
> of
>> type of name 'appletHost' with the applet, and I get the following
>> exception;
>> 
>> org.mozilla.javascript.WrappedException: Wrapped
>> org.mozilla.javascript.EcmaError: ReferenceError: "appletHost" is not
>> defined. (Inline <script> 
> http://localhost:8080/FO5/applet/3D.svg:-1#107)
>> (Event attribute http://localhost:8080/FO5/applet/3D.svg:-1 onload#1)
>> at
>> 
> org.apache.batik.script.rhino.BatikSecurityController.callWithDomain(Unknown
>> Source)
>>    at org.mozilla.javascript.Interpreter.interpret(Unknown Source)
>>    at org.mozilla.javascript.InterpretedFunction.call(Unknown Source)
>>    at org.mozilla.javascript.Interpreter.interpretLoop(Unknown Source)
>>    at script(Event attribute http://localhost:8080/FO5/applet/3D.svg:-1
>> onload:1)
>>    at org.mozilla.javascript.Interpreter.interpret(Unknown Source)
>>    at org.mozilla.javascript.InterpretedFunction.call(Unknown Source)
>>    at 
> org.apache.batik.script.rhino.BatikSecurityController$1.run(Unknown
>> Source)
>>    at java.security.AccessController.doPrivileged(Native Method)
>>    at
>> 
> org.apache.batik.script.rhino.BatikSecurityController.callWithDomain(Unknown
>> Source)
>>    at org.mozilla.javascript.Interpreter.interpret(Unknown Source)
>>    at org.mozilla.javascript.InterpretedFunction.call(Unknown Source)
>>    at org.mozilla.javascript.ContextFactory.doTopCall(Unknown Source)
>>    at org.mozilla.javascript.ScriptRuntime.doTopCall(Unknown Source)
>>    at org.mozilla.javascript.InterpretedFunction.exec(Unknown Source)
>>    at org.mozilla.javascript.Context.evaluateReader(Unknown Source)
>>    at org.apache.batik.script.rhino.RhinoInterpreter$2.run(Unknown 
> Source)
>>    at org.mozilla.javascript.Context.call(Unknown Source)
>> 
>> Not really sure what I am doing wrong.  Is there something I am missing?
>> 
>> Any help greatly appreciated
>> 
>> Pat
>> -- 
>> View this message in context: http://www.nabble.com/Problem-calling-
>> applet-method-from-script-in-svg-file-tf4378175.html#a12479719
>> Sent from the Batik - Users mailing list archive at Nabble.com.
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Problem-calling-applet-method-from-script-in-svg-file-tf4378175.html#a12878410
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to