xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sugaw...@humane-systems.co.jp (sugawara)
Subject RE: modifying css in live svg document
Date Wed, 10 May 2006 08:37:23 GMT
Hi Roger,

I also made a minimal sample.
It does not use any mouse listeners but does a key listener instead.
You can see the color of the <rect> element is toggled between
cyan and red by pressing any key.

Hope this saves your time to make your own example.

Good luck.

--------------------------------------------
package RogersIssue;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.StringReader;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.svg.SVGOMStyleElement;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.gvt.GVTTreeRendererListener;
import org.apache.batik.util.RunnableQueue;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.svg.SVGDocument;
import org.w3c.dom.svg.SVGSVGElement;

public class CSSTest extends JFrame {
  Canvas canvas;
  SVGDocument document;
  RunnableQueue runQ = null;
  String svg	=
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  + "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"
\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
  + "<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\"
version=\"1.1\">\n"
  + "\t<style type=\"text/css\">"
  + "\t<![CDATA[.rect{fill:cyan}}]]>"
  + "\t</style>"
  + "\t<rect id=\"rect\" x=\"50\" y=\"50\" width=\"100\" height=\"100\"
class=\"rect\"/>\n"
  + "</svg>\n";

  public CSSTest() {
    super("");
    canvas = new Canvas();
    setContentPane(canvas);

    WindowAdapter windowAdapter = new WindowAdapter() {
      public void windowOpened(WindowEvent we) {
        SVGDocument doc = null;
        try {
          String parser = XMLResourceDescriptor.getXMLParserClassName();
          SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
          StringReader reader = new StringReader(svg);
          doc = f.createSVGDocument(null, reader);
        } catch (Exception ex) {}
        canvas.setSVGDocument(doc);
      }
    };
    addWindowListener(windowAdapter);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(200,200);
    setVisible(true);
  }

  public class Canvas extends JSVGCanvas implements GVTTreeRendererListener
{
    public Canvas() {
      canvas = this;
      setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
      addGVTTreeRendererListener(this);

      KeyAdapter keyAdapter = new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
          if (runQ==null) return;
          runQ.invokeLater(new Runnable(){public void run(){
            changeColor();
          }});
        }
      };
      addKeyListener(keyAdapter);
   }

    public void gvtRenderingPrepare(GVTTreeRendererEvent e) {}
    public void gvtRenderingStarted(GVTTreeRendererEvent e) {}
    public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
      document = canvas.getSVGDocument();
      runQ = canvas.getUpdateManager().getUpdateRunnableQueue();
    }
    public void gvtRenderingCancelled(GVTTreeRendererEvent e) {}
    public void gvtRenderingFailed	(GVTTreeRendererEvent e) {}
  }
  private void changeColor() {
    SVGSVGElement root = document.getRootElement();
    SVGOMStyleElement style =
(SVGOMStyleElement)getFirstChildByNodeName(root,"style");
    for (Node node=style.getFirstChild(); node!=null;
node=node.getNextSibling()) {
      if (node.getNodeType()==Node.CDATA_SECTION_NODE) {
        CDATASection cdata = (CDATASection)node;
        String value = cdata.getNodeValue();
        int indx = value.indexOf(".rect");
        int indx1 = value.indexOf("fill:", indx) + 5;
        int indx2 = value.indexOf('}', indx1);
        String currColor = value.substring(indx1, indx2);
        String newColor = currColor.equals("cyan") ? "red" : "cyan";
        cdata.replaceData(indx1, (indx2-indx1), newColor);
        return;
      }
    }
  }
  public static Element getFirstChildByNodeName(Element parent, String
nodeName) {
    for (Node node=parent.getFirstChild(); node!=null;
node=node.getNextSibling()) {
      if (node.getNodeType()==Node.ELEMENT_NODE &&
nodeName.equals(node.getNodeName()))
        return (Element)node;
    }
    return null;
  }
  public static void main( String[] args ) {
    SwingUtilities.invokeLater( new Runnable() { public void run() {
      new CSSTest();
    }});
}

}



---------------------------------------------------------------------
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