Revision: 1175
Author:   heuermh
Date:     Fri Aug 31 13:11:33 2012
Log: Issue 207 ; ignore mouse pressed and released events with button MouseEvent.NOBUTTON instead of throwing an exception
http://code.google.com/p/piccolo2d/source/detail?r=1175

Modified:
 /piccolo2d.java/trunk/core/src/main/java/org/piccolo2d/PCanvas.java
 /piccolo2d.java/trunk/core/src/test/java/org/piccolo2d/PCanvasTest.java

=======================================
--- /piccolo2d.java/trunk/core/src/main/java/org/piccolo2d/PCanvas.java Sun Nov 13 11:11:49 2011 +++ /piccolo2d.java/trunk/core/src/main/java/org/piccolo2d/PCanvas.java Fri Aug 31 13:11:33 2012
@@ -757,6 +757,7 @@
             requestFocus();

             boolean shouldBalanceEvent = false;
+            boolean shouldSendEvent = false;

final MouseEvent event = copyButtonsFromModifiers(rawEvent, MouseEvent.MOUSE_PRESSED);

@@ -766,6 +767,7 @@
                         shouldBalanceEvent = true;
                     }
                     isButton1Pressed = true;
+                    shouldSendEvent = true;
                     break;

                 case MouseEvent.BUTTON2:
@@ -773,6 +775,7 @@
                         shouldBalanceEvent = true;
                     }
                     isButton2Pressed = true;
+                    shouldSendEvent = true;
                     break;

                 case MouseEvent.BUTTON3:
@@ -780,22 +783,26 @@
                         shouldBalanceEvent = true;
                     }
                     isButton3Pressed = true;
+                    shouldSendEvent = true;
                     break;
-                default:
- throw new RuntimeException("mousePressed without buttons specified");

+                default:
+                    break;
             }

             if (shouldBalanceEvent) {
sendRetypedMouseEventToInputManager(event, MouseEvent.MOUSE_RELEASED);
             }

-            sendInputEventToInputManager(event, MouseEvent.MOUSE_PRESSED);
+            if (shouldSendEvent) {
+ sendInputEventToInputManager(event, MouseEvent.MOUSE_PRESSED);
+            }
         }

         /** {@inheritDoc} */
         public void mouseReleased(final MouseEvent rawEvent) {
             boolean shouldBalanceEvent = false;
+            boolean shouldSendEvent = false;

final MouseEvent event = copyButtonsFromModifiers(rawEvent, MouseEvent.MOUSE_RELEASED);

@@ -805,6 +812,7 @@
                         shouldBalanceEvent = true;
                     }
                     isButton1Pressed = false;
+                    shouldSendEvent = true;
                     break;

                 case MouseEvent.BUTTON2:
@@ -812,6 +820,7 @@
                         shouldBalanceEvent = true;
                     }
                     isButton2Pressed = false;
+                    shouldSendEvent = true;
                     break;

                 case MouseEvent.BUTTON3:
@@ -819,16 +828,21 @@
                         shouldBalanceEvent = true;
                     }
                     isButton3Pressed = false;
+                    shouldSendEvent = true;
                     break;
+
                 default:
- throw new RuntimeException("mouseReleased without buttons specified");
+                    shouldBalanceEvent = false;
+                    shouldSendEvent = false;
             }

             if (shouldBalanceEvent) {
sendRetypedMouseEventToInputManager(event, MouseEvent.MOUSE_PRESSED);
             }

-            sendInputEventToInputManager(event, MouseEvent.MOUSE_RELEASED);
+            if (shouldSendEvent) {
+ sendInputEventToInputManager(event, MouseEvent.MOUSE_RELEASED);
+            }
         }

private MouseEvent copyButtonsFromModifiers(final MouseEvent rawEvent, final int eventType) {
=======================================
--- /piccolo2d.java/trunk/core/src/test/java/org/piccolo2d/PCanvasTest.java Sun Nov 13 11:11:49 2011 +++ /piccolo2d.java/trunk/core/src/test/java/org/piccolo2d/PCanvasTest.java Fri Aug 31 13:11:33 2012
@@ -30,6 +30,11 @@

 import java.awt.Cursor;

+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+import javax.swing.JPanel;
+
 import org.piccolo2d.event.PInputEventListener;
 import org.piccolo2d.util.PBounds;
 import org.piccolo2d.util.PPaintContext;
@@ -153,4 +158,109 @@
         assertNotNull(listeners);
         assertEquals(2, listeners.length); // zoom + pan + mockListener
     }
