Drag & Drop with batik
Hi !
I have a java application which display a svg file. Iid like to drag & drop elements such as circles in the svg, and want to do it using batik.
Here is the code I use, but there is a problem, if I move the mouse too fast, the circle doesn't follow the cursor but is still in a "draggable state", and I have to put back the cursor on the circle and click to set it in a "non draggable" state (It's logical because : mousedown --> draggable mouseup -->non draggable mousemove -->update coordinates). (I hope i'm clear, I'm french)
So do you have code samples or a solution to this problem ?
Here is the code :
Element gBornes = svgDoc.getElementById("bornes");
((EventTarget)gBornes).addEventListener("mouseover", onAction.getOverAction(), false);
((EventTarget)gBornes).addEventListener("mouseout", onAction.getOutAction(), false);
((EventTarget)gBornes).addEventListener("click", onAction.getClickAction(), false);
((EventTarget)gBornes).addEventListener("mousedown", onAction.getDownAction(), false);
((EventTarget)gBornes).addEventListener("mousemove", onAction.getMoveAction(), false);
((EventTarget)gBornes).addEventListener("mouseup", onAction.getUpAction(), false);
private class OnDownAction implements EventListener {
public void handleEvent(Event evt) {
System.out.println("Down");
//perform select, deselect operations, etc...
// i.e. assign or validate that you have a draggable item
// in the "selectedItem" object
selectedItem = (Element)evt.getTarget();
player.setEnablePanMode(false);
System.out.println(selectedItem.getAttribute("id"));
drag = DRAG_DOWN;
// Set the initial drag point
DOMMouseEvent elEvt = (DOMMouseEvent)evt;
int nowToX = elEvt.getClientX();
int nowToY = elEvt.getClientY();
// Convert Screen coordinates to Document Coordinates.
SVGOMPoint pt = new SVGOMPoint(nowToX, nowToY);
SVGMatrix mat = ((SVGLocatable)evt.getTarget()).getScreenCTM(); // elem -> screen
mat = mat.inverse(); // screen -> elem
initialDragPoint = (SVGOMPoint)pt.matrixTransform(mat);
}
}
private class OnUpAction implements EventListener {
public void handleEvent(Event evt) {
System.out.println("Up");
player.setEnablePanMode(true);
selectedItem = null;
drag = DRAG_UP;
}
}
private class OnMoveAction implements EventListener {
public void handleEvent(Event evt) {
if (drag == DRAG_DOWN && selectedItem!=null) {
//Cast the event onto a Batik Mouse event,
//so we can get ccordinates
DOMMouseEvent elEvt = (DOMMouseEvent)evt;
int nowToX = elEvt.getClientX();
int nowToY = elEvt.getClientY();
//convert it to a point for use with the Matrix
SVGOMPoint pt = new SVGOMPoint(nowToX, nowToY);
//Get the items screen coordinates, and apply the transformation
// elem -> screen
SVGMatrix mat = ((SVGLocatable)evt.getTarget()).getScreenCTM();
mat = mat.inverse(); // screen -> elem
SVGOMPoint dragpt = (SVGOMPoint)pt.matrixTransform(mat);
selectedItem.setAttribute("cx",new Double(dragpt.getX()).toString());
selectedItem.setAttribute("cy",new Double(dragpt.getY()).toString());
}
}
}