Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
19   126   12   1.9
4   53   0.63   10
10     1.2  
1    
8.3% of code in this file is excluded from these metrics.
 
  KeyStrokeMap       Line # 33 19 8.3% 12 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Mar 26, 2008
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5    * the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10    * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11    * specific language governing permissions and limitations under the License.
12    *
13    * Copyright @2008-2010 the original author or authors.
14    */
15    package org.fest.swing.keystroke;
16   
17    import static java.awt.event.InputEvent.SHIFT_MASK;
18    import static java.awt.event.KeyEvent.CHAR_UNDEFINED;
19    import static org.fest.swing.util.Platform.osFamily;
20   
21    import java.util.Locale;
22   
23    import javax.swing.KeyStroke;
24   
25    import org.fest.util.VisibleForTesting;
26   
27    /**
28    * Understands a collection of <code>{@link KeyStrokeMapping}</code>.
29    *
30    * @author Yvonne Wang
31    * @author Alex Ruiz
32    */
 
33    public class KeyStrokeMap {
34   
35    private static KeyStrokeMapCollection maps = new KeyStrokeMapCollection();
36   
 
37  21 toggle static {
38  21 reloadFromSystemSettings();
39    }
40   
41    /**
42    * Reloads the key stroke mappings for the language from the default locale.
43    * @deprecated use <code>{@link #reloadFromSystemSettings()}</code> instead.
44    */
 
45    toggle @Deprecated
46    public static void reloadFromLocale() {
47    reloadFromSystemSettings();
48    }
49   
50    /**
51    * Reloads the key stroke mappings for the language using the current system settings.
52    * @since 1.2
53    */
 
54  25 toggle public static void reloadFromSystemSettings() {
55  25 KeyStrokeMappingProviderPicker picker = new KeyStrokeMappingProviderPicker();
56  25 maps.clear();
57  25 addKeyStrokesFrom(picker.providerFor(osFamily(), Locale.getDefault()));
58    }
59   
 
60  6 toggle @VisibleForTesting
61    static void updateKeyStrokeMapCollection(KeyStrokeMapCollection c) {
62  6 maps = c;
63    }
64   
65    /**
66    * Adds the collection of <code>{@link KeyStrokeMapping}</code>s from the given
67    * <code>{@link KeyStrokeMappingProvider}</code> to this map.
68    * @param provider the given <code>KeyStrokeMappingProvider</code>.
69    */
 
70  27 toggle public static void addKeyStrokesFrom(KeyStrokeMappingProvider provider) {
71  27 for (KeyStrokeMapping entry : provider.keyStrokeMappings())
72  2527 add(entry.character(), entry.keyStroke());
73    }
74   
 
75  2527 toggle private static void add(Character character, KeyStroke keyStroke) {
76  2527 maps.add(character, keyStroke);
77    }
78   
79    /**
80    * Removes all the character-<code>{@link KeyStroke}</code> mappings.
81    */
 
82  4 toggle public static void clearKeyStrokes() {
83  4 maps.clear();
84    }
85   
86    /**
87    * Indicates whether <code>{@link KeyStrokeMap}</code> has mappings or not.
88    * @return <code>true</code> if it has mappings, <code>false</code> otherwise.
89    */
 
90  4 toggle public static boolean hasKeyStrokes() {
91  4 return !maps.isEmpty();
92    }
93   
94    /**
95    * Returns the <code>{@link KeyStroke}</code> corresponding to the given character, as best we can guess it, or
96    * <code>null</code> if we don't know how to generate it.
97    * @param character the given character.
98    * @return the key code-based <code>KeyStroke</code> corresponding to the given character, or <code>null</code> if
99    * we cannot generate it.
100    */
 
101  86 toggle public static KeyStroke keyStrokeFor(char character) {
102  86 return maps.keyStrokeFor(character);
103    }
104   
105    /**
106    * Given a <code>{@link KeyStroke}</code>, returns the equivalent character. Key strokes are defined properly for
107    * US keyboards only. To contribute your own, please add them using the method
108    * <code>{@link #addKeyStrokesFrom(KeyStrokeMappingProvider)}</code>.
109    * @param keyStroke the given <code>KeyStroke</code>.
110    * @return KeyEvent.VK_UNDEFINED if the result is unknown.
111    */
 
112  3 toggle public static char charFor(KeyStroke keyStroke) {
113  3 Character character = maps.charFor(keyStroke);
114    // Try again, but strip all modifiers but shift
115  2 if (character == null) character = charWithoutModifiersButShift(keyStroke);
116  1 if (character == null) return CHAR_UNDEFINED;
117  2 return character.charValue();
118    }
119   
 
120  2 toggle private static Character charWithoutModifiersButShift(KeyStroke keyStroke) {
121  2 int mask = keyStroke.getModifiers() & ~SHIFT_MASK;
122  2 return maps.charFor(KeyStroke.getKeyStroke(keyStroke.getKeyCode(), mask));
123    }
124   
 
125    toggle private KeyStrokeMap() {}
126    }