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 still <st...@commondata.net> on 2002/07/19 09:19:47 UTC

add script functions and event via batik api--how

Hi,all:
	I try to create a SVG DOM object via batik api.
The SVG DOM should be look like this:
//------------
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="400" height="450">
<script type="text/ecmascript">

    function showDialog(msg) {
        alert(msg);
    }
    </script>

<rect x="10" y="20" width="100" style="fill:red" height="50"
onmousedown="showDialog('onmousedown')"/>
</svg>
//------------
it is easy to create the "rect" element to the svg dom tree.but I can
not
figure out how to add the script functions and how to add the invoke
statement in the element rect.
It seems I should use the package org.apache.batik.script.rhino .
Who could show me a simple sample about it?
Thanks in advance


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


RE: add script functions and event via batik api--how

Posted by Thomas E Deweese <th...@kodak.com>.
>>>>> "JA" == J Aaron Farr <ja...@yahoo.com> writes:

JA> --- Thomas E Deweese <th...@kodak.com> wrote:

>> If you want to handle the event in Java, you can add a Java
>> listener to the SVG Elements in the DOM directly.

JA>   Is there an example somewhere of how to do this in java?  That
JA> is, how to add the listener to the SVG Element in the DOM?  This
JA> is something I've been looking at doing too.

Take this with a grain of salt, it's abstracted loosely from the
SVGTextElementBridge class.  This is all DOM level 2 event stuff
nothing the slightest bit Batik specific here.

---

import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.events.MutationEvent;

    Element e = ....;
    EventTarget evtTarget = (EventTarget)e;

    /**
     * The DOM EventListener to receive 'DOMNodeRemoved' event.
     */
    EventListener childNodeRemovedEventListener = 
        new EventListener() {
            /**
             * Handles 'DOMNodeRemoved' event type.
             */
            public void handleEvent(Event evt) {
                MutationEvent me = (MutationEvent)evt;
            }
        };  

    //to be notified when a child is removed from the element.
    evtTarget.addEventListener
        ("DOMNodeRemoved", childNodeRemovedEventListener, true);


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


RE: add script functions and event via batik api--how

Posted by J Aaron Farr <ja...@yahoo.com>.
--- Thomas E Deweese <th...@kodak.com> wrote:
> >>>>> "s" == still  <st...@commondata.net> writes:
> 
> s> It really works,and yes it is better to put the script in the cdata
> s> section.  But the problem is how to add a event(eg.a mousedown
> s> event) to the element I create to invoke the script function I
> s> defined as following?
> 
> s> <rect x="10" y="20" width="100" style="fill:red" height="50"
> s> onmousedown="showDialog('onmousedown')"/>
> s> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^And is it the normal way to
> s> do so ^_^? I thought there will be methods like:
> 
> s> doc.insertFunction(Function f) ; node.insertEvent(...);
> 
>    No but there is:
> 
>    rect.setAttributeNS(null,"onmousedown", "showDialog('onmousedown')");
> 
>    If you want to handle the event in Java, you can add a Java
> listener to the SVG Elements in the DOM directly.
> 
  Is there an example somewhere of how to do this in java?  That is, how to add
the listener to the SVG Element in the DOM?  This is something I've been
looking at doing too.

Thanks,
jaaron


__________________________________________________
Do You Yahoo!?
Yahoo! Health - Feel better, live better
http://health.yahoo.com

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


RE: add script functions and event via batik api--how

Posted by Thomas E Deweese <th...@kodak.com>.
>>>>> "s" == still  <st...@commondata.net> writes:

s> It really works,and yes it is better to put the script in the cdata
s> section.  But the problem is how to add a event(eg.a mousedown
s> event) to the element I create to invoke the script function I
s> defined as following?

s> <rect x="10" y="20" width="100" style="fill:red" height="50"
s> onmousedown="showDialog('onmousedown')"/>
s> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^And is it the normal way to
s> do so ^_^? I thought there will be methods like:

