tkormann    02/02/14 04:37:55

  Modified:    sources/org/apache/batik/bridge BridgeContext.java
                        SVGGElementBridge.java
               sources/org/apache/batik/gvt AbstractGraphicsNode.java
                        UpdateTracker.java
  Added:       samples/tests/spec/scripting add.svg remove.svg
  Log:
  - add/remove child on the DOM now works
  - two new tests added add.svg and remove.svg
  
  - two small patches have been applied to support add and remove
  
    1. updateTracker does not put 'null' new region (remove usecase)
  
    2. AbstractGraphicsNode.invalidateGeometryCache,
       if bounds == null does *not* mean the parent is dirty too
       -> empty <g> + appendChild usecase
  
  Revision  Changes    Path
  1.1                  xml-batik/samples/tests/spec/scripting/add.svg
  
  Index: add.svg
  ===================================================================
  <?xml version="1.0" standalone="no"?>
  <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
  "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd";>
  
  <!-- ====================================================================== -->
  <!-- Copyright (C) The Apache Software Foundation. All rights reserved.     -->
  <!--                                                                        -->
  <!-- This software is published under the terms of the Apache Software      -->
  <!-- License version 1.1, a copy of which has been included with this       -->
  <!-- distribution in the LICENSE file.                                      -->
  <!-- ====================================================================== -->
  
  <!-- ====================================================================== -->
  <!-- append child test                                                      -->
  <!--                                                                        -->
  <!-- @author [EMAIL PROTECTED]                                               -->
  <!-- @version $Id: add.svg,v 1.1 2002/02/14 12:37:54 tkormann Exp $ -->
  <!-- ====================================================================== -->
  
  <?xml-stylesheet type="text/css" href="../../resources/style/test.css" ?>  
  
  <svg xmlns="http://www.w3.org/2000/svg"; 
       xmlns:xlink="http://www.w3.org/1999/xlink";
       id="body" width="450" height="500" viewBox="0 0 450 500">
  
      <title>appendChild test 'onload'</title>
  
      <text x="50%" y="45" class="title">appendChild test 'onload'</text>
  
      <script type="text/ecmascript">
  
      var svgNamespaceURI = "http://www.w3.org/2000/svg";;
  
      function build(evt) {
          var g = evt.target;
          var document = g.ownerDocument;
          var e;
  
          e = createElement(document, 200, 100, "fill:crimson");
          g.appendChild(e);
  
          e = createElement(document, 250, 100, "fill:orange");
          g.appendChild(e);
  
          e = createElement(document, 150, 100, "fill:gold");
          g.insertBefore(e, g.firstChild);
  
          var ee = createElement(document, 100, 100, "fill:#eee");
          g.insertBefore(ee, e);
      }
  
      function createElement(g, x, y, style) {
          var e = document.createElementNS(svgNamespaceURI, "rect");
          e.setAttribute("x", x);
          e.setAttribute("y", y);
          e.setAttribute("width", 100);
          e.setAttribute("height", 100);
          e.setAttribute("style", style);
          return e;
      }
  
      </script>
  
      <g onload="build(evt)" id="test-content">
      </g>
          <text x="225" y="220" style="text-anchor:middle">constructed using 
