Aber auch als Allgemeinlösung gefällt mir Deine Arbeit.
Man sieht an meinem Code hoffentlich wenigstens in Ansätzen, dass er darauf vorbereitet ist, im Bedarfsfall mit geringem Aufwand zur allgemeinen Lösung zu mutieren.
🙂
[...] weißt Du welcher Shortcut clearSelection in einer Tabelle ausführt?
Hängt vom TableUI ab. Im Metal L&F (BasicTableUI) ist diese Aktion nicht per KeyBinding verfügbar. In meinen Tabellen hab ich die Aktion aber im Kontextmenü.
Ich bin bisher noch nicht draufgekommen wie ich zu einem bekannten actionMapKey einen KeyStroke ermitteln kann. Geht das?
Natürlich, wenn auch nicht so performant und einfach wie andersherum (was ja der Standard-Anwendungsfall ist). Habe eben meine Utilities erweitert; schenk ich Dir.
😀 [HIGHLIGHT="Java"]/**
* Get the key strokes bound to the given action key from the given input
* map. The parent input maps' bindings are not included.
*
* @param im the input map to investigate
* @param actionKey the action key bound
* @return the array of keys, not {@code null}
* @throws NullPointerException if the input map or the action key is
* {@code null}
*/
public static KeyStroke[] boundKeys(InputMap im, Object actionKey) {
return boundKeys(im, actionKey, false);
}
/**
* Convenience method getting one of the given component's input maps. Get
* the key strokes bound to the given action key from that input map. The
* parent input maps' bindings are not included.
*
* @param component the component to get the input map from
* @param condition the kind of input map to get
* @param actionKey the action key bound
* @return the array of keys, not {@code null}
* @throws NullPointerException if the component or the action key is
* {@code null}
* @see JComponent#getInputMap(int)
*/
public static KeyStroke[] boundKeys(
JComponent component,
int condition,
Object actionKey) {
return boundKeys(component.getInputMap(condition), actionKey, false);
}
/**
* Get the key strokes bound to the given action key from the given input
* map. The parent input maps' bindings are included.
*
* @param im the input map to investigate
* @param actionKey the action key bound
* @return the array of keys, not {@code null}
* @throws NullPointerException if the input map or the action key is
* {@code null}
*/
public static KeyStroke[] allBoundKeys(InputMap im, Object actionKey) {
return boundKeys(im, actionKey, true);
}
/**
* Convenience method getting one of the given component's input maps. Get
* the key strokes bound to the given action key from that input map. The
* parent input maps' bindings are included.
*
* @param component the component to get the input map from
* @param condition the kind of input map to get
* @param actionKey the action key bound
* @return the array of keys, not {@code null}
* @throws NullPointerException if the component or the action key is
* {@code null}
* @see JComponent#getInputMap(int)
*/
public static KeyStroke[] allBoundKeys(
JComponent component,
int condition,
Object actionKey) {
return boundKeys(component.getInputMap(condition), actionKey, true);
}
/**
* Get the key strokes from the given input map, bound to the given action
* key.
*
* @param im the input map to investigate
* @param actionKey the action key bound
* @param includeParents whether or not to include all input map's parents
* @return the array of keys, not {@code null}
* @throws NullPointerException if the input map or the action key is
* {@code null}
*/
private static KeyStroke[] boundKeys(
InputMap im,
Object actionKey,
boolean includeParents) {
/* special handling for first match, as many actions are mapped to one
key stroke only */
KeyStroke firstMatch = null;
Collection<KeyStroke> matches = null;
do {
final KeyStroke[] keys = im.keys();
if (keys == null) {
continue;
}
for (final KeyStroke keyStroke : keys) {
final Object ak = im.get(keyStroke);
if (actionKey == ak || actionKey != null && actionKey.equals(ak)) {
if (firstMatch == null) {
firstMatch = keyStroke;
} else if (matches == null) {
matches = new HashSet<KeyStroke>(7);
matches.add(firstMatch);
matches.add(keyStroke);
} else {
matches.add(keyStroke);
}
}
}
} while (includeParents && (im = im.getParent()) != null);
return firstMatch == null ? new KeyStroke[0] : matches == null
? new KeyStroke[] { firstMatch }
: matches.toArray(new KeyStroke[matches.size()]);
}[/HIGHLIGHT]
Ebenius