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 James Burton <J....@brighton.ac.uk> on 2013/08/09 00:25:06 UTC

Adding links to a document

I'm new to using batik, and I want to turn all text elements in an 
SVGDocument into links which pass the text to a callback. Can you point 
me to an example of something similar? I'm finding the text elements 
like this:

SVGDocument doc = ...
Element svgRoot = doc.getDocumentElement();
NodeList labels = svgRoot.getElementsByTagNameNS("*", "text");
Node n;
for(int i=0;i<labels.getLength();i++) {
   n = labels.item(i);
   log.info("A label:"+n.getFirstChild().getNodeValue());
   //presumably, replace n with a link element that contains n?
}

Thanks in advance,

Jim

___________________________________________________________
This email has been scanned by MessageLabs' Email Security
System on behalf of the University of Brighton.
For more information see http://www.brighton.ac.uk/is/spam/
___________________________________________________________

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


Re: Adding links to a document

Posted by Robert Marcano <ro...@marcanoonline.com>.
On 08/08/2013 05:55 PM, James Burton wrote:
> I'm new to using batik, and I want to turn all text elements in an
> SVGDocument into links which pass the text to a callback. Can you point
> me to an example of something similar? I'm finding the text elements
> like this:

There is no need to

>
> SVGDocument doc = ...
> Element svgRoot = doc.getDocumentElement();
> NodeList labels = svgRoot.getElementsByTagNameNS("*", "text");
> Node n;
> for(int i=0;i<labels.getLength();i++) {
>    n = labels.item(i);
>    log.info("A label:"+n.getFirstChild().getNodeValue());

      EventListener l = new EventListener {
         public void handleEvent(Event evt) {
            // you can try to use the current n element here, that
            // means you need to make it final and build one listener
            // for each element or use evt.getTarget() to get the
            // element and use only one listener instance per document
         }
     }
     n.addEventListener("click", l, false)

>    //presumably, replace n with a link element that contains n?
> }
>

You can use too event bubbling on the addEventListener and attach only 
one listener on the document root and check the element type you are 
getting, instead of attaching a listener to each element, the advantage 
of this method is that it work for all text elements you add later 
without need to add a listener to them


> Thanks in advance,
>
> Jim
>
> ___________________________________________________________
> This email has been scanned by MessageLabs' Email Security
> System on behalf of the University of Brighton.
> For more information see http://www.brighton.ac.uk/is/spam/
> ___________________________________________________________
>
> ---------------------------------------------------------------------
> 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


Re: Adding links to a document

Posted by jonathan wood <jo...@gmail.com>.
This prob won't compile, and some methods may be misnamed, but it should be
close in spirit!

note that you need to use the xlink namespace for the xlink:href attribute
as shown below.  Since a node can only be in one place I chose to use the
shorcut of adding n to a and letting the dom remove it from n.parent.


hope it helps

...

SVGElement a = null;
for(int i=0;i<labels.getLength();i++) {
  n = labels.item(i);
  log.info("A label:"+n.getFirstChild().**getNodeValue());
  //presumably, replace n with a link element that contains n?
  a = document.createElementNS(SVGNAMESPACE, "a");
  a.setAttributeNS(XLINKNAMESPACE, "xlink;href", my_special_url);
  n.getParentNode().appendChild(a);
  a.appendChild(n);
}
...


On Thu, Aug 8, 2013 at 6:25 PM, James Burton <J....@brighton.ac.uk>wrote:

> I'm new to using batik, and I want to turn all text elements in an
> SVGDocument into links which pass the text to a callback. Can you point me
> to an example of something similar? I'm finding the text elements like this:
>
> SVGDocument doc = ...
> Element svgRoot = doc.getDocumentElement();
> NodeList labels = svgRoot.**getElementsByTagNameNS("*", "text");
> Node n;
> for(int i=0;i<labels.getLength();i++) {
>   n = labels.item(i);
>   log.info("A label:"+n.getFirstChild().**getNodeValue());
>   //presumably, replace n with a link element that contains n?
> }
>
> Thanks in advance,
>
> Jim
>
> ______________________________**_____________________________
> This email has been scanned by MessageLabs' Email Security
> System on behalf of the University of Brighton.
> For more information see http://www.brighton.ac.uk/is/**spam/<http://www.brighton.ac.uk/is/spam/>
> ______________________________**_____________________________
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: batik-users-unsubscribe@**xmlgraphics.apache.org<ba...@xmlgraphics.apache.org>
> For additional commands, e-mail: batik-users-help@xmlgraphics.**apache.org<ba...@xmlgraphics.apache.org>
>
>

