Praveen Nayak:
> In addition to the HTML and Applet interaction, I was looking at
> having the ECMAScript within the SVG interact with the applet.
>
> For example,
> 
> <svg>
>       <script>
>               function Test() 
>               {
>                       //Call the applet's appletTestMethod() here
>               }
>       </script>
> ...
> </svg>

Ah I see.  What you can do is bind a variable in the ECMAScript
environment to the applet object.  You need to get a reference to the
BridgeContext object, but I think it’s currently not exposed.  You can
extend JSVGCanvas to get it, though:

  public class Something extends Applet {

      private static class MyCanvas extends JSVGCanvas {
          public void getBridgeContext() {
              return bridgeContext;
          }
      }

      MyCanvas canvas = new MyCanvas();

      public void f() {
          BridgeContext ctx = canvas.getBridgeContext();
          Interpreter i = ctx.getInterpreter("application/ecmascript");
          i.bindObject("applet", this);
      }

      public void doSomething() {
      }
  }

then in your document you can call applet.doSomething();.

To call a script function from Java code in your applet, you again get a
reference to the Interpreter object, and use the evaluate method:

  BridgeContext ctx = canvas.getBridgeContext();
  Interpreter i = ctx.getInterpreter("application/ecmascript");
  try {
      i.evaluate("myScriptFunction()");
  } catch (InterpreterException e) {
      ...
  }

If you want to pass an object into the script function, you’ll need to
do something more elaborate:

  private static class MyRhinoIntepreterFactory
          extends RhinoInterpreterFactory {
      public Interpreter createInterpreter(URL documentURL,
                                           boolean svg12) {
          if (svg12) {
              return new MySVG12RhinoInterpreter(documentURL);
          }
          return new MyRhinoInterpreter(documentURL);
      }
  }
  private static Object call(final String fn, final Object[] args) {
      contextFactory.call(new ContextAction() {
          public Object run(Context cx) {
              ScriptableObject.callMethod
                  (globalObject, fn, args);
          }
      });
  }
  private static class MyRhinoInterpreter extends RhinoInterpreter {
      public Object callGlobalFunction(String fn, Object[] args) {
          call(fn, args);
      }
  }
  private static class MySVG12RhinoInterpreter extends RhinoInterpreter {
      public Object callGlobalFunction(String fn, Object[] args) {
          call(fn, args);
      }
  }
  private static class MyCanvas extends JSVGCanvas {
      public void getBridgeContext() {
          return bridgeContext;
      }
      protected BridgeContext createBridgeContext(SVGOMDocument doc) {
          BridgeContext ctx = super.createBridgeContext(doc);
          InterpreterPool ip =
              canvas.getBridgeContext().getInterpreterPool();
          MyRhinoIntepreterFactory f = new MyRhinoIntepreterFactory();
          ip.putInterpreterFactory("application/ecmascript", f);
          ip.putInterpreterFactory("application/javascript", f);
          ip.putInterpreterFactory("text/ecmascript", f);
          ip.putInterpreterFactory("text/javascript", f);
      }
  }

This extends the RhinoInterpreter (the SVG 1.1 and the SVG 1.2 version)
to add the callGlobalFunction method.  If you then use
getInterpreter("application/ecmascript") and cast it to
MyRhinoInterpreter (or MySVG12RhinoInterpreter if you’re using a SVG 1.2
document), you can call callGlobalFunction:

  HashMap map = ...; // object we want to pass to the script function

  BridgeContext ctx = canvas.getBridgeContext();
  MyRhinoInterpreter i =
      (MyRhinoInterpreter) ctx.getInterpreter("application/ecmascript");
  i.callGlobalFunction
      ("myScriptFunction",
       new Object[] { Boolean.TRUE, map, new Integer(123) });
  
That’s equivalent to calling myScriptFunction(true, map, 123);.

The above code’s completely untested, but the general principle should
be right.

Cameron

-- 
Cameron McCormack, http://mcc.id.au/
        xmpp:[EMAIL PROTECTED]  ▪  ICQ 26955922  ▪  MSN [EMAIL PROTECTED]

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

Reply via email to