src/share/classes/java/awt/DefaultKeyboardFocusManager.java

Print this page




 791      * DefaultKeyboardFocusManager is designed so that neither
 792      * <code>dispatchEvent</code>, nor the AWT event dispatcher, should take
 793      * further action on the event in any situation.
 794      *
 795      * @param e the KeyEvent to be dispatched
 796      * @return <code>true</code>
 797      * @see Component#dispatchEvent
 798      */
 799     public boolean dispatchKeyEvent(KeyEvent e) {
 800         Component focusOwner = (((AWTEvent)e).isPosted) ? getFocusOwner() : e.getComponent();
 801 
 802         if (focusOwner != null && focusOwner.isShowing() && focusOwner.canBeFocusOwner()) {
 803             if (!e.isConsumed()) {
 804                 Component comp = e.getComponent();
 805                 if (comp != null && comp.isEnabled()) {
 806                     redispatchEvent(comp, e);
 807                 }
 808             }
 809         }
 810         boolean stopPostProcessing = false;
 811         java.util.List processors = getKeyEventPostProcessors();
 812         if (processors != null) {
 813             for (java.util.Iterator iter = processors.iterator();
 814                  !stopPostProcessing && iter.hasNext(); )
 815             {
 816                 stopPostProcessing = (((KeyEventPostProcessor)(iter.next())).
 817                             postProcessKeyEvent(e));
 818             }
 819         }
 820         if (!stopPostProcessing) {
 821             postProcessKeyEvent(e);
 822         }
 823 
 824         // Allow the peer to process KeyEvent
 825         Component source = e.getComponent();
 826         ComponentPeer peer = source.getPeer();
 827 
 828         if (peer == null || peer instanceof LightweightPeer) {
 829             // if focus owner is lightweight then its native container
 830             // processes event
 831             Container target = source.getNativeContainer();
 832             if (target != null) {
 833                 peer = target.getPeer();


1042          * event proxying mechanism is active.
1043          * If it is active we should redispatch key events after
1044          * we detected its correct target.
1045          */
1046         if (KeyboardFocusManager.isProxyActive(ke)) {
1047             Component source = (Component)ke.getSource();
1048             Container target = source.getNativeContainer();
1049             if (target != null) {
1050                 ComponentPeer peer = target.getPeer();
1051                 if (peer != null) {
1052                     peer.handleEvent(ke);
1053                     /**
1054                      * Fix for 4478780 - consume event after it was dispatched by peer.
1055                      */
1056                     ke.consume();
1057                 }
1058             }
1059             return true;
1060         }
1061 
1062         java.util.List dispatchers = getKeyEventDispatchers();
1063         if (dispatchers != null) {
1064             for (java.util.Iterator iter = dispatchers.iterator();
1065                  iter.hasNext(); )
1066              {
1067                  if (((KeyEventDispatcher)(iter.next())).
1068                      dispatchKeyEvent(ke))
1069                  {
1070                      return true;
1071                  }
1072              }
1073         }
1074         return dispatchKeyEvent(ke);
1075     }
1076 
1077     /*
1078      * @param e is a KEY_PRESSED event that can be used
1079      *          to track the next KEY_TYPED related.
1080      */
1081     private void consumeNextKeyTyped(KeyEvent e) {
1082         consumeNextKeyTyped = true;
1083     }
1084 


1114      * @param e the event that may represent a focus traversal key
1115      */
1116     public void processKeyEvent(Component focusedComponent, KeyEvent e) {
1117         // consume processed event if needed
1118         if (consumeProcessedKeyEvent(e)) {
1119             return;
1120         }
1121 
1122         // KEY_TYPED events cannot be focus traversal keys
1123         if (e.getID() == KeyEvent.KEY_TYPED) {
1124             return;
1125         }
1126 
1127         if (focusedComponent.getFocusTraversalKeysEnabled() &&
1128             !e.isConsumed())
1129         {
1130             AWTKeyStroke stroke = AWTKeyStroke.getAWTKeyStrokeForEvent(e),
1131                 oppStroke = AWTKeyStroke.getAWTKeyStroke(stroke.getKeyCode(),
1132                                                  stroke.getModifiers(),
1133                                                  !stroke.isOnKeyRelease());
1134             Set toTest;
1135             boolean contains, containsOpp;
1136 
1137             toTest = focusedComponent.getFocusTraversalKeys(
1138                 KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
1139             contains = toTest.contains(stroke);
1140             containsOpp = toTest.contains(oppStroke);
1141 
1142             if (contains || containsOpp) {
1143                 consumeTraversalKey(e);
1144                 if (contains) {
1145                     focusNextComponent(focusedComponent);
1146                 }
1147                 return;
1148             } else if (e.getID() == KeyEvent.KEY_PRESSED) {
1149                 // Fix for 6637607: consumeNextKeyTyped should be reset.
1150                 consumeNextKeyTyped = false;
1151             }
1152 
1153             toTest = focusedComponent.getFocusTraversalKeys(
1154                 KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);




 791      * DefaultKeyboardFocusManager is designed so that neither
 792      * <code>dispatchEvent</code>, nor the AWT event dispatcher, should take
 793      * further action on the event in any situation.
 794      *
 795      * @param e the KeyEvent to be dispatched
 796      * @return <code>true</code>
 797      * @see Component#dispatchEvent
 798      */
 799     public boolean dispatchKeyEvent(KeyEvent e) {
 800         Component focusOwner = (((AWTEvent)e).isPosted) ? getFocusOwner() : e.getComponent();
 801 
 802         if (focusOwner != null && focusOwner.isShowing() && focusOwner.canBeFocusOwner()) {
 803             if (!e.isConsumed()) {
 804                 Component comp = e.getComponent();
 805                 if (comp != null && comp.isEnabled()) {
 806                     redispatchEvent(comp, e);
 807                 }
 808             }
 809         }
 810         boolean stopPostProcessing = false;
 811         java.util.List<KeyEventPostProcessor> processors = getKeyEventPostProcessors();
 812         if (processors != null) {
 813             for (java.util.Iterator<KeyEventPostProcessor> iter = processors.iterator();
 814                  !stopPostProcessing && iter.hasNext(); )
 815             {
 816                 stopPostProcessing = (((KeyEventPostProcessor)(iter.next())).
 817                             postProcessKeyEvent(e));
 818             }
 819         }
 820         if (!stopPostProcessing) {
 821             postProcessKeyEvent(e);
 822         }
 823 
 824         // Allow the peer to process KeyEvent
 825         Component source = e.getComponent();
 826         ComponentPeer peer = source.getPeer();
 827 
 828         if (peer == null || peer instanceof LightweightPeer) {
 829             // if focus owner is lightweight then its native container
 830             // processes event
 831             Container target = source.getNativeContainer();
 832             if (target != null) {
 833                 peer = target.getPeer();


1042          * event proxying mechanism is active.
1043          * If it is active we should redispatch key events after
1044          * we detected its correct target.
1045          */
1046         if (KeyboardFocusManager.isProxyActive(ke)) {
1047             Component source = (Component)ke.getSource();
1048             Container target = source.getNativeContainer();
1049             if (target != null) {
1050                 ComponentPeer peer = target.getPeer();
1051                 if (peer != null) {
1052                     peer.handleEvent(ke);
1053                     /**
1054                      * Fix for 4478780 - consume event after it was dispatched by peer.
1055                      */
1056                     ke.consume();
1057                 }
1058             }
1059             return true;
1060         }
1061 
1062         java.util.List<KeyEventDispatcher> dispatchers = getKeyEventDispatchers();
1063         if (dispatchers != null) {
1064             for (java.util.Iterator<KeyEventDispatcher> iter = dispatchers.iterator();
1065                  iter.hasNext(); )
1066              {
1067                  if (((KeyEventDispatcher)(iter.next())).
1068                      dispatchKeyEvent(ke))
1069                  {
1070                      return true;
1071                  }
1072              }
1073         }
1074         return dispatchKeyEvent(ke);
1075     }
1076 
1077     /*
1078      * @param e is a KEY_PRESSED event that can be used
1079      *          to track the next KEY_TYPED related.
1080      */
1081     private void consumeNextKeyTyped(KeyEvent e) {
1082         consumeNextKeyTyped = true;
1083     }
1084 


1114      * @param e the event that may represent a focus traversal key
1115      */
1116     public void processKeyEvent(Component focusedComponent, KeyEvent e) {
1117         // consume processed event if needed
1118         if (consumeProcessedKeyEvent(e)) {
1119             return;
1120         }
1121 
1122         // KEY_TYPED events cannot be focus traversal keys
1123         if (e.getID() == KeyEvent.KEY_TYPED) {
1124             return;
1125         }
1126 
1127         if (focusedComponent.getFocusTraversalKeysEnabled() &&
1128             !e.isConsumed())
1129         {
1130             AWTKeyStroke stroke = AWTKeyStroke.getAWTKeyStrokeForEvent(e),
1131                 oppStroke = AWTKeyStroke.getAWTKeyStroke(stroke.getKeyCode(),
1132                                                  stroke.getModifiers(),
1133                                                  !stroke.isOnKeyRelease());
1134             Set<AWTKeyStroke> toTest;
1135             boolean contains, containsOpp;
1136 
1137             toTest = focusedComponent.getFocusTraversalKeys(
1138                 KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
1139             contains = toTest.contains(stroke);
1140             containsOpp = toTest.contains(oppStroke);
1141 
1142             if (contains || containsOpp) {
1143                 consumeTraversalKey(e);
1144                 if (contains) {
1145                     focusNextComponent(focusedComponent);
1146                 }
1147                 return;
1148             } else if (e.getID() == KeyEvent.KEY_PRESSED) {
1149                 // Fix for 6637607: consumeNextKeyTyped should be reset.
1150                 consumeNextKeyTyped = false;
1151             }
1152 
1153             toTest = focusedComponent.getFocusTraversalKeys(
1154                 KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);