Thomas,
You are correct. The test case for this no longer fails as I previously
encountered. My apologies for any confusion caused.
Instead of trashing the example, here it is (excuse the ide-component
code...I did not have alot of time for further reduction). Maybe it will
serve as an event example or a framework for other test case reductions.
Are examples like this of use to anyone on the list currently?
>>>>>>>>>>>> Begin Example
package textboundingbox;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.svg.SVGDocument;
import org.w3c.dom.svg.SVGLocatable;
import org.w3c.dom.svg.SVGRect;
/**
*
* @author Jonathan Wood
*/
public class Main extends javax.swing.JFrame {
private org.apache.batik.swing.JSVGCanvas svgCanvas;
private int clickCount = 0;
private void initGlassPane() {
svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
@Override
public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
svgCanvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(new
Runnable() {
public void run() {
final SVGDocument doc = svgCanvas.getSVGDocument();
Element glassPane =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "rect");
glassPane.setAttributeNS(null, "id", "glass-pane");
glassPane.setAttributeNS(null, "x", "0");
glassPane.setAttributeNS(null, "y", "0");
glassPane.setAttributeNS(null, "height", "" +
getHeight());
glassPane.setAttributeNS(null, "width", "" +
getWidth());
glassPane.setAttributeNS(null, "fill", "none");
glassPane.setAttributeNS(null, "pointer-events",
"all");
EventListener glassPaneListener = new
EventListener() {
public void handleEvent(Event event) {
String clickString = "Clicks = " +
++clickCount;
Element textNode =
doc.getElementById("my-text-node");
if(textNode == null) {
textNode =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "text");
textNode.setAttributeNS(null, "id",
"my-text-node");
textNode.setAttributeNS(null, "x",
"50");
textNode.setAttributeNS(null, "y",
"50");
textNode.setAttributeNS(null,
"font-size", "20");
Text t =
doc.createTextNode(clickString);
textNode.appendChild(t);
doc.getDocumentElement().appendChild(textNode);
} else {
textNode.getFirstChild().setNodeValue(clickString);
}
SVGRect bbox =
((SVGLocatable)textNode).getBBox();
Element rect =
doc.getElementById("my-bg-rect");
if (rect == null) {
rect =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "rect");
rect.setAttributeNS(null, "id",
"my-bg-rect");
rect.setAttributeNS(null, "fill",
"#CCDDFF");
rect.setAttributeNS(null, "rx", "10");
rect.setAttributeNS(null, "ry", "10");
rect.setAttributeNS(null, "stroke",
"#551111");
rect.setAttributeNS(null,
"stroke-width", "2");
}
rect.setAttributeNS(null, "x", "" +
(bbox.getX() - 10) );
rect.setAttributeNS(null, "y", "" +
(bbox.getY() - 10) );
rect.setAttributeNS(null, "width", "" +
(bbox.getWidth() + 20) );
rect.setAttributeNS(null, "height", "" +
(bbox.getHeight() + 20) );
doc.getDocumentElement().insertBefore(rect,
textNode);
}
};
((EventTarget)glassPane).addEventListener("click",
glassPaneListener, false);
doc.getDocumentElement().appendChild(glassPane);
}
});
}
});
SVGDocument startDoc = (SVGDocument)
SVGDOMImplementation.getDOMImplementation().createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI,
"svg", null);
DOMImplementation impl =
SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
Document doc = impl.createDocument(svgNS, "svg", null);
svgCanvas.setSVGDocument((SVGDocument) doc);
}
public Main() {
initComponents();
initGlassPane();
}
@SuppressWarnings("unchecked")
private void initComponents() {
svgCanvas = new org.apache.batik.swing.JSVGCanvas();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
org.jdesktop.layout.GroupLayout svgCanvasLayout = new
org.jdesktop.layout.GroupLayout(svgCanvas);
svgCanvas.setLayout(svgCanvasLayout);
svgCanvasLayout.setHorizontalGroup(
svgCanvasLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 400, Short.MAX_VALUE)
);
svgCanvasLayout.setVerticalGroup(
svgCanvasLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 300, Short.MAX_VALUE)
);
org.jdesktop.layout.GroupLayout layout = new
org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(svgCanvas, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(svgCanvas, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
300, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
}
<<<<<<<<<<<< End Example
On Mon, Dec 14, 2009 at 9:53 AM, jonathan wood
<[email protected]>wrote:
> Hi Thomas,
>
> I'll try to pull a current SVN copy this week and test it.
>
> On Mon, Dec 14, 2009 at 9:02 AM, <[email protected]> wrote:
>
>> Hi Jonathan,
>>
>> jonathan wood <[email protected]> wrote on 12/14/2009 12:48:49
>> AM:
>>
>>
>> > I have found that when dealing with text and bounding boxes you
>> > must be careful to never try to obtain the newly "added to the DOM"
>> > text's bounding box in the same worker.
>>
>> Really? I know some of the other SVG implementations
>> have this sort of a requirement, but I am not aware of any situation
>> where this is true in Batik. Do you have a concrete example?
>>
>>
>