Author: heuermh
Date: Tue Oct  7 13:33:48 2008
New Revision: 394

Modified:
    piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/PCanvas.java

Log:
Issue 17 ; refactoring PCanvas KeyListener to KeyEventPostProcessor

Modified:  
piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/PCanvas.java
==============================================================================
--- piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/PCanvas.java     
 
(original)
+++ piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/PCanvas.java     
 
Tue Oct  7 13:33:48 2008
@@ -33,16 +33,17 @@
  import java.awt.Cursor;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
+import java.awt.KeyEventPostProcessor;
  import java.awt.event.ActionListener;
  import java.awt.event.InputEvent;
  import java.awt.event.KeyEvent;
-import java.awt.event.KeyListener;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.awt.event.MouseMotionListener;
  import java.awt.event.MouseWheelEvent;
  import java.awt.event.MouseWheelListener;

+import javax.swing.FocusManager;
  import javax.swing.JComponent;
  import javax.swing.RepaintManager;
  import javax.swing.Timer;
@@ -83,7 +84,7 @@
      private boolean paintingImmediately;
      private boolean animatingOnLastPaint;
      private MouseListener mouseListener;
-    private KeyListener keyListener;
+    private KeyEventPostProcessor keyEventPostProcessor;
      private MouseWheelListener mouseWheelListener;
      private MouseMotionListener mouseMotionListener;

@@ -542,21 +543,22 @@
              addMouseWheelListener(mouseWheelListener);
          }

-        if (keyListener == null) {
-            keyListener = new KeyListener() {
-                public void keyPressed(KeyEvent e) {
-                    sendInputEventToInputManager(e, KeyEvent.KEY_PRESSED);
-                }
-
-                public void keyReleased(KeyEvent e) {
-                    sendInputEventToInputManager(e, KeyEvent.KEY_RELEASED);
-                }
-
-                public void keyTyped(KeyEvent e) {
-                    sendInputEventToInputManager(e, KeyEvent.KEY_TYPED);
+        if (keyEventPostProcessor == null) {
+            keyEventPostProcessor = new KeyEventPostProcessor() {
+                /** [EMAIL PROTECTED] */
+                public boolean postProcessKeyEvent(final KeyEvent  
keyEvent) {
+                    Component owner =  
FocusManager.getCurrentManager().getFocusOwner();
+                    while (owner != null) {
+                        if (owner == PCanvas.this) {
+                            sendInputEventToInputManager(keyEvent,  
keyEvent.getID());
+                            return true;
+                        }
+                        owner = owner.getParent();
+                    }
+                    return false;
                  }
              };
-            addKeyListener(keyListener);
+             
FocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(keyEventPostProcessor);
          }
      }

@@ -571,13 +573,14 @@
              removeMouseMotionListener(mouseMotionListener);
          if (mouseWheelListener != null)
              removeMouseWheelListener(mouseWheelListener);
-        if (keyListener != null)
-            removeKeyListener(keyListener);
+        if (keyEventPostProcessor != null) {
+             
FocusManager.getCurrentKeyboardFocusManager().removeKeyEventPostProcessor(keyEventPostProcessor);
+        }

          mouseListener = null;
          mouseMotionListener = null;
          mouseWheelListener = null;
-        keyListener = null;
+        keyEventPostProcessor = null;
      }

      protected void sendInputEventToInputManager(InputEvent e, int type) {

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

Reply via email to