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 nm...@messel.emse.fr on 2006/04/17 02:41:03 UTC

catch svg events

Hi,
I'm a beginner in svg and batik. I would like to use an svg event (for  
example an onclick on a rectangle in a svg file) in my java code to  
call a method. I thought that the example given the page Java  
Scripting of the Batik website would do that, but apparently it's not  
the case.
Is there any simple solution ?

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.


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


Re: catch svg events

Posted by Nigel MOTTE <nm...@messel.emse.fr>.
Thanks, it works great.

>>          Element elt = document.getElementById("elt-id");
This line didn't kept my attention, I was focused on the idea to catch  
the event linked with ECMAScript.


It's pretty sure I will need help on the next days but I hope that my  
questions will be a little more interesting ;-)

Nigel

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.


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


Re: catch svg events

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

Nigel MOTTE <nm...@messel.emse.fr> wrote on 04/17/2006 08:24:31 AM:

> in fact I use the example given on the page "Java Scripting" and I've 
> just modified it to support a file chooser and my event :
> 
> public void registerListeners() {
>          // Gets an element from the loaded document.
>          Element elt = document.getElementById("elt-id");
                                                 ^^^^^^^^
            For your sample SVG below this should be 'rect1308' so
it get's the rect you want your click event handler on.

>          EventTarget t = (EventTarget)elt;
> 
> 
>          // Adds a 'onclick' listener
>          t.addEventListener("testEvent", new OnClickAction(), false);
                               ^^^^^^
        For your example this should be just 'click'. 

> I've added  an onclick event (onclick="testEvent") on a rectangle :

    This will tell it to try and run the ECMAScript function 'testEvent'.
Simply remove this line from your SVG.


>      <rect
> /*Here is my event */
>         onclick="testEvent" <!--- delete the 'onclick' attribute -->
>         width="80"
>         height="150"
>         x="20"
>         y="30"
>         fill="red"
>         id="rect1308" />


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


Re: catch svg events

Posted by Nigel MOTTE <nm...@messel.emse.fr>.
Thanks for the answer,

in fact I use the example given on the page "Java Scripting" and I've  
just modified it to support a file chooser and my event :

public void registerListeners() {
         // Gets an element from the loaded document.
         Element elt = document.getElementById("elt-id");
         EventTarget t = (EventTarget)elt;


         // Adds a 'onclick' listener
         t.addEventListener("testEvent", new OnClickAction(), false);
     }

     public class OnClickAction implements EventListener {
         public void handleEvent(Event evt) {
             // Make some actions here...
             label.setText("Event captured");
             }

     }

I've taken the svg example of the french svg article of wikipedia and  
I've added  an onclick event (onclick="testEvent") on a rectangle :

<?xml version="1.0" encoding="utf-8"?>
<svg
    xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:ev="http://www.w3.org/2001/xml-events"
    version="1.1"
    baseProfile="full"
    x="0"
    y="0"
    width="300"
    height="200"
    id="svg2">
   <title>Rectangles</title>
   <defs
      id="defs4" />
   <g
      id="layer1">
     <rect
        width="300"
        height="120"
        x="0"
        y="20"
        fill="green"
        id="rect1306" />
     <rect
/*Here is my event */
        onclick="testEvent"
        width="80"
        height="150"
        x="20"
        y="30"
        fill="red"
        id="rect1308" />
     <rect
        width="140"
        height="80"
        x="50"
        y="50"
        fill="blue"
        id="rect1310" />
   </g>
</svg>

When I run my code with this svg, and I click on the rectangle, I  
obtain a message "svg error, testEvent not defined", perhaps it comes  
from my event in the svg file ?


Quoting thomas.deweese@kodak.com:

> Hi nmotte,
>
> nmotte@messel.emse.fr wrote on 04/16/2006 08:41:03 PM:
>
>> I'm a beginner in svg and batik. I would like to use an svg event (for
>> example an onclick on a rectangle in a svg file) in my java code to
>> call a method. I thought that the example given the page Java
>> Scripting of the Batik website would do that, but apparently it's not
>> the case.
>
>    I think it does this, perhaps you can explain why you think it
> doesn't?  In particular the following code from the example on
> that page seems like it meets your needs (you can do what ever you want
> in the handleEvent method of the listeners).
>
>         public void registerListeners() {
>         // Gets an element from the loaded document.
>         Element elt = document.getElementById("elt-id");
>         EventTarget t = (EventTarget)elt;
>
>         // Adds a 'onload' listener
>         t.addEventListener("SVGLoad", new OnLoadAction(), false);
>
>         // Adds a 'onclick' listener
>         t.addEventListener("click", new OnClickAction(), false);
>     }
>
>     public class OnLoadAction implements EventListener {
>         public void handleEvent(Event evt) {
>             // Make some actions here...
>
>             // ...for example start an animation loop:
>             window.setInterval(new Animation(), 50);
>         }
>     }
>
>     public class OnClickAction implements EventListener {
>         public void handleEvent(Event evt) {
>             // Make some actions here...
>
>             // ...for example schedule an action for later:
>             window.setTimeout(new DelayedTask(), 500);
>         }
>     }
>
>> Is there any simple solution ?
>>
>> ----------------------------------------------------------------
>> This message was sent using IMP, the Internet Messaging Program.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
>> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>



----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.


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


Re: catch svg events

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

nmotte@messel.emse.fr wrote on 04/16/2006 08:41:03 PM:

> I'm a beginner in svg and batik. I would like to use an svg event (for 
> example an onclick on a rectangle in a svg file) in my java code to 
> call a method. I thought that the example given the page Java 
> Scripting of the Batik website would do that, but apparently it's not 
> the case.

   I think it does this, perhaps you can explain why you think it
doesn't?  In particular the following code from the example on
that page seems like it meets your needs (you can do what ever you want
in the handleEvent method of the listeners).

        public void registerListeners() {
        // Gets an element from the loaded document.
        Element elt = document.getElementById("elt-id");
        EventTarget t = (EventTarget)elt;

        // Adds a 'onload' listener
        t.addEventListener("SVGLoad", new OnLoadAction(), false);

        // Adds a 'onclick' listener
        t.addEventListener("click", new OnClickAction(), false);
    }

    public class OnLoadAction implements EventListener {
        public void handleEvent(Event evt) {
            // Make some actions here...
 
            // ...for example start an animation loop:
            window.setInterval(new Animation(), 50);
        }
    }

    public class OnClickAction implements EventListener {
        public void handleEvent(Event evt) {
            // Make some actions here...

            // ...for example schedule an action for later:
            window.setTimeout(new DelayedTask(), 500);
        }
    }

> Is there any simple solution ?
> 
> ----------------------------------------------------------------
> This message was sent using IMP, the Internet Messaging Program.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 


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