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-unsubscr...@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-h...@xmlgraphics.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscr...@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-h...@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-unsubscr...@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-h...@xmlgraphics.apache.org