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 "Warren W. Thompson" <wt...@altaira.com> on 2002/05/03 17:13:33 UTC

Problem Using The tspan Element And The UpdateManager

Hi. I have encountered an unusual behavior when dealing with tspan
elements and the Batik UpdateManager. Essentially, I am trying to
change a text label within an SVG file. I have succeeded in changing
the label when the target XML text node is contained within a text
element. However, I have been less successful when the target XML
text node is contained in a tspan element. Also, I am using the
Java 2 SDK 1.4.0 and working with a self-compiled copy of Batik 1.5
that I checked out of CVS on April 29, 2002.

I do currently have a work-around, see example 3, for this problem.
However, I would like to know if I am incorrectly using the DOM
tree or if this is an actual bug.

Thanks in advance for your assistance, and keep up the great work!

Warren

Beer is proof that God loves us and wants us to be happy.
                                             --Benjamin Franklin


1. Here is an example of code that successfully displays the updated
    label in a JSVGCanvas:

SVG :  <text id="targetLabel" ...>Hello</text>

Java:  // Obtain the target XML text element.
        SVGTextElement svgText =
            (SVGTextElement)doc.getElementById( "targetLabel" );
        final Text xmlText = (Text)svgText.getFirstChild();

        // Alternate labels each time this action is invoked.
        String oldLabel = xmlText.getData();
        String newLabel = null;
        if( oldLabel.equals( "Hello" ) ){
            newLabel = "World";
        } else {
            newLabel = "Hello";
        }

        // Schedule the DOM tree update using the Batik update manager.
        final String  updateText = newLabel;
        UpdateManager updateMgr = m_svgCanvas.getUpdateManager();
        RunnableQueue svgEventQueue = updateMgr.getUpdateRunnableQueue();
        svgEventQueue.invokeLater( new Runnable( ){
            public void run( ){
                xmlText.setData( updateText );
            }
        } );


2. Here is an example of code that I would expect to work; however, the
    label is never updated in the JSVGCanvas:

SVG :  <text id="targetLabel" ...><tspan ...>Hello</tspan></text>

Java:  // Obtain the target XML text element.
        SVGTextElement svgText =
            (SVGTextElement)doc.getElementById( "targetLabel" );
        SVGTSpanElement svgTSpan = (SVGTSpanElement)doc.getFirstChild();
        final Text xmlText = (Text)svgTSpan.getFirstChild();

        // Alternate labels each time this action is invoked.
        String oldLabel = xmlText.getData();
        String newLabel = null;
        if( oldLabel.equals( "Hello" ) ){
            newLabel = "World";
        } else {
            newLabel = "Hello";
        }

        // Schedule the DOM tree update using the Batik update manager.
        final String  updateText = newLabel;
        UpdateManager updateMgr = m_svgCanvas.getUpdateManager();
        RunnableQueue svgEventQueue = updateMgr.getUpdateRunnableQueue();
        svgEventQueue.invokeLater( new Runnable( ){
            public void run( ){
                xmlText.setData( updateText );
            }
        } );


3. I discovered a work-around that involves replacing the tspan element
    that is contained within the text element with a cloned and updated
    copy of the tspan and XML text nodes. This code successfully causes
    updates in the JSVGCanvas:

SVG :  <text id="targetLabel" ...><tspan ...>Hello</tspan></text>

Java:  // Obtain the target XML text element.
        final SVGTextElement svgText =
            (SVGTextElement)doc.getElementById( "targetLabel" );
        final SVGTSpanElement svgTSpan = (SVGTSpanElement)doc.getFirstChild();

        // Make a copy of the tspan node.
        final SVGTSpanElement cpyTSpan =
            (SVGTSpanElement)svgTSpan.cloneNode( true );
        Text txtNode = (Text)cpyTSpan.getFirstChild();

        // Alternate labels each time this action is invoked.
        String oldLabel = txtNode.getData();
        String newLabel = null;
        if( oldLabel.equals( "Hello" ) ){
            newLabel = "World";
        } else {
            newLabel = "Hello";
        }
        txtNode.setData( newLabel );

        // Schedule the DOM tree update using the Batik update manager.
        UpdateManager updateMgr = m_svgCanvas.getUpdateManager();
        RunnableQueue svgEventQueue = updateMgr.getUpdateRunnableQueue();
        svgEventQueue.invokeLater( new Runnable( ){
            public void run( ){
                svgText.removeChild( svgTSpan );
                xmlText.appendChild( cpyTSpan );
            }
        } );


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