Thomas DeWeese wrote:
Hi Fernando,
I don't know if I've ever seen this happen before. What version of Batik are you using? Also you show the code for
I am currently using Batik 1.5.1; I donwloaded it in binary form from http://apache.fastorama.com/dist/xml/
I have built a reduced program, based in a Batik example, that reproduces the problematic behavior of our project.A reproduceable test case is of course the best way to identify the problem (if there is one).
The test itself is done through the actionPerformed method, called when pressing the test button. Test button should only be pressed once.
I don't know what I am doing wrong, but I think I am missing some basic step. Follows the broken code:
import java.awt.*; import java.awt.event.*; import javax.swing.*;
import org.apache.batik.bridge.BridgeContext; import org.apache.batik.bridge.DocumentLoader; import org.apache.batik.bridge.GVTBuilder; import org.apache.batik.bridge.UpdateManager; import org.apache.batik.bridge.UserAgent; import org.apache.batik.bridge.UserAgentAdapter; import org.apache.batik.dom.AbstractDocument; import org.apache.batik.dom.svg.*; import org.apache.batik.swing.JSVGCanvas; import org.apache.batik.swing.gvt.GVTTreeRendererAdapter; import org.apache.batik.swing.gvt.GVTTreeRendererEvent; import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter; import org.apache.batik.swing.svg.SVGDocumentLoaderEvent; import org.apache.batik.swing.svg.GVTTreeBuilderAdapter; import org.apache.batik.swing.svg.GVTTreeBuilderEvent; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Node; import org.w3c.dom.svg.SVGDocument; import org.w3c.dom.svg.SVGElement;
public class CanvasTest implements ActionListener{protected JFrame frame;
protected JButton button;
protected JLabel label;
protected JSVGCanvas svgCanvas;
protected UpdateManager um;
protected SVGDocument document;
public static void main(String[] args) {
JFrame f = new JFrame("Batik");
CanvasTest app = new CanvasTest(f);
f.getContentPane().add(app.createComponents());
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize(400, 400);
f.setVisible(true);
} public CanvasTest(JFrame f) {
frame = f;
button = new JButton("Test");
label = new JLabel();
svgCanvas = new JSVGCanvas();
} public JComponent createComponents() {
final JPanel panel = new JPanel(new BorderLayout()); JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(button);
p.add(label); panel.add("North", p);
panel.add("Center", svgCanvas); // Set the button action.
button.addActionListener(this);DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
document = (SVGDocument) impl.createDocument(svgNS, "svg", null);
document.getRootElement().setAttribute("width",new Integer(400).toString());
document.getRootElement().setAttribute("height",new Integer(400).toString());
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext ctx = new BridgeContext(userAgent, loader);
ctx.setDynamicState(BridgeContext.DYNAMIC);
GVTBuilder builder = new GVTBuilder();
builder.build(ctx, document);
document.normalize();
svgCanvas.addGVTTreeRendererListener(new AdaptateurArbreGVT(this));
svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
svgCanvas.setSVGDocument(document);
return panel;
}
public void actionPerformed(ActionEvent e) {
SVGElement rect1=new SVGOMRectElement(document.getPrefix(), (AbstractDocument)document);
rect1.setAttributeNS(null,"x","20");
rect1.setAttributeNS(null,"y","20");
rect1.setAttributeNS(null,"width","50");
rect1.setAttributeNS(null,"height","50");
rect1.setAttributeNS(null,"style", "fill:rgb(0,20,160); stroke:rgb(0,0,0); stroke-width:2.0; stroke-opacity:0.6; fill-opacity:0.4;");
um.getUpdateRunnableQueue().invokeLater(new ActionAddition(document.getRootElement(), rect1));
try { Thread.sleep(100); } catch(Exception ex) { }
SVGElement rect2=new SVGOMRectElement(document.getPrefix(), (AbstractDocument)document);
rect2.setAttributeNS(null,"x","45");
rect2.setAttributeNS(null,"y","45");
rect2.setAttributeNS(null,"width","50");
rect2.setAttributeNS(null,"height","50");
rect2.setAttributeNS(null,"style", "fill:rgb(125,80,0); stroke:rgb(0,0,0); stroke-width:2.0; stroke-opacity:0.6; fill-opacity:0.4;");
um.getUpdateRunnableQueue().invokeLater(new ActionAddition(document.getRootElement(), rect2));
try { Thread.sleep(100); } catch(Exception ex) { }
for(int i = 45; i <200; i++) {
um.getUpdateRunnableQueue().invokeLater(new ActionModification(rect1, "x", new Integer(i).toString()));
um.getUpdateRunnableQueue().invokeLater(new ActionModification(rect2, "y", new Integer(i+25).toString()));
}
// By some reason there are two copies left when moving the rectangles...
// Delete and add the same rectangle to the document:
um.getUpdateRunnableQueue().invokeLater(new ActionSuppression(rect1));
try { Thread.sleep(100); } catch(Exception ex) { }
um.getUpdateRunnableQueue().invokeLater(new ActionAddition(document.getRootElement(), rect1));
try { Thread.sleep(100); } catch(Exception ex) { }
// After this there is still a copy drawn in the canvas. We can see this if we move again both rectangles:
for(int i = 45; i <200; i++) {
um.getUpdateRunnableQueue().invokeLater(new ActionModification(rect1, "y", new Integer(i).toString()));
um.getUpdateRunnableQueue().invokeLater(new ActionModification(rect2, "x", new Integer(i+25).toString()));
}
// The rectangle that was removed and readded has left a copy drawn there...
}
public void setUpdateManager() {
um=svgCanvas.getUpdateManager();
}
protected class AdaptateurArbreGVT extends GVTTreeRendererAdapter {
CanvasTest test;
public AdaptateurArbreGVT(CanvasTest test) {
this.test=test;
}
public void gvtRenderingPrepare(GVTTreeRendererEvent e) { }
public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
test.setUpdateManager();
}
}
class ActionAddition implements Runnable{
private Node parent, element;
ActionAddition(Node parent, Node element) {
this.parent=parent;
this.element=element;
}
public void run() {
System.out.println("ADDITION "+element);
parent.appendChild(element);
}
}
class ActionModification implements Runnable {
private SVGElement element;
private String nom, valeur;
ActionModification(SVGElement element, String nom, String valeur) {
this.element=element;
this.nom=nom;
this.valeur=valeur;
}
public void run() {
if(valeur!=null && !valeur.equals("")) {
System.out.println("MODIFICATION "+element);
element.setAttributeNS(null, nom, valeur);
}
}
}
class ActionSuppression implements Runnable {
private Node element;
ActionSuppression(Node element) {
this.element=element;
}
public void run() {
System.out.println("SUPPRESSION "+element);
element.getParentNode().removeChild(element);
}
}
}
Thanks for all your time and attention, Fernando Mendez.
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
