import sun.swing.UIAction; //导入依赖的package包/类
/**
* Invokes actionPerformed
on action
if
* action
is enabled (and non-{@code null}). The command for the
* ActionEvent is determined by:
*
*
If the action was registered via* registerKeyboardAction
, then the command string
* passed in ({@code null} will be used if {@code null} was passed in).
*
Action value with name Action.ACTION_COMMAND_KEY, unless {@code null}.*
String value of the KeyEvent, unlessgetKeyChar
* returns KeyEvent.CHAR_UNDEFINED..
*
* This will return true if action
is non-{@code null} and
* actionPerformed is invoked on it.
*
* @since 1.3
*/
public static boolean notifyAction(Action action, KeyStroke ks,
KeyEvent event, Object sender,
int modifiers) {
if (action == null) {
return false;
}
if (action instanceof UIAction) {
if (!((UIAction)action).isEnabled(sender)) {
return false;
}
}
else if (!action.isEnabled()) {
return false;
}
Object commandO;
boolean stayNull;
// Get the command object.
commandO = action.getValue(Action.ACTION_COMMAND_KEY);
if (commandO == null && (action instanceof JComponent.ActionStandin)) {
// ActionStandin is used for historical reasons to support
// registerKeyboardAction with a null value.
stayNull = true;
}
else {
stayNull = false;
}
// Convert it to a string.
String command;
if (commandO != null) {
command = commandO.toString();
}
else if (!stayNull && event.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
command = String.valueOf(event.getKeyChar());
}
else {
// Do null for undefined chars, or if registerKeyboardAction
// was called with a null.
command = null;
}
action.actionPerformed(new ActionEvent(sender,
ActionEvent.ACTION_PERFORMED, command, event.getWhen(),
modifiers));
return true;
}