Re: Adding links to a document

Posted by Robert Marcano <ro...@marcanoonline.com>.
On 08/09/2013 10:26 AM, James Burton wrote:
> On 09/08/13 13:58, Robert Marcano wrote:
>> On 08/08/2013 05:55 PM, James Burton wrote:
>>> I'm new to using batik, and I want to turn all text elements in an
>>> SVGDocument into links which pass the text to a callback. Can you point
>>> me to an example of something similar? I'm finding the text elements
>>> like this:
>>
>> There is no need to modify the document
>>
>
> Hi Robert, thanks for your reply. I've applied your suggestion but
> haven't managed to add links to my document yet as far as I can tell.
> The pointer doesn't change over the text (not sure if it that's the
> default) and more importantly the event handler isn't triggered. Here's
> what it looks like now:

IIRC if your document doesn't contains JavaScript, the canvas consider 
it is a static document. You need to tell to the canvas before loading 
the document you want them to be dynamic ALWAYS_DYNAMIC or interactive 
(ALWAYS_INTERACTIVE), I don't remember which one is for events

canvas.setDocumentState(ALWAYS_DYNAMIC)

You can add a CSS stylesheet (inline or linked) that change the cursor 
property to all text elements, you can use standard DOM APIs to add the 
style element attributes and content

Element styleElem = doc.createElementNS("http://www.w3.org/2000/svg", 
"style");
styleElem.setAttribute("type", "text/css");
Text styleText = doc.createTextNode("text {cursor: pointer}");
styleElem.appendChild(styleText);
doc.getDocumentElement().appendChild(styleElem);

Before setting the document on the canvas I think, I don't remember if 
ALWAYS_DYNAMIC notices changes on the CSS

But adding the CSS you are already modifying the document, You can play 
with the SVGUserAgent interface and SVGUserAgentGUIAdapter and override 
the method that return the user stylesheet, this way all you documents 
can have the same CSS without modifying the document

All this because you want a event handler in Java, you don't get the 
default <a> links look and feel adding those

>
> for(int i=0;i<labels.getLength();i++) {
>    n = labels.item(i);
>    log.info("A label:"+n.getFirstChild().getNodeValue());//prints as
>                                                          //expected
>    EventListener l = new EventListener() {
>      public void handleEvent(Event evt) {
>        log.info("Clicked on "+(
>          (Node) evt.getTarget()).getFirstChild().getNodeValue());
>      }
>    };
>    ((EventTarget) n).addEventListener("click", l, false);
> }
>
> Quite happy to RTFM if someone can point me to a suitable FM. I'm aware
> of http://xmlgraphics.apache.org/batik/using/, but it's low on examples.
> Cheers,
>
> Jim
>
>>>
>>> SVGDocument doc = ...
>>> Element svgRoot = doc.getDocumentElement();
>>> NodeList labels = svgRoot.getElementsByTagNameNS("*", "text");
>>> Node n;
>>> for(int i=0;i<labels.getLength();i++) {
>>>    n = labels.item(i);
>>>    log.info("A label:"+n.getFirstChild().getNodeValue());
>>
>>       EventListener l = new EventListener {
>>          public void handleEvent(Event evt) {
>>             // you can try to use the current n element here, that
>>             // means you need to make it final and build one listener
>>             // for each element or use evt.getTarget() to get the
>>             // element and use only one listener instance per document
>>          }
>>      }
>>      n.addEventListener("click", l, false)
>>
>>>    //presumably, replace n with a link element that contains n?
>>> }
>>>
>>
>> You can use too event bubbling on the addEventListener and attach only
>> one listener on the document root and check the element type you are
>> getting, instead of attaching a listener to each element, the advantage
>> of this method is that it work for all text elements you add later
>> without need to add a listener to them
>>
>>
>>> Thanks in advance,
>>>
>>> Jim
>>>
>>> ___________________________________________________________
>>> This email has been scanned by MessageLabs' Email Security
>>> System on behalf of the University of Brighton.
>>> For more information see http://www.brighton.ac.uk/is/spam/
>>> ___________________________________________________________
>>>
>>> ---------------------------------------------------------------------
>>> 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 email has been scanned by MessageLabs' Email Security
>> System on behalf of the University of Brighton.
>> For more information see http://www.brighton.ac.uk/is/spam/
>> ___________________________________________________________
>


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


Re: Adding links to a document