s> doc.insertFunction(Function f) ; node.insertEvent(...);

   No but there is:

   rect.setAttributeNS(null,"onmousedown", "showDialog('onmousedown')");

   If you want to handle the event in Java, you can add a Java
listener to the SVG Elements in the DOM directly.


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


RE: add script functions and event via batik api--how

Posted by still <st...@commondata.net>.
Thank you.It really works,and yes it is better to put the script in the
cdata section.
But the  problem is how to add a event(eg.a mousedown event) to the
element I create to invoke the script function I defined as following? 

<rect x="10" y="20" width="100" style="fill:red" height="50"
 onmousedown="showDialog('onmousedown')"/>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^And is it the normal way to do so
^_^? I thought there will be  methods like:

doc.insertFunction(Function f) ; 
node.insertEvent(...);



-----Original Message-----
From: Vincent Hardy [mailto:vhardy@apache.org] 
Sent: 2002年7月19日 16:00
To: Batik Users
Subject: Re: add script functions and event via batik api--how

Hello,

You can do something like:

Document doc = ...; // Your SVG document.
Element svg = doc.getDocumentElement();
Element rect doc.createElementNS(svgNS, "rect");
svg.appendChild(rect);
rect.setAttribute(...);
Element script = doc.createElementNS(svgNS, "script");
Element scriptCode = doc.createTextElement("function showDialog() { \n
alert('msg'); })");
script.appendChild(scriptCode);
svg.appendChild(script);


Actually, it is better practice to put your code in a CDATA section
and use the createCDATASection method to do so.

I hope this helps,
Regards,
Vincent.

still wrote:
> Hi,all:
> 	I try to create a SVG DOM object via batik api.
> The SVG DOM should be look like this:
> //------------
> <?xml version="1.0" encoding="UTF-8"?>
> <svg xmlns="http://www.w3.org/2000/svg" width="400" height="450">
> <script type="text/ecmascript">
> 
>     function showDialog(msg) {
>         alert(msg);
>     }
>     </script>
> 
> <rect x="10" y="20" width="100" style="fill:red" height="50"
> onmousedown="showDialog('onmousedown')"/>
> </svg>
> //------------
> it is easy to create the "rect" element to the svg dom tree.but I can
> not
> figure out how to add the script functions and how to add the invoke
> statement in the element rect.
> It seems I should use the package org.apache.batik.script.rhino .
> Who could show me a simple sample about it?
> Thanks in advance
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org



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



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


Re: add script functions and event via batik api--how

Posted by Vincent Hardy <vh...@apache.org>.
Hello,

You can do something like:

Document doc = ...; // Your SVG document.
Element svg = doc.getDocumentElement();
Element rect doc.createElementNS(svgNS, "rect");
svg.appendChild(rect);
rect.setAttribute(...);
Element script = doc.createElementNS(svgNS, "script");
Element scriptCode = doc.createTextElement("function showDialog() { \n
alert('msg'); })");
script.appendChild(scriptCode);
svg.appendChild(script);

Actually, it is better practice to put your code in a CDATA section
and use the createCDATASection method to do so.

I hope this helps,
Regards,
Vincent.

still wrote:
> Hi,all:
> 	I try to create a SVG DOM object via batik api.
> The SVG DOM should be look like this:
> //------------
> <?xml version="1.0" encoding="UTF-8"?>
> <svg xmlns="http://www.w3.org/2000/svg" width="400" height="450">
> <script type="text/ecmascript">
> 
>     function showDialog(msg) {
>         alert(msg);
>     }
>     </script>
> 
> <rect x="10" y="20" width="100" style="fill:red" height="50"
> onmousedown="showDialog('onmousedown')"/>
> </svg>
> //------------
> it is easy to create the "rect" element to the svg dom tree.but I can
> not
> figure out how to add the script functions and how to add the invoke
> statement in the element rect.
> It seems I should use the package org.apache.batik.script.rhino .
> Who could show me a simple sample about it?
> Thanks in advance
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org



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