下面列出了怎么用javax.swing.plaf.InputMapUIResource的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Object createValue( UIDefaults table ) {
InputMap inputMap = new InputMapUIResource();
for( Object[] bindings : bindingsArray )
LookAndFeel.loadKeyBindings( inputMap, bindings );
return inputMap;
}
/**
* Installs the state needed for mnemonics.
*/
private void initMnemonics() {
mnemonicToIndexMap = new Hashtable<Integer, Integer>();
mnemonicInputMap = new InputMapUIResource();
mnemonicInputMap.setParent(SwingUtilities.getUIInputMap(tabPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT));
SwingUtilities.replaceUIInputMap(tabPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, mnemonicInputMap);
}
/**
* A hack to work around swing hard coding key bindings, and providing
* no reasonable way to access the native ones. Add new components and
* key bindings here when mac users report them.
*
* @param table A mess of all UI defaults crammed in one hash map
*/
private void restoreSystemKeymaps(UIDefaults table) {
/*
* Input mappings that need to be tuned. the list is not exhaustive and
* will likely need more entries when new components start to be used.
*/
String[] keys = { "EditorPane.focusInputMap",
"FormattedTextField.focusInputMap",
"PasswordField.focusInputMap",
"TextArea.focusInputMap",
"TextField.focusInputMap",
"TextPane.focusInputMap" };
// Native modifier key. Ctrl for others, cmd on mac
int modifier = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
for (String key : keys) {
Object value = table.get(key);
if (value instanceof InputMapUIResource) {
InputMapUIResource map = (InputMapUIResource) value;
// CUT
remapKey(map, KeyEvent.VK_X, InputEvent.CTRL_DOWN_MASK, modifier);
// COPY
remapKey(map, KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK, modifier);
// PASTE
remapKey(map, KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK, modifier);
// SELECT ALL
remapKey(map, KeyEvent.VK_A, InputEvent.CTRL_DOWN_MASK, modifier);
} else {
Logger.getLogger(StyledLookAndFeel.class).error("Can not modify resource: " + key);
}
}
}
/**
* Remap a swing default key binding to a native one, if needed.
*
* @param map keymap to be modified
* @param key
* @param defaultModifier swing default modifier key for the action
* @param nativeModifier native modifier key for the action
*/
private void remapKey(InputMapUIResource map, int key, int defaultModifier,
int nativeModifier) {
KeyStroke defaultKey = KeyStroke.getKeyStroke(key, defaultModifier);
Object action = map.get(defaultKey);
KeyStroke nativeKey = KeyStroke.getKeyStroke(key, nativeModifier);
if (!nativeKey.equals(defaultKey)) {
map.remove(defaultKey);
map.put(nativeKey, action);
}
}
protected void initLookAndFeelDefaults() {
InputMapUIResource inputMap =
(InputMapUIResource) UIManager.getLookAndFeelDefaults().get("FormattedTextField.focusInputMap");
inputMap.remove(KeyStroke.getKeyStroke("ESCAPE"));
}
protected InputMap getInputMap()
{
InputMap map = new InputMapUIResource();
return map;
}