'onload'</text>
      <g>
         <rect x="100" y="300" width="100" height="100" style="fill:#eee"/>
         <rect x="150" y="300" width="100" height="100" style="fill:gold"/>
         <rect x="200" y="300" width="100" height="100" style="fill:crimson"/>
         <rect x="250" y="300" width="100" height="100" style="fill:orange"/>
      </g>
          <text x="225" y="420" style="text-anchor:middle">reference</text>
  </svg>
  
  
  
  1.1                  xml-batik/samples/tests/spec/scripting/remove.svg
  
  Index: remove.svg
  ===================================================================
  <?xml version="1.0" standalone="no"?>
  <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
  "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd";>
  
  <!-- ====================================================================== -->
  <!-- Copyright (C) The Apache Software Foundation. All rights reserved.     -->
  <!--                                                                        -->
  <!-- This software is published under the terms of the Apache Software      -->
  <!-- License version 1.1, a copy of which has been included with this       -->
  <!-- distribution in the LICENSE file.                                      -->
  <!-- ====================================================================== -->
  
  <!-- ====================================================================== -->
  <!-- append child test                                                      -->
  <!--                                                                        -->
  <!-- @author [EMAIL PROTECTED]                                               -->
  <!-- @version $Id: remove.svg,v 1.1 2002/02/14 12:37:54 tkormann Exp $ -->
  <!-- ====================================================================== -->
  
  <?xml-stylesheet type="text/css" href="../../resources/style/test.css" ?>  
  
  <svg xmlns="http://www.w3.org/2000/svg"; 
       xmlns:xlink="http://www.w3.org/1999/xlink";
       id="body" width="450" height="500" viewBox="0 0 450 500">
  
      <title>removeChild test 'onload'</title>
  
      <text x="50%" y="45" class="title">removeChild test 'onload'</text>
  
      <script type="text/ecmascript">
  
      var svgNamespaceURI = "http://www.w3.org/2000/svg";;
  
      function remove(evt) {
          var g = evt.target;
          g.removeChild(g.firstChild); // remove grey
          g.removeChild(g.firstChild.nextSibling); // remove crimson
          g.removeChild(g.firstChild.nextSibling); // remove orange
          g.removeChild(g.firstChild); // remove gold
      }
  
      function removeAll(evt) {
          var g = evt.target;
  //        while (g.hasChildNodes) {
          while (g.firstChild != null) {
                  g.removeChild(g.firstChild);
          }
      }
  
      </script>
  
      <g onload="remove(evt)" id="test-content">
         <rect x="100" y="100" width="100" height="100" style="fill:#eee"/>
         <rect x="150" y="100" width="100" height="100" style="fill:gold"/>
         <rect x="200" y="100" width="100" height="100" style="fill:crimson"/>
         <rect x="250" y="100" width="100" height="100" style="fill:orange"/>
      </g>
  
      <g onload="removeAll(evt)" id="test-content">
         <rect x="100" y="300" width="100" height="100" style="fill:#eee"/>
         <rect x="150" y="300" width="100" height="100" style="fill:gold"/>
         <rect x="200" y="300" width="100" height="100" style="fill:crimson"/>
         <rect x="250" y="300" width="100" height="100" style="fill:orange"/>
      </g>
  
  </svg>
  
  
  
  1.33      +23 -1     xml-batik/sources/org/apache/batik/bridge/BridgeContext.java
  
  Index: BridgeContext.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/bridge/BridgeContext.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- BridgeContext.java        12 Feb 2002 15:14:37 -0000      1.32
  +++ BridgeContext.java        14 Feb 2002 12:37:54 -0000      1.33
  @@ -47,7 +47,7 @@
    * a SVG DOM tree such as the current viewport or the user agent.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Thierry Kormann</a>
  - * @version $Id: BridgeContext.java,v 1.32 2002/02/12 15:14:37 tkormann Exp $
  + * @version $Id: BridgeContext.java,v 1.33 2002/02/14 12:37:54 tkormann Exp $
    */
   public class BridgeContext implements ErrorConstants {
   
  @@ -488,6 +488,7 @@
    
       /**
        * Returns the bridge associated with the specified element.
  +     *
        * @param element the element
        */
       public Bridge getBridge(Element element) {
  @@ -510,7 +511,28 @@
       }
   
       /**
  +     * Returns true if the specified element has a GraphicsNodeBridge
  +     * associated to it, false otherwise.
  +     *
  +     * @param element the element
  +     */
  +    public boolean hasGraphicsNodeBridge(Element element) {
  +        if (namespaceURIMap == null || element == null) {
  +            return false;
  +        }
  +        String namespaceURI = element.getNamespaceURI();
  +        String localName = element.getLocalName();
  +        namespaceURI = ((namespaceURI == null)? "" : namespaceURI);
  +        HashMap localNameMap = (HashMap) namespaceURIMap.get(namespaceURI);
  +        if (localNameMap == null) {
  +            return false;
  +        }
  +        return (localNameMap.get(localName) instanceof GraphicsNodeBridge);
  +    }
  +
  +    /**
        * Returns the bridge associated with the element type
  +     *
        * @param nameSpaceURI namespace of the requested element
        * @param localName element's local name
        *
  
  
  
  1.13      +136 -1    xml-batik/sources/org/apache/batik/bridge/SVGGElementBridge.java
  
  Index: SVGGElementBridge.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/bridge/SVGGElementBridge.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- SVGGElementBridge.java    13 Feb 2002 09:51:01 -0000      1.12
  +++ SVGGElementBridge.java    14 Feb 2002 12:37:54 -0000      1.13
  @@ -16,12 +16,17 @@
   import org.apache.batik.gvt.GraphicsNode;
   
   import org.w3c.dom.Element;
  +import org.w3c.dom.Node;
  +import org.w3c.dom.events.Event;
  +import org.w3c.dom.events.EventListener;
  +import org.w3c.dom.events.EventTarget;
  +import org.w3c.dom.events.MutationEvent;
   
   /**
    * Bridge class for the &lt;g> element.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Thierry Kormann</a>
  - * @version $Id: SVGGElementBridge.java,v 1.12 2002/02/13 09:51:01 tkormann Exp $
  + * @version $Id: SVGGElementBridge.java,v 1.13 2002/02/14 12:37:54 tkormann Exp $
    */
   public class SVGGElementBridge extends AbstractGraphicsNodeBridge {
   
  @@ -87,5 +92,135 @@
        */
       public boolean isComposite() {
           return true;
  +    }
  +
  +    // dynamic support
  +
  +    /**
  +     * This method is invoked during the build phase if the document
  +     * is dynamic. The responsability of this method is to ensure that
  +     * any dynamic modifications of the element this bridge is
  +     * dedicated to, happen on its associated GVT product.
  +     */
  +    protected void initializeDynamicSupport() {
  +        super.initializeDynamicSupport();
  +        ((EventTarget)e).addEventListener("DOMNodeInserted", 
  +                                          new DOMNodeInsertedEventListener(),
  +                                          false);
  +        ((EventTarget)e).addEventListener("DOMNodeRemoved", 
  +                                          new DOMNodeRemovedEventListener(),
  +                                          false);
  +    }
  +
  +    /**
  +     * Handles DOMNodeInserted events.
  +     *
  +     * @param evt the DOM mutation event
  +     */
  +    protected void handleDOMNodeInserted(MutationEvent evt) {
  +        //System.out.println("handleDOMNodeInserted "+e.getLocalName());
  +        Element childElt = (Element)evt.getTarget();
  +        // build the graphics node
  +        GVTBuilder builder = ctx.getGVTBuilder();
  +        GraphicsNode childNode = builder.build(ctx, childElt);
  +        if (childNode == null) {
  +            return;
  +        }
  +        // add the graphics node
  +        Node n = e.getFirstChild();
  +        Node lastChild = e.getLastChild();
  +        if (n == childElt) {
  +            // add at the beginning
  +            ((CompositeGraphicsNode)node).add(0, childNode);
  +        } else if (lastChild == childElt) {
  +            // append at the end
  +            ((CompositeGraphicsNode)node).add(childNode);
  +        } else {
  +            // find the index into the CompositeGraphicsNode
  +            int index = 0;
  +            while (n != lastChild && n != childElt) {
  +                if (n.getNodeType() == Node.ELEMENT_NODE) {
  +                    if (ctx.hasGraphicsNodeBridge((Element)n)) {
  +                        index++;
  +                    }
  +                }
  +                n = n.getNextSibling();
  +            }
  +            // insert at the index
  +            ((CompositeGraphicsNode)node).add(index, childNode);
  +        }
  +    }
  +
  +    /**
  +     * Handles DOMNodeRemoved events.
  +     *
  +     * @param evt the DOM mutation event
  +     */
  +    protected void handleDOMNodeRemoved(MutationEvent evt) {
  +        //System.out.println("handleDOMNodeRemoved "+e.getLocalName());
  +        Element childElt = (Element)evt.getTarget();
  +        Node n = e.getFirstChild();
  +        Node lastChild = e.getLastChild();
  +        if (n == childElt) {
  +            // remove first
  +            ((CompositeGraphicsNode)node).remove(0);
  +        } else if (lastChild == childElt) {
  +            // remove last
  +            CompositeGraphicsNode cgn = (CompositeGraphicsNode)node;
  +            cgn.remove(cgn.size()-1);
  +        } else {
  +            // find the index into the CompositeGraphicsNode
  +            int index = 0;
  +            while (n != lastChild && n != childElt) {
  +                if (n.getNodeType() == Node.ELEMENT_NODE) {
  +                    if (ctx.hasGraphicsNodeBridge((Element)n)) {
  +                        index++;
  +                    }
  +                }
  +                n = n.getNextSibling();
  +            }
  +            // remove at the index
  +            ((CompositeGraphicsNode)node).remove(index);
  +        }
  +    }
  +
  +    /**
  +     * The listener class for 'DOMNodeInserted' event.
  +     */
  +    protected class DOMNodeInsertedEventListener implements EventListener {
  +
  +        /**
  +         * Handles 'DOMNodeInserted' events and deleguates to the
  +         * 'handleDOMNodeInserted' method any changes to the
  +         * GraphicsNode if any.
  +         *
  +         * @param evt the DOM event
  +         */
  +        public void handleEvent(Event evt) {
  +            if (((MutationEvent)evt).getRelatedNode() != e) {
  +                return;
  +            }
  +            handleDOMNodeInserted((MutationEvent)evt);
  +        }
  +    }
  +
  +    /**
  +     * The listener class for 'DOMNodeRemoved' event.
  +     */
  +    protected class DOMNodeRemovedEventListener implements EventListener {
  +
  +        /**
  +         * Handles 'DOMNodeRemoved' events and deleguates to the
  +         * 'handleDOMNodeRemoved' method any changes to the
  +         * GraphicsNode if any.
  +         *
  +         * @param evt the DOM event
  +         */
  +        public void handleEvent(Event evt) {
  +            if (((MutationEvent)evt).getRelatedNode() != e) {
  +                return;
  +            }
  +            handleDOMNodeRemoved((MutationEvent)evt);
  +        }
       }
   }
  
  
  
  1.34      +2 -2      xml-batik/sources/org/apache/batik/gvt/AbstractGraphicsNode.java
  
  Index: AbstractGraphicsNode.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/gvt/AbstractGraphicsNode.java,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -r1.33 -r1.34
  --- AbstractGraphicsNode.java 31 Jan 2002 21:57:35 -0000      1.33
  +++ AbstractGraphicsNode.java 14 Feb 2002 12:37:54 -0000      1.34
  @@ -54,7 +54,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]";>Thierry Kormann</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Emmanuel Tissandier</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Thomas DeWeese</a>
  - * @version $Id: AbstractGraphicsNode.java,v 1.33 2002/01/31 21:57:35 deweese Exp $
  + * @version $Id: AbstractGraphicsNode.java,v 1.34 2002/02/14 12:37:54 tkormann Exp $
    */
   public abstract class AbstractGraphicsNode implements GraphicsNode {
   
  @@ -879,7 +879,7 @@
       protected void invalidateGeometryCache() {
           // If our bounds are invalid then our parents bounds
           // must be invalid also. So just return.
  -        if (bounds == null) return;
  +        //if (bounds == null) return;
   
           if (parent != null) {
               ((AbstractGraphicsNode) parent).invalidateGeometryCache();
  
  
  
  1.8       +4 -2      xml-batik/sources/org/apache/batik/gvt/UpdateTracker.java
  
  Index: UpdateTracker.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/gvt/UpdateTracker.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- UpdateTracker.java        12 Feb 2002 18:58:31 -0000      1.7
  +++ UpdateTracker.java        14 Feb 2002 12:37:55 -0000      1.8
  @@ -29,7 +29,7 @@
    * This class tracks the changes on a GVT tree
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Thomas DeWeese</a>
  - * @version $Id: UpdateTracker.java,v 1.7 2002/02/12 18:58:31 deweese Exp $
  + * @version $Id: UpdateTracker.java,v 1.8 2002/02/14 12:37:55 tkormann Exp $
    */
   public class UpdateTracker extends GraphicsNodeChangeAdapter {
   
  @@ -124,7 +124,9 @@
                   //      org.ImageDisplay.stringShape(oRgn) + "\n" +
                   //      org.ImageDisplay.stringShape(nRgn) + "\n");
                   ret.add(oRgn);
  -                ret.add(nRgn);
  +                if (nRgn != null) {
  +                    ret.add(nRgn);
  +                }
               }
           }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to