xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dan Slater <dsla...@simulsoft.com>
Subject Re: Determine clicked 'use' element
Date Thu, 29 Jan 2009 18:53:09 GMT
Dan Slater wrote:
> Age Bosma wrote:
>> 2009/1/29  <thomas.deweese@kodak.com>:
>>  
>>> Hi Age,
>>>
>>> Age Bosma <agebosma@gmail.com> wrote on 01/28/2009 02:43:51 PM:
>>>
>>>    
>>>> Age Bosma wrote:
>>>>
>>>>      
>>>>> getTarget() Always gives me the referenced element if someone clicked
>>>>> on the 'use' element and thus e.g. the id of the rect. This is 
>>>>> fine if
>>>>> someone actually clicked on the 'rect' element but I'd like to know
>>>>> which 'use' element was clicked (the id of the use element) if 
>>>>> someone
>>>>> clicked on that.
>>>>>         
>>>    Well you can walk up the tree from the clicked element to the
>>> use element.  In Batik the elements are all basically normal DOM 
>>> elements
>>> (they should be SVGElementInstance references).  In anycase you should
>>> be able to use 'getParentNode()' (java) or 'parentNode' (ecmascript) to
>>> walk up the tree checking if the element is a use element or not.
>>>
>>>     
>>
>> The problem is that I'm unable to get to a use element. If I click on
>> a use element, I'm getting exactly the same back as when I click on
>> the element the use element is referencing (getNodeName(), getId(),
>> etc.). If you look at the initial e-mail describing the situation,
>> getNodeName() will always be 'rect' even if I click on the use
>> element. The same goes for the Id.
>>
>> A use element does not contain any children, it only references an
>> element to get a clone. getParentNode() is therefor no option to get
>> to the use element.
>>
>> Age
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
>> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>>
>>
>>   
> Hi, Age,
>
> I think this is really a W3C DOM navigation question but I suppose 
> Batik has something to do with the DOM ;-)  I think you need to climb 
> the DOM  from your target  and 'escape' from the local use element 
> <defs> reference using this test of each Node n (quick and dirty):
>
>                if (n instanceof SVGOMCSSImportedElementRoot){
>                    
> n=((SVGOMCSSImportedElementRoot)n).getCSSParentElement();
>                }
>
> It was a bit of a bear to figure that out and maybe someone could tell 
> me an easier way or better way!
>
> Then test for the custom attribute that Thomas mentioned.  I haven't 
> implemented your exact case, but in a separate part of my app I supply 
> an SVGElement as a DOMTreeWalker root with a filter like so:
>
>    class ReceptorsAll implements NodeFilter {
>
>           public short acceptNode(Node n) {
>                String segtype = ((SVGElement)n).getAttributeNS(null, 
> "segtype");
>                if (n.getNodeType() == Node.ELEMENT_NODE) {
>                    if (segtype.equals("receptor") &&
>                            (n.getNodeName().equalsIgnoreCase("g")
>                            || n.getNodeName().equalsIgnoreCase("use"))
>                        ) {
>                        return FILTER_ACCEPT;
>                    } else return FILTER_SKIP;
>                } else
>                    return FILTER_SKIP;
>           }
>
> As you see, the filter is pretty highly coupled to my DOM structure 
> and looks for the custom attribute.  Then I extract all the id's from 
> the walker's children.
>
> I will probably have to implement some version of what you're doing in 
> the future, so I'd be curious as to your solution.  Hope this helps...
>
> Dan Slater
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>
Hi, Age,

Never mind the filter.  This will get you where you want to go...supply 
your click target, and modify it to break at the use element - you'll 
see you can access the id:

    public static SVGElement returnNextDOMEltUp(SVGElement svgElt) {
        Node n = svgElt;
        String name = "start";

        while (!name.equals("g")) {
                if (n instanceof SVGOMCSSImportedElementRoot){
                    
n=((SVGOMCSSImportedElementRoot)n).getCSSParentElement();
                } else    {
                    n = n.getParentNode();
                    if (n instanceof SVGOMDocument) return null;
                }
                if (n instanceof SVGElement) {
                    showIdAndNodeName(n);
                }
                name = n.getNodeName();
        }
       
        if (n instanceof SVGElement) {
                svgElt = (SVGElement)n;
            } else svgElt = null;
       
        return svgElt;
    }

Cheers!  Dan

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


Mime
View raw message