xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Age Bosma <agebo...@gmail.com>
Subject Unable to remove event listener
Date Mon, 09 Mar 2009 10:28:45 GMT

I'm trying to remove an event listener but for some reason this doesn't work.

I'm creating a new element to add to the JSVGCanvas. To add an event
listener I use:

((EventTarget)device).addEventListener("mousedown", new DeviceHandler(), false);

This works fine. The event gets triggered as it should. The event is
used to drag an element. When it gets triggered I'm adding two new
events to the same element. These get triggered nicely as well but I
can't get them removed anyone.
The code:

import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.events.MouseEvent;
import org.w3c.dom.svg.SVGElement;
import org.w3c.dom.svg.SVGLocatable;
import org.w3c.dom.svg.SVGMatrix;
import org.w3c.dom.svg.SVGPoint;

public class DeviceHandler implements EventListener {
  DeviceHandler() {
    DeviceHandler(float startX, float startY) {
      this.startX = startX;
      this.startY = startY;

  public void handleEvent(Event evt) {
      String eventType = evt.getType();
      System.out.println("Event type: " + eventType);
      targetElement = (SVGElement) evt.getTarget();
      System.out.println("Target: " + targetElement.getTagName());
      event = (MouseEvent) evt;

      SVGMatrix mat =
      SVGMatrix imat = mat.inverse();
      position = targetElement.getOwnerSVGElement().createSVGPoint();

      position.setX((float) event.getClientX());
      position.setY((float) event.getClientY());

      position = position.matrixTransform(imat);

      if (eventType.equals("mousedown")) {
          startX = position.getX();
          startY = position.getY();

new DeviceHandler(startX, startY), false);
          ((EventTarget)targetElement).addEventListener("mouseup", new
DeviceHandler(), false);
      else if (eventType.equals("mousemove")) {
          double x = position.getX() - startX;
          double y = position.getY() - startY;

          System.out.println("Translate: x=" + x + ", y=" + y);

          targetElement.setAttributeNS(null, "transform", "translate("
+ x + "," + y + ")");
          targetElement.setAttributeNS(null, "stroke", "rgb(255,0,0)");
      else if (eventType.equals("mouseup")) {
new DeviceHandler(), false);
new DeviceHandler(), false);

  private SVGElement targetElement = null;
  private MouseEvent event;
  private float startX = 0;
  private float startY = 0;
  private SVGPoint position;

Intended bahaviour: mousedown+ mousemove = drag. It should remove the
'mousemove' and 'mouseup' again when the button is let go.
I tried replacing 'new DeviceHandler()' with 'this' when removing the
events but 'mousemove' nor 'mouseup' gets removed, they remain getting
triggered. As a result you can't stop dragging the element.
As I test I even added removeEventListener right after a
addEventListener but the event does not get removed, it keeps getting

I'm I doing something completely wrong here?



To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org

View raw message