src/share/classes/java/awt/KeyboardFocusManager.java

Print this page




 974      */
 975     public void setDefaultFocusTraversalPolicy(FocusTraversalPolicy
 976                                                defaultPolicy) {
 977         if (defaultPolicy == null) {
 978             throw new IllegalArgumentException("default focus traversal policy cannot be null");
 979         }
 980 
 981         FocusTraversalPolicy oldPolicy;
 982 
 983         synchronized (this) {
 984             oldPolicy = this.defaultPolicy;
 985             this.defaultPolicy = defaultPolicy;
 986         }
 987 
 988         firePropertyChange("defaultFocusTraversalPolicy", oldPolicy,
 989                            defaultPolicy);
 990     }
 991 
 992     /**
 993      * Sets the default focus traversal keys for a given traversal operation.
 994      * This traversal key <code>Set</code> will be in effect on all
 995      * <code>Window</code>s that have no such <code>Set</code> of
 996      * their own explicitly defined. This <code>Set</code> will also be
 997      * inherited, recursively, by any child <code>Component</code> of
 998      * those <code>Windows</code> that has
 999      * no such <code>Set</code> of its own explicitly defined.
1000      * <p>
1001      * The default values for the default focus traversal keys are
1002      * implementation-dependent. Sun recommends that all implementations for a
1003      * particular native platform use the same default values. The
1004      * recommendations for Windows and Unix are listed below. These
1005      * recommendations are used in the Sun AWT implementations.
1006      *
1007      * <table border=1 summary="Recommended default values for focus traversal keys">
1008      * <tr>
1009      *    <th>Identifier</th>
1010      *    <th>Meaning</th>
1011      *    <th>Default</th>
1012      * </tr>
1013      * <tr>
1014      *    <td><code>KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS</code></td>
1015      *    <td>Normal forward keyboard traversal</td>
1016      *    <td><code>TAB</code> on <code>KEY_PRESSED</code>,
1017      *        <code>CTRL-TAB</code> on <code>KEY_PRESSED</code></td>
1018      * </tr>
1019      * <tr>
1020      *    <td><code>KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS</code></td>
1021      *    <td>Normal reverse keyboard traversal</td>
1022      *    <td><code>SHIFT-TAB</code> on <code>KEY_PRESSED</code>,
1023      *        <code>CTRL-SHIFT-TAB</code> on <code>KEY_PRESSED</code></td>
1024      * </tr>
1025      * <tr>
1026      *    <td><code>KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS</code></td>
1027      *    <td>Go up one focus traversal cycle</td>
1028      *    <td>none</td>
1029      * </tr>
1030      * <tr>
1031      *    <td><code>KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS</code></td>
1032      *    <td>Go down one focus traversal cycle</td>
1033      *    <td>none</td>
1034      * </tr>
1035      * </table>
1036      *
1037      * To disable a traversal key, use an empty <code>Set</code>;
1038      * <code>Collections.EMPTY_SET</code> is recommended.
1039      * <p>
1040      * Using the <code>AWTKeyStroke</code> API, client code can
1041      * specify on which of two
1042      * specific <code>KeyEvent</code>s, <code>KEY_PRESSED</code> or
1043      * <code>KEY_RELEASED</code>, the focus traversal operation will
1044      * occur. Regardless of which <code>KeyEvent</code> is specified,
1045      * however, all <code>KeyEvent</code>s related to the focus
1046      * traversal key, including the associated <code>KEY_TYPED</code>
1047      * event, will be consumed, and will not be dispatched
1048      * to any <code>Component</code>. It is a runtime error to
1049      * specify a <code>KEY_TYPED</code> event as
1050      * mapping to a focus traversal operation, or to map the same event to
1051      * multiple default focus traversal operations.




1052      *
1053      * @param id one of
1054      *        <code>KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS</code>,
1055      *        <code>KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS</code>,
1056      *        <code>KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS</code>, or
1057      *        <code>KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS</code>
1058      * @param keystrokes the Set of <code>AWTKeyStroke</code>s for the
1059      *        specified operation
1060      * @see #getDefaultFocusTraversalKeys
1061      * @see Component#setFocusTraversalKeys
1062      * @see Component#getFocusTraversalKeys
1063      * @throws IllegalArgumentException if id is not one of
1064      *         <code>KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS</code>,
1065      *         <code>KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS</code>,
1066      *         <code>KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS</code>, or
1067      *         <code>KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS</code>,
1068      *         or if keystrokes is <code>null</code>,
1069      *         or if keystrokes contains <code>null</code>,
1070      *         or if any <code>Object</code> in
1071      *         keystrokes is not an <code>AWTKeyStroke</code>,
1072      *         or if any keystroke
1073      *         represents a <code>KEY_TYPED</code> event,
1074      *         or if any keystroke already maps
1075      *         to another default focus traversal operation
1076      * @beaninfo
1077      *       bound: true
1078      */
1079     public void
1080         setDefaultFocusTraversalKeys(int id,
1081                                      Set<? extends AWTKeyStroke> keystrokes)
1082     {
1083         if (id < 0 || id >= TRAVERSAL_KEY_LENGTH) {
1084             throw new IllegalArgumentException("invalid focus traversal key identifier");
1085         }
1086         if (keystrokes == null) {
1087             throw new IllegalArgumentException("cannot set null Set of default focus traversal keys");
1088         }
1089 
1090         Set oldKeys;
1091 
1092         synchronized (this) {
1093             for (Iterator iter = keystrokes.iterator(); iter.hasNext(); ) {
1094                 Object obj = iter.next();
1095 
1096                 if (obj == null) {
1097                     throw new IllegalArgumentException("cannot set null focus traversal key");
1098                 }
1099 
1100                 // Fix for 6195831:
1101                 //According to javadoc this method should throw IAE instead of ClassCastException
1102                 if (!(obj instanceof AWTKeyStroke)) {
1103                     throw new IllegalArgumentException("object is expected to be AWTKeyStroke");
1104                 }
1105                 AWTKeyStroke keystroke = (AWTKeyStroke)obj;
1106 
1107                 if (keystroke.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
1108                     throw new IllegalArgumentException("focus traversal keys cannot map to KEY_TYPED events");
1109                 }
1110 
1111                 // Check to see if key already maps to another traversal
1112                 // operation
1113                 for (int i = 0; i < TRAVERSAL_KEY_LENGTH; i++) {
1114                     if (i == id) {
1115                         continue;
1116                     }
1117 
1118                     if (defaultFocusTraversalKeys[i].contains(keystroke)) {
1119                         throw new IllegalArgumentException("focus traversal keys must be unique for a Component");
1120                     }
1121                 }
1122             }
1123 
1124             oldKeys = defaultFocusTraversalKeys[id];
1125             defaultFocusTraversalKeys[id] =




 974      */
 975     public void setDefaultFocusTraversalPolicy(FocusTraversalPolicy
 976                                                defaultPolicy) {
 977         if (defaultPolicy == null) {
 978             throw new IllegalArgumentException("default focus traversal policy cannot be null");
 979         }
 980 
 981         FocusTraversalPolicy oldPolicy;
 982 
 983         synchronized (this) {
 984             oldPolicy = this.defaultPolicy;
 985             this.defaultPolicy = defaultPolicy;
 986         }
 987 
 988         firePropertyChange("defaultFocusTraversalPolicy", oldPolicy,
 989                            defaultPolicy);
 990     }
 991 
 992     /**
 993      * Sets the default focus traversal keys for a given traversal operation.
 994      * This traversal key {@code Set} will be in effect on all
 995      * {@code Window}s that have no such {@code Set} of
 996      * their own explicitly defined. This {@code Set} will also be
 997      * inherited, recursively, by any child {@code Component} of
 998      * those {@code Windows} that has
 999      * no such {@code Set} of its own explicitly defined.
1000      * <p>
1001      * The default values for the default focus traversal keys are
1002      * implementation-dependent. Sun recommends that all implementations for a
1003      * particular native platform use the same default values. The
1004      * recommendations for Windows and Unix are listed below. These
1005      * recommendations are used in the Sun AWT implementations.
1006      *
1007      * <table border=1 summary="Recommended default values for focus traversal keys">
1008      * <tr>
1009      *    <th>Identifier</th>
1010      *    <th>Meaning</th>
1011      *    <th>Default</th>
1012      * </tr>
1013      * <tr>
1014      *    <td>{@code KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS}</td>
1015      *    <td>Normal forward keyboard traversal</td>
1016      *    <td>{@code TAB} on {@code KEY_PRESSED},
1017      *        {@code CTRL-TAB} on {@code KEY_PRESSED}</td>
1018      * </tr>
1019      * <tr>
1020      *    <td>{@code KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS}</td>
1021      *    <td>Normal reverse keyboard traversal</td>
1022      *    <td>{@code SHIFT-TAB} on {@code KEY_PRESSED},
1023      *        {@code CTRL-SHIFT-TAB} on {@code KEY_PRESSED}</td>
1024      * </tr>
1025      * <tr>
1026      *    <td>{@code KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS}</td>
1027      *    <td>Go up one focus traversal cycle</td>
1028      *    <td>none</td>
1029      * </tr>
1030      * <tr>
1031      *    <td>{@code KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS}</td>
1032      *    <td>Go down one focus traversal cycle</td>
1033      *    <td>none</td>
1034      * </tr>
1035      * </table>
1036      *
1037      * To disable a traversal key, use an empty {@code Set};
1038      * {@code Collections.EMPTY_SET} is recommended.
1039      * <p>
1040      * Using the {@code AWTKeyStroke} API, client code can
1041      * specify on which of two
1042      * specific {@code KeyEvent}s, {@code KEY_PRESSED} or
1043      * {@code KEY_RELEASED}, the focus traversal operation will
1044      * occur. Regardless of which {@code KeyEvent} is specified,
1045      * however, all {@code KeyEvent}s related to the focus
1046      * traversal key, including the associated {@code KEY_TYPED}
1047      * event, will be consumed, and will not be dispatched
1048      * to any {@code Component}. It is a runtime error to
1049      * specify a {@code KEY_TYPED} event as
1050      * mapping to a focus traversal operation, or to map the same event to
1051      * multiple default focus traversal operations.
1052      * <p>
1053      * This method may throw a {@code ClassCastException} if any 
1054      * {@code Object} in {@code keystrokes} is not an 
1055      * {@code AWTKeyStroke}.
1056      *
1057      * @param id one of
1058      *        {@code KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS},
1059      *        {@code KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS},
1060      *        {@code KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS}, or
1061      *        {@code KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS}
1062      * @param keystrokes the Set of {@code AWTKeyStroke}s for the
1063      *        specified operation
1064      * @see #getDefaultFocusTraversalKeys
1065      * @see Component#setFocusTraversalKeys
1066      * @see Component#getFocusTraversalKeys
1067      * @throws IllegalArgumentException if id is not one of
1068      *         {@code KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS},
1069      *         {@code KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS},
1070      *         {@code KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS}, or
1071      *         {@code KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS},
1072      *         or if keystrokes is {@code null},
1073      *         or if keystrokes contains {@code null},


1074      *         or if any keystroke
1075      *         represents a {@code KEY_TYPED} event,
1076      *         or if any keystroke already maps
1077      *         to another default focus traversal operation
1078      * @beaninfo
1079      *       bound: true
1080      */
1081     public void
1082         setDefaultFocusTraversalKeys(int id,
1083                                      Set<? extends AWTKeyStroke> keystrokes)
1084     {
1085         if (id < 0 || id >= TRAVERSAL_KEY_LENGTH) {
1086             throw new IllegalArgumentException("invalid focus traversal key identifier");
1087         }
1088         if (keystrokes == null) {
1089             throw new IllegalArgumentException("cannot set null Set of default focus traversal keys");
1090         }
1091 
1092         Set oldKeys;
1093 
1094         synchronized (this) {
1095             for (AWTKeyStroke keystroke : keystrokes) {

1096 
1097                 if (keystroke == null) {
1098                     throw new IllegalArgumentException("cannot set null focus traversal key");
1099                 }







1100 
1101                 if (keystroke.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
1102                     throw new IllegalArgumentException("focus traversal keys cannot map to KEY_TYPED events");
1103                 }
1104 
1105                 // Check to see if key already maps to another traversal
1106                 // operation
1107                 for (int i = 0; i < TRAVERSAL_KEY_LENGTH; i++) {
1108                     if (i == id) {
1109                         continue;
1110                     }
1111 
1112                     if (defaultFocusTraversalKeys[i].contains(keystroke)) {
1113                         throw new IllegalArgumentException("focus traversal keys must be unique for a Component");
1114                     }
1115                 }
1116             }
1117 
1118             oldKeys = defaultFocusTraversalKeys[id];
1119             defaultFocusTraversalKeys[id] =