I am creating a simple svg application using JSVGCanvas. I am trying to add mouse event listeners to the svg elements (rect, group etc.). I follow the standard routine, using EventTarget.addEventListener(). My listeners are able to receive mouse events correctly, but the MouseEvent.getButton() does not return the correct value (which should be 0 for left, 1 for middle, 2 for right i think). To be more specific, no matter which button i pressed, i always get 1. I try to look in depth and i think there might be some mouse or key mask problems.

The following is the program that can reproduce the problem. You can do a quick test by copy and paste, and compile it with batik 1.7 and its required jar files. And try it again with the batik version 1.6. From what i got batik 1.6 will generate the right mouse button, but in 1.7, MouseEvent.getButton() always return 1.

//BEGINNING OF CODE

/*
* Copyright (c) 2005 - 2007 PeterHi
* All rights reserved.
*
* Copyright (c) 2005 - 2007 by Hai Yun Tao
* All rights reserved.
*
*/

package com.peterhi.err;

import java.awt.BorderLayout;
import java.awt.Dimension;
import org.apache.batik.dom.svg12.SVG12DOMImplementation;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.gvt.GVTTreeRendererListener;
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.events.MouseEvent;
import org.w3c.dom.svg.SVGDocument;

public class Main extends javax.swing.JFrame
                 implements GVTTreeRendererListener,
                            EventListener {

   // the canvas
   private JSVGCanvas canvas;
   // the rect you can click, used to test mouse events
   private Element rect;

   public Main() {
       initComponents();
       initComponentsEx();
   }

   private void initComponentsEx() {
       // Set up canvas
       canvas = new JSVGCanvas();
       canvas.setPreferredSize(new Dimension(640, 480));
       canvas.addGVTTreeRendererListener(this);
       add(canvas, BorderLayout.CENTER);

       try {
           // Create a document
           String ns = SVG12DOMImplementation.SVG_NAMESPACE_URI;
           SVG12DOMImplementation impl = (SVG12DOMImplementation)
               SVG12DOMImplementation.getDOMImplementation();
           SVGDocument doc = (SVGDocument )
               impl.createDocument(ns, "svg", null);
           // Create a rectangle to reproduce the error
           rect = doc.createElementNS(ns, "rect");
           rect.setAttributeNS(null, "x", "0");
           rect.setAttributeNS(null, "y", "0");
           rect.setAttributeNS(null, "width", "1000");
           rect.setAttributeNS(null, "height", "1000");
           rect.setAttributeNS(null, "style", "fill:red");
           // put it into document
           doc.getRootElement().appendChild(rect);

           // set document to the canvas
           canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
           canvas.setDocument(doc); // I think gvt renderer
                                    // will start working
       } catch (Exception ex) {
           ex.printStackTrace();
       }

       pack();
   }

   // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
   private void initComponents() {

       setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
   }// </editor-fold>

   public static void main(String[] args) {
       java.awt.EventQueue.invokeLater(new Runnable() {
           public void run() {
               new Main().setVisible(true);
           }
       });
   }

   // Variables declaration - do not modify
   // End of variables declaration

   public void gvtRenderingPrepare(GVTTreeRendererEvent e) { }

   public void gvtRenderingStarted(GVTTreeRendererEvent e) { }

   public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
       System.out.println("render completed, adding event listeners");
       // I appended my listener here, which works perfectly
       // back in batik 1.6
       EventTarget et = (EventTarget )rect;
       // subscribe listener here, take mouse down as
       // an example
       et.addEventListener("mousedown", this, false);
   }

   public void gvtRenderingCancelled(GVTTreeRendererEvent e) { }

   public void gvtRenderingFailed(GVTTreeRendererEvent e) { }

   public void handleEvent(Event evt) {
       // Prove that we are able to received mouse down event
       System.out.println("mouse down trigerred!!!");

       // Here is where the problem comes
       // Check the value of MouseEvent.getButton(), which,
       // no matter what button is pressed, will always show 1

       MouseEvent me = (MouseEvent )evt;
       short button = me.getButton();
       System.out.println("button pressed: " + button);
   }

}

// END OF CODE

FYI:
Batik Version: Batik 1.7 Beta 1 (This problem does not occur in Batik 1.6)
Program IDE: NetBeans IDE 5.5
OS: Windows Server 2003
JDK version: Java SE Version 1.5.0 (build 1.5.0_11-b03)
Mouse: standard mouse with left, right, and a scrollable middle button

Please help me solve the problem as soon as possible. Thanks in advance.

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to