Posted by James Burton <J....@brighton.ac.uk>.
On 09/08/13 13:58, Robert Marcano wrote:
> On 08/08/2013 05:55 PM, James Burton wrote:
>> I'm new to using batik, and I want to turn all text elements in an
>> SVGDocument into links which pass the text to a callback. Can you point
>> me to an example of something similar? I'm finding the text elements
>> like this:
>
> There is no need to modify the document
>

Hi Robert, thanks for your reply. I've applied your suggestion but 
haven't managed to add links to my document yet as far as I can tell. 
The pointer doesn't change over the text (not sure if it that's the 
default) and more importantly the event handler isn't triggered. Here's 
what it looks like now:

for(int i=0;i<labels.getLength();i++) {
   n = labels.item(i);
   log.info("A label:"+n.getFirstChild().getNodeValue());//prints as
                                                         //expected
   EventListener l = new EventListener() {
     public void handleEvent(Event evt) {
       log.info("Clicked on "+(
         (Node) evt.getTarget()).getFirstChild().getNodeValue());
     }
   };
   ((EventTarget) n).addEventListener("click", l, false);
}

Quite happy to RTFM if someone can point me to a suitable FM. I'm aware 
of http://xmlgraphics.apache.org/batik/using/, but it's low on examples. 
Cheers,

Jim

>>
>> SVGDocument doc = ...
>> Element svgRoot = doc.getDocumentElement();
>> NodeList labels = svgRoot.getElementsByTagNameNS("*", "text");
>> Node n;
>> for(int i=0;i<labels.getLength();i++) {
>>    n = labels.item(i);
>>    log.info("A label:"+n.getFirstChild().getNodeValue());
>
>       EventListener l = new EventListener {
>          public void handleEvent(Event evt) {
>             // you can try to use the current n element here, that
>             // means you need to make it final and build one listener
>             // for each element or use evt.getTarget() to get the
>             // element and use only one listener instance per document
>          }
>      }
>      n.addEventListener("click", l, false)
>
>>    //presumably, replace n with a link element that contains n?
>> }
>>
>
> You can use too event bubbling on the addEventListener and attach only
> one listener on the document root and check the element type you are
> getting, instead of attaching a listener to each element, the advantage
> of this method is that it work for all text elements you add later
> without need to add a listener to them
>
>
>> Thanks in advance,
>>
>> Jim
>>
>> ___________________________________________________________
>> This email has been scanned by MessageLabs' Email Security
>> System on behalf of the University of Brighton.
>> For more information see http://www.brighton.ac.uk/is/spam/
>> ___________________________________________________________
>>
>> ---------------------------------------------------------------------
>> 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 email has been scanned by MessageLabs' Email Security
> System on behalf of the University of Brighton.
> For more information see http://www.brighton.ac.uk/is/spam/
> ___________________________________________________________

-- 
Dr Jim Burton
Senior Lecturer in Computing
School of Computing, Engineering and Mathematics
University of Brighton

___________________________________________________________
This email has been scanned by MessageLabs' Email Security
System on behalf of the University of Brighton.
For more information see http://www.brighton.ac.uk/is/spam/
___________________________________________________________

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


Re: Adding links to a document

Posted by Robert Marcano <ro...@marcanoonline.com>.
On 08/08/2013 05:55 PM, James Burton wrote:
> I'm new to using batik, and I want to turn all text elements in an
> SVGDocument into links which pass the text to a callback. Can you point
> me to an example of something similar? I'm finding the text elements
> like this:

There is no need to modify the document

>
> SVGDocument doc = ...
> Element svgRoot = doc.getDocumentElement();
> NodeList labels = svgRoot.getElementsByTagNameNS("*", "text");
> Node n;
> for(int i=0;i<labels.getLength();i++) {
>    n = labels.item(i);
>    log.info("A label:"+n.getFirstChild().getNodeValue());

      EventListener l = new EventListener {
         public void handleEvent(Event evt) {
            // you can try to use the current n element here, that
            // means you need to make it final and build one listener
            // for each element or use evt.getTarget() to get the
            // element and use only one listener instance per document
         }
     }
     n.addEventListener("click", l, false)

>    //presumably, replace n with a link element that contains n?
> }
>

You can use too event bubbling on the addEventListener and attach only 
one listener on the document root and check the element type you are 
getting, instead of attaching a listener to each element, the advantage 
of this method is that it work for all text elements you add later 
without need to add a listener to them


> Thanks in advance,
>
> Jim
>
> ___________________________________________________________
> This email has been scanned by MessageLabs' Email Security
> System on behalf of the University of Brighton.
> For more information see http://www.brighton.ac.uk/is/spam/
> ___________________________________________________________
>
> ---------------------------------------------------------------------
> 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