You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Emmy Alex <ea...@hermes.tietronix.com> on 2007/03/09 14:13:30 UTC

Getting user data in the handleEvent function in java application

Hi All,
I have an SVG that passes mouse click events to a function in a java application (SVGApllication.jar).

The code for the SVG is as given below
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" 
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink">

   <script type="application/java-archive"  xlink:href="SVGApplication.jar" />
   <circle id="myclic-circle" cx="25" cy="25" r="25" fill="blue" />

</svg>

The SVG Application.jar has a SVGApplication class. This class has a run method that registers the mouse click event. It also implements the handleEvent method of the org.w3c.dom.events.EventListener interface. When the user click on the SVG control goes to the handleEvent function in my SVGApplication class. On click in the circle we change the color of the circle in the SVG.

The code for SVGApplication.java is given below:
import org.w3c.dom.*;
import org.w3c.dom.events.*;
import org.apache.batik.script.ScriptHandler;
import org.apache.batik.script.Window;

public class SVGApplication implements ScriptHandler, EventListener {
    private Document document;
   
    public SVGApplication()
	{
		
		System.out.println("In constructor!!!");
	}
    public void run (final Document document, final Window win) {
        System.out.println("In run of the function!!!");
    	this.document = document;
        EventTarget changecolor = 
(EventTarget)document.getElementById("myclic-circle");
        changecolor.addEventListener("click", this, false);
    }
   
    public void handleEvent(Event evt) {
    	System.out.println("In handleEvent of the function!!!");
        Element changecolor = document.getElementById("myclic-circle");
        System.out.println("changecolor:" + changecolor);
        changecolor.setAttributeNS(null, "fill", "#0F0000");
    }

My question is how can I pass the color I intend to change from the SVG. I mean can handleEvent in the java file take in any UserData.I know in javascript we can do a onclick and send in parameters how do we do this for a Dom EventListener interface.

Thank you,
Emmy 
             

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: Getting user data in the handleEvent function in java application

Posted by th...@kodak.com.
Hi Emmy,

"Emmy Alex" <ea...@hermes.tietronix.com> wrote on 03/09/2007 08:13:30 AM:

> The SVG Application.jar has a SVGApplication class. This class has a
> run method that registers the mouse click event. It also implements 
> the handleEvent method of the org.w3c.dom.events.EventListener 
> interface. When the user click on the SVG control goes to the 
> handleEvent function in my SVGApplication class. On click in the 
> circle we change the color of the circle in the SVG.

   [...] 

> My question is how can I pass the color I intend to change from the 
> SVG. I mean can handleEvent in the java file take in any UserData.I 
> know in javascript we can do a onclick and send in parameters how do
> we do this for a Dom EventListener interface.

   In java (most object orient languages actually) you don't use
userData, instead you use the callback object to store your user data.
So in your case you will need to create a separate listener object
that you pass the color you want to when you create it:

    public class MyListener implements EventListener {
        String myColor;
        public MyListener(String myColor) {
                this.myColor = mycolor;
        }
        public void handleEvent(Event evt) {
                 System.out.println("In handleEvent of the function!!!");
        Element changecolor = document.getElementById("myclic-circle");
        System.out.println("changecolor:" + changecolor);
        changecolor.setAttributeNS(null, "fill", myColor);
      }
    }



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org