import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.svg.SVGLoadEventDispatcherAdapter;
import org.apache.batik.swing.svg.SVGLoadEventDispatcherEvent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class ModifyingExample {

    static class RotationTimerTask extends TimerTask {

        JSVGCanvas canvas;

        int angle;

        public RotationTimerTask(JSVGCanvas canvas) {
            this.canvas = canvas;
        }

        public void run() {
            // Increment the angle
            angle += 10;

            // Find the <g> element to rotate
            Document doc = canvas.getSVGDocument();
            final Element g = doc.getElementById("rotationGroup");

            // Schedule the rotation in the update manager's thread
            Runnable rotationUpdater = new Runnable() {
                public void run() {
                    g.setAttributeNS(null, "transform", "rotate(" + angle + ")");
                }
            };
            canvas.getUpdateManager().getUpdateRunnableQueue().invokeLater
                (rotationUpdater);
        }
    }

    public static void main(String[] args) {
        // A canvas to show "text.svg"
        JSVGCanvas c = new JSVGCanvas();
        c.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);

        // Timer task that will do the rotation
        final RotationTimerTask tt = new RotationTimerTask(c);

        // Start the timer when the document is ready
        c.addSVGLoadEventDispatcherListener(new SVGLoadEventDispatcherAdapter() {
            public void svgLoadEventDispatchCompleted(SVGLoadEventDispatcherEvent e) {
                new Timer().scheduleAtFixedRate(tt, 500, 500);
            }
        });

        // Load the document
        c.setURI(new File("text.svg").toURI().toString());

        // A frame for the canvas to live in
        JFrame f = new JFrame("Modifying example");
        f.setSize(400, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Add the canvas to the frame
        f.getContentPane().add(c);

        // Show the frame
        f.setVisible(true);
    }
}