+
+    public void testMouseNoButtonPressed() {
+        JPanel panel = new JPanel();
+        panel.add("Center", canvas);
+        canvas.addInputEventListener(mockListener);
+ MouseEvent mouseEvent = new MouseEvent(canvas, 0, System.currentTimeMillis(), 0, 10, 10, 1, false, MouseEvent.NOBUTTON); + for (int i = 0, size = canvas.getMouseListeners().length; i < size; i++) {
+            MouseListener mouseListener = canvas.getMouseListeners()[i];
+            mouseListener.mousePressed(mouseEvent);
+        }
+        assertEquals(0, mockListener.getNotificationCount());
+    }
+
+    public void testMouseButton1Pressed() {
+        JPanel panel = new JPanel();
+        panel.add("Center", canvas);
+        canvas.addInputEventListener(mockListener);
+ MouseEvent mouseEvent = new MouseEvent(canvas, 0, System.currentTimeMillis(), 0, 10, 10, 1, false, MouseEvent.BUTTON1); + for (int i = 0, size = canvas.getMouseListeners().length; i < size; i++) {
+            MouseListener mouseListener = canvas.getMouseListeners()[i];
+            mouseListener.mousePressed(mouseEvent);
+        }
+        assertEquals(1, mockListener.getNotificationCount());
+    }
+
+    public void testMouseButton2Pressed() {
+        JPanel panel = new JPanel();
+        panel.add("Center", canvas);
+        canvas.addInputEventListener(mockListener);
+ MouseEvent mouseEvent = new MouseEvent(canvas, 0, System.currentTimeMillis(), 0, 10, 10, 1, false, MouseEvent.BUTTON2); + for (int i = 0, size = canvas.getMouseListeners().length; i < size; i++) {
+            MouseListener mouseListener = canvas.getMouseListeners()[i];
+            mouseListener.mousePressed(mouseEvent);
+        }
+        assertEquals(1, mockListener.getNotificationCount());
+    }
+
+    public void testMouseButton3Pressed() {
+        JPanel panel = new JPanel();
+        panel.add("Center", canvas);
+        canvas.addInputEventListener(mockListener);
+ MouseEvent mouseEvent = new MouseEvent(canvas, 0, System.currentTimeMillis(), 0, 10, 10, 1, false, MouseEvent.BUTTON3); + for (int i = 0, size = canvas.getMouseListeners().length; i < size; i++) {
+            MouseListener mouseListener = canvas.getMouseListeners()[i];
+            mouseListener.mousePressed(mouseEvent);
+        }
+        assertEquals(1, mockListener.getNotificationCount());
+    }
+
+    public void testMouseNoButtonReleased() {
+        JPanel panel = new JPanel();
+        panel.add("Center", canvas);
+        canvas.addInputEventListener(mockListener);
+ MouseEvent mouseEvent = new MouseEvent(canvas, 0, System.currentTimeMillis(), 0, 10, 10, 1, false, MouseEvent.NOBUTTON); + for (int i = 0, size = canvas.getMouseListeners().length; i < size; i++) {
+            MouseListener mouseListener = canvas.getMouseListeners()[i];
+            mouseListener.mouseReleased(mouseEvent);
+        }
+        assertEquals(0, mockListener.getNotificationCount());
+    }
+
+    public void testMouseButton1Released() {
+        JPanel panel = new JPanel();
+        panel.add("Center", canvas);
+        canvas.addInputEventListener(mockListener);
+ MouseEvent mouseEvent = new MouseEvent(canvas, 0, System.currentTimeMillis(), 0, 10, 10, 1, false, MouseEvent.BUTTON1); + for (int i = 0, size = canvas.getMouseListeners().length; i < size; i++) {
+            MouseListener mouseListener = canvas.getMouseListeners()[i];
+            mouseListener.mouseReleased(mouseEvent);
+        }
+        assertEquals(3, mockListener.getNotificationCount());
+        for (int i = 0; i < 3; i++) {
+ assertEquals(MouseEvent.BUTTON1, mockListener.getNotification(i).event.getButton());
+        }
+    }
+
+    public void testMouseButton2Released() {
+        JPanel panel = new JPanel();
+        panel.add("Center", canvas);
+        canvas.addInputEventListener(mockListener);
+ MouseEvent mouseEvent = new MouseEvent(canvas, 0, System.currentTimeMillis(), 0, 10, 10, 1, false, MouseEvent.BUTTON2); + for (int i = 0, size = canvas.getMouseListeners().length; i < size; i++) {
+            MouseListener mouseListener = canvas.getMouseListeners()[i];
+            mouseListener.mouseReleased(mouseEvent);
+        }
+        assertEquals(3, mockListener.getNotificationCount());
+        for (int i = 0; i < 3; i++) {
+ assertEquals(MouseEvent.BUTTON2, mockListener.getNotification(i).event.getButton());
+        }
+    }
+
+    public void testMouseButton3Released() {
+        JPanel panel = new JPanel();
+        panel.add("Center", canvas);
+        canvas.addInputEventListener(mockListener);
+ MouseEvent mouseEvent = new MouseEvent(canvas, 0, System.currentTimeMillis(), 0, 10, 10, 1, false, MouseEvent.BUTTON3); + for (int i = 0, size = canvas.getMouseListeners().length; i < size; i++) {
+            MouseListener mouseListener = canvas.getMouseListeners()[i];
+            mouseListener.mouseReleased(mouseEvent);
+        }
+        assertEquals(3, mockListener.getNotificationCount());
+        for (int i = 0; i < 3; i++) {
+ assertEquals(MouseEvent.BUTTON3, mockListener.getNotification(i).event.getButton());
+        }
+    }
 }

--
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en

Reply via email to