package gps.svg.test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import org.apache.batik.bridge.UpdateManager;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.gvt.GVTTreeRendererListener;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.svg.SVGDocument;
public class Test2 {
static UpdateManager um;
public static void main(String[] args) {
// Para leer un archivo svg y meterlo en un DOM
/*try {
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
String uri = "http://www.example.org/diagram.svg";
Document doc = f.createDocument(uri);
} catch (IOException ex) {
// ...
}*/
//rectangle.setAttributeNS(null, "style", "fill:white; stroke:black");
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
final String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
final Document doc = impl.createDocument(svgNS, "svg", null);
final Element svgRoot = doc.getDocumentElement();
svgRoot.setAttributeNS(null, "width", "500");
svgRoot.setAttributeNS(null, "height", "400");
Element rectangle = doc.createElementNS(svgNS, "rect");
rectangle.setAttributeNS(null, "x", "100");
rectangle.setAttributeNS(null, "y", "120");
rectangle.setAttributeNS(null, "width", "100");
rectangle.setAttributeNS(null, "height", "200");
rectangle.setAttributeNS(null, "fill", "red");
rectangle.setAttributeNS(null, "id", "miId");
svgRoot.appendChild(rectangle);
final JSVGCanvas canvas = new JSVGCanvas();
//UpdateManager um;
canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
canvas.setEnablePanInteractor(false);
canvas.addGVTTreeRendererListener(new GVTTreeRendererListener() {
public void gvtRenderingPrepare(GVTTreeRendererEvent gVTTreeRendererEvent) {
}
public void gvtRenderingStarted(GVTTreeRendererEvent gVTTreeRendererEvent) {
}
public void gvtRenderingCompleted(GVTTreeRendererEvent gVTTreeRendererEvent) {
um = canvas.getUpdateManager();
}
public void gvtRenderingCancelled(GVTTreeRendererEvent gVTTreeRendererEvent) {
}
public void gvtRenderingFailed(GVTTreeRendererEvent gVTTreeRendererEvent) {
}
});
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.add(canvas, BorderLayout.CENTER);
f.add(new JButton(new Action() {
public Object getValue(String key) {
return null;
}
public void putValue(String key, Object value) {
}
public void setEnabled(boolean b) {
}
public boolean isEnabled() {
return true;
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
}
public void actionPerformed(ActionEvent e) {
System.out.println("agregar rectangulo");
um.getUpdateRunnableQueue().invokeLater(new Runnable() {
public void run() {
Element rectangle = doc.createElementNS(svgNS, "rect");
rectangle.setAttributeNS(null, "x", "10");
rectangle.setAttributeNS(null, "y", "12");
rectangle.setAttributeNS(null, "width", "10");
rectangle.setAttributeNS(null, "height", "20");
rectangle.setAttributeNS(null, "fill", "green");
svgRoot.appendChild(rectangle);
rectangle = doc.createElementNS(svgNS, "rect");
rectangle.setAttributeNS(null, "x", "12");
rectangle.setAttributeNS(null, "y", "16");
rectangle.setAttributeNS(null, "width", "6");
rectangle.setAttributeNS(null, "height", "12");
rectangle.setAttributeNS(null, "fill", "white");
rectangle.setAttributeNS(null, "cursor", "pointer");
EventTarget et = (EventTarget) rectangle;
et.addEventListener("mouseover", new EventListener() {
public void handleEvent(Event evt) {
System.out.println("event " + evt.getType());
System.out.println("target " + evt.getTarget());
System.out.println("x " + ((Element) evt.getTarget()).getAttributeNodeNS(null, "x").getValue());
System.out.println("y " + ((Element) evt.getTarget()).getAttributeNodeNS(null, "y").getValue());
}
}, false);
et.addEventListener("mouseout", new EventListener() {
public void handleEvent(Event evt) {
System.out.println("event " + evt.getType());
System.out.println("target " + evt.getTarget());
System.out.println("x " + ((Element) evt.getTarget()).getAttributeNodeNS(null, "x").getValue());
System.out.println("y " + ((Element) evt.getTarget()).getAttributeNodeNS(null, "y").getValue());
}
}, false);
svgRoot.appendChild(rectangle);
doc.getElementById("miId").getAttributeNodeNS(null, "x").setValue("30");
}
});
}}), BorderLayout.SOUTH);
canvas.setSVGDocument((SVGDocument) doc);
f.pack();
f.setVisible(true);
}
}