src/share/classes/java/awt/AWTKeyStroke.java

Print this page


   1 /*
   2  * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 226     /* returns noarg Constructor for class with accessible flag. No security
 227        threat as accessible flag is set only for this Constructor object,
 228        not for Class constructor.
 229      */
 230     private static Constructor getCtor(final Class clazz)
 231     {
 232         Constructor ctor = AccessController.doPrivileged(new PrivilegedAction<Constructor>() {
 233             public Constructor run() {
 234                 try {
 235                     Constructor ctor = clazz.getDeclaredConstructor((Class[]) null);
 236                     if (ctor != null) {
 237                         ctor.setAccessible(true);
 238                     }
 239                     return ctor;
 240                 } catch (SecurityException e) {
 241                 } catch (NoSuchMethodException e) {
 242                 }
 243                 return null;
 244             }
 245         });
 246         return (Constructor)ctor;
 247     }
 248 
 249     private static synchronized AWTKeyStroke getCachedStroke
 250         (char keyChar, int keyCode, int modifiers, boolean onKeyRelease)
 251     {
 252         Map<AWTKeyStroke, AWTKeyStroke> cache = (Map)AppContext.getAppContext().get(APP_CONTEXT_CACHE_KEY);
 253         AWTKeyStroke cacheKey = (AWTKeyStroke)AppContext.getAppContext().get(APP_CONTEXT_KEYSTROKE_KEY);
 254 
 255         if (cache == null) {
 256             cache = new HashMap<>();
 257             AppContext.getAppContext().put(APP_CONTEXT_CACHE_KEY, cache);
 258         }
 259 
 260         if (cacheKey == null) {
 261             try {
 262                 Class<AWTKeyStroke> clazz = getAWTKeyStrokeClass();
 263                 cacheKey = (AWTKeyStroke)getCtor(clazz).newInstance((Object[]) null);
 264                 AppContext.getAppContext().put(APP_CONTEXT_KEYSTROKE_KEY, cacheKey);
 265             } catch (InstantiationException e) {
 266                 assert(false);
 267             } catch (IllegalAccessException e) {
 268                 assert(false);
 269             } catch (InvocationTargetException e) {
 270                 assert(false);
 271             }
 272         }
 273         cacheKey.keyChar = keyChar;
 274         cacheKey.keyCode = keyCode;
 275         cacheKey.modifiers = mapNewModifiers(mapOldModifiers(modifiers));
 276         cacheKey.onKeyRelease = onKeyRelease;
 277 
 278         AWTKeyStroke stroke = (AWTKeyStroke)cache.get(cacheKey);
 279         if (stroke == null) {
 280             stroke = cacheKey;
 281             cache.put(stroke, stroke);
 282             AppContext.getAppContext().remove(APP_CONTEXT_KEYSTROKE_KEY);
 283         }
 284         return stroke;
 285     }
 286 
 287     /**
 288      * Returns a shared instance of an <code>AWTKeyStroke</code>
 289      * that represents a <code>KEY_TYPED</code> event for the
 290      * specified character.
 291      *
 292      * @param keyChar the character value for a keyboard key
 293      * @return an <code>AWTKeyStroke</code> object for that key
 294      */
 295     public static AWTKeyStroke getAWTKeyStroke(char keyChar) {
 296         return getCachedStroke(keyChar, KeyEvent.VK_UNDEFINED, 0, false);
 297     }
 298 


 564                 String keyCodeName = "VK_" + token;
 565                 int keyCode = getVKValue(keyCodeName);
 566 
 567                 return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode,
 568                                        mask, released);
 569             }
 570 
 571             if (token.equals("released")) {
 572                 released = true;
 573                 continue;
 574             }
 575             if (token.equals("pressed")) {
 576                 pressed = true;
 577                 continue;
 578             }
 579             if (token.equals("typed")) {
 580                 typed = true;
 581                 continue;
 582             }
 583 
 584             Integer tokenMask = (Integer)modifierKeywords.get(token);
 585             if (tokenMask != null) {
 586                 mask |= tokenMask.intValue();
 587             } else {
 588                 throw new IllegalArgumentException(errmsg);
 589             }
 590         }
 591 
 592         throw new IllegalArgumentException(errmsg);
 593     }
 594 
 595     private static VKCollection getVKCollection() {
 596         if (vks == null) {
 597             vks = new VKCollection();
 598         }
 599         return vks;
 600     }
 601     /**
 602      * Returns the integer constant for the KeyEvent.VK field named
 603      * <code>key</code>. This will throw an
 604      * <code>IllegalArgumentException</code> if <code>key</code> is


 862 
 863 class VKCollection {
 864     Map<Integer, String> code2name;
 865     Map<String, Integer> name2code;
 866 
 867     public VKCollection() {
 868         code2name = new HashMap<>();
 869         name2code = new HashMap<>();
 870     }
 871 
 872     public synchronized void put(String name, Integer code) {
 873         assert((name != null) && (code != null));
 874         assert(findName(code) == null);
 875         assert(findCode(name) == null);
 876         code2name.put(code, name);
 877         name2code.put(name, code);
 878     }
 879 
 880     public synchronized Integer findCode(String name) {
 881         assert(name != null);
 882         return (Integer)name2code.get(name);
 883     }
 884 
 885     public synchronized String findName(Integer code) {
 886         assert(code != null);
 887         return (String)code2name.get(code);
 888     }
 889 }
   1 /*
   2  * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 226     /* returns noarg Constructor for class with accessible flag. No security
 227        threat as accessible flag is set only for this Constructor object,
 228        not for Class constructor.
 229      */
 230     private static Constructor getCtor(final Class clazz)
 231     {
 232         Constructor ctor = AccessController.doPrivileged(new PrivilegedAction<Constructor>() {
 233             public Constructor run() {
 234                 try {
 235                     Constructor ctor = clazz.getDeclaredConstructor((Class[]) null);
 236                     if (ctor != null) {
 237                         ctor.setAccessible(true);
 238                     }
 239                     return ctor;
 240                 } catch (SecurityException e) {
 241                 } catch (NoSuchMethodException e) {
 242                 }
 243                 return null;
 244             }
 245         });
 246         return ctor;
 247     }
 248 
 249     private static synchronized AWTKeyStroke getCachedStroke
 250         (char keyChar, int keyCode, int modifiers, boolean onKeyRelease)
 251     {
 252         Map<AWTKeyStroke, AWTKeyStroke> cache = (Map)AppContext.getAppContext().get(APP_CONTEXT_CACHE_KEY);
 253         AWTKeyStroke cacheKey = (AWTKeyStroke)AppContext.getAppContext().get(APP_CONTEXT_KEYSTROKE_KEY);
 254 
 255         if (cache == null) {
 256             cache = new HashMap<>();
 257             AppContext.getAppContext().put(APP_CONTEXT_CACHE_KEY, cache);
 258         }
 259 
 260         if (cacheKey == null) {
 261             try {
 262                 Class<AWTKeyStroke> clazz = getAWTKeyStrokeClass();
 263                 cacheKey = (AWTKeyStroke)getCtor(clazz).newInstance((Object[]) null);
 264                 AppContext.getAppContext().put(APP_CONTEXT_KEYSTROKE_KEY, cacheKey);
 265             } catch (InstantiationException e) {
 266                 assert(false);
 267             } catch (IllegalAccessException e) {
 268                 assert(false);
 269             } catch (InvocationTargetException e) {
 270                 assert(false);
 271             }
 272         }
 273         cacheKey.keyChar = keyChar;
 274         cacheKey.keyCode = keyCode;
 275         cacheKey.modifiers = mapNewModifiers(mapOldModifiers(modifiers));
 276         cacheKey.onKeyRelease = onKeyRelease;
 277 
 278         AWTKeyStroke stroke = cache.get(cacheKey);
 279         if (stroke == null) {
 280             stroke = cacheKey;
 281             cache.put(stroke, stroke);
 282             AppContext.getAppContext().remove(APP_CONTEXT_KEYSTROKE_KEY);
 283         }
 284         return stroke;
 285     }
 286 
 287     /**
 288      * Returns a shared instance of an <code>AWTKeyStroke</code>
 289      * that represents a <code>KEY_TYPED</code> event for the
 290      * specified character.
 291      *
 292      * @param keyChar the character value for a keyboard key
 293      * @return an <code>AWTKeyStroke</code> object for that key
 294      */
 295     public static AWTKeyStroke getAWTKeyStroke(char keyChar) {
 296         return getCachedStroke(keyChar, KeyEvent.VK_UNDEFINED, 0, false);
 297     }
 298 


 564                 String keyCodeName = "VK_" + token;
 565                 int keyCode = getVKValue(keyCodeName);
 566 
 567                 return getCachedStroke(KeyEvent.CHAR_UNDEFINED, keyCode,
 568                                        mask, released);
 569             }
 570 
 571             if (token.equals("released")) {
 572                 released = true;
 573                 continue;
 574             }
 575             if (token.equals("pressed")) {
 576                 pressed = true;
 577                 continue;
 578             }
 579             if (token.equals("typed")) {
 580                 typed = true;
 581                 continue;
 582             }
 583 
 584             Integer tokenMask = modifierKeywords.get(token);
 585             if (tokenMask != null) {
 586                 mask |= tokenMask.intValue();
 587             } else {
 588                 throw new IllegalArgumentException(errmsg);
 589             }
 590         }
 591 
 592         throw new IllegalArgumentException(errmsg);
 593     }
 594 
 595     private static VKCollection getVKCollection() {
 596         if (vks == null) {
 597             vks = new VKCollection();
 598         }
 599         return vks;
 600     }
 601     /**
 602      * Returns the integer constant for the KeyEvent.VK field named
 603      * <code>key</code>. This will throw an
 604      * <code>IllegalArgumentException</code> if <code>key</code> is


 862 
 863 class VKCollection {
 864     Map<Integer, String> code2name;
 865     Map<String, Integer> name2code;
 866 
 867     public VKCollection() {
 868         code2name = new HashMap<>();
 869         name2code = new HashMap<>();
 870     }
 871 
 872     public synchronized void put(String name, Integer code) {
 873         assert((name != null) && (code != null));
 874         assert(findName(code) == null);
 875         assert(findCode(name) == null);
 876         code2name.put(code, name);
 877         name2code.put(name, code);
 878     }
 879 
 880     public synchronized Integer findCode(String name) {
 881         assert(name != null);
 882         return name2code.get(name);
 883     }
 884 
 885     public synchronized String findName(Integer code) {
 886         assert(code != null);
 887         return code2name.get(code);
 888     }
 889 }