下面列出了javax.swing.JComponent#getActionMap ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void mapAction(JComponent jc, Map<String, Object[]> actionKeys, Action act, String key) {
if (actionKeys == null) {
actionKeys = getActionKeys(jc);
}
if (jc != null && actionKeys != null) {
ActionMap am = jc.getActionMap();
Object[] keys = actionKeys.get(key);
boolean replaced = false;
if (keys != null) {
for (Object k : keys) {
am.put(k, act);
if (k.equals(key)) {
replaced = true;
}
}
}
if (!replaced) {
am.put(key, act);
}
}
}
/**
* Registers a component for tooltip management.
* <p>
* This will register key bindings to show and hide the tooltip text
* only if <code>component</code> has focus bindings. This is done
* so that components that are not normally focus traversable, such
* as <code>JLabel</code>, are not made focus traversable as a result
* of invoking this method.
*
* @param component a <code>JComponent</code> object to add
* @see JComponent#isFocusTraversable
*/
protected void registerComponent(JComponent component) {
component.removeMouseListener(this);
component.addMouseListener(this);
component.removeMouseMotionListener(moveBeforeEnterListener);
component.addMouseMotionListener(moveBeforeEnterListener);
if (shouldRegisterBindings(component)) {
// register our accessibility keybindings for this component
// this will apply globally across L&F
// Post Tip: Ctrl+F1
// Unpost Tip: Esc and Ctrl+F1
InputMap inputMap = component.getInputMap(JComponent.WHEN_FOCUSED);
ActionMap actionMap = component.getActionMap();
if (inputMap != null && actionMap != null) {
//XXX remove
}
}
}
/**
* Collects the keys of all the actions linked to a specific
* {@link javax.swing.JComponent} and groups them in arrays by action names.
*
* @param jc The JComponent with the actions linked to.
* @return A HashMap formed by pairs of action names (key) and arrays of action
* keys (value). Usually each action name has only one action key
* associated to it, and the value of the pair is an object array with
* only one string, but this is not an imperative: several actions can
* be associated to a single name.
*/
public static Map<String, Object[]> getActionKeys(JComponent jc) {
Map<String, Object[]> result = new HashMap<String, Object[]>();
ActionMap am = jc.getActionMap();
for (Object amk : am.allKeys()) {
Action act = am.get(amk);
Object o = act.getValue(Action.NAME);
if (o == null)
o = "";
String name = o.toString();
Object[] keys = result.get(name);
if (keys == null)
keys = new Object[] { amk };
else {
Object[] k2 = new Object[keys.length + 1];
int j;
for (j = 0; j < keys.length; j++)
k2[j] = keys[j];
k2[j] = amk;
keys = k2;
}
result.put(name, keys);
}
return result;
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
/**
* Removes a component from tooltip control.
*
* @param component a <code>JComponent</code> object to remove
*/
protected void unregisterComponent(JComponent component) {
component.removeMouseListener(this);
component.removeMouseMotionListener(moveBeforeEnterListener);
if (shouldRegisterBindings(component)) {
InputMap inputMap = component.getInputMap(JComponent.WHEN_FOCUSED);
ActionMap actionMap = component.getActionMap();
if (inputMap != null && actionMap != null) {
//XXX remove
}
}
}
private void registerCustomCCPActions(@Nonnull final JComponent component) {
final ActionMap actionMap = component.getActionMap();
actionMap.put(DefaultEditorKit.cutAction, this.actionCut);
actionMap.put(DefaultEditorKit.copyAction, this.actionCopy);
actionMap.put(DefaultEditorKit.pasteAction, this.actionPaste);
actionMap.put(FindAction.class.getName(), findAction);
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
private void addSpaceActionhandler(JComponent host) {
InputMap inputMap = host.getInputMap();
ActionMap actionMap = host.getActionMap();
KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
String key = keyStroke.toString();
inputMap.put(keyStroke, key);
actionMap.put(key, invertCheckAction);
}
private void bindKeys (JComponent component)
{
InputMap inputMap;
// inputMap = component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
// inputMap = component.getInputMap(JComponent.WHEN_FOCUSED); // Default
inputMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = component.getActionMap();
inputMap.put(KeyStroke.getKeyStroke("UP"), "UpAction");
actionMap.put("UpAction", new UpAction());
inputMap.put(KeyStroke.getKeyStroke("DOWN"), "DownAction");
actionMap.put("DownAction", new DownAction());
inputMap.put(KeyStroke.getKeyStroke("shift UP"), "ShiftUpAction");
actionMap.put("ShiftUpAction", new ShiftUpAction());
inputMap.put(KeyStroke.getKeyStroke("shift DOWN"), "ShiftDownAction");
actionMap.put("ShiftDownAction", new ShiftDownAction());
inputMap.put(KeyStroke.getKeyStroke("LEFT"), "LeftAction");
actionMap.put("LeftAction", new LeftAction());
inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "RightAction");
actionMap.put("RightAction", new RightAction());
inputMap.put(KeyStroke.getKeyStroke("shift LEFT"), "ShiftLeftAction");
actionMap.put("ShiftLeftAction", new ShiftLeftAction());
inputMap.put(KeyStroke.getKeyStroke("shift RIGHT"), "ShiftRightAction");
actionMap.put("ShiftRightAction", new ShiftRightAction());
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
/**
* Helper method to play a named sound.
*
* @param c JComponent to play the sound for.
* @param actionKey Key for the sound.
*/
static void playSound(JComponent c, Object actionKey) {
LookAndFeel laf = UIManager.getLookAndFeel();
if (laf instanceof BasicLookAndFeel) {
ActionMap map = c.getActionMap();
if (map != null) {
Action audioAction = map.get(actionKey);
if (audioAction != null) {
// pass off firing the Action to a utility method
((BasicLookAndFeel)laf).playSound(audioAction);
}
}
}
}
private void init( final ReportDesignerContext reportDesignerContext ) {
if ( reportDesignerContext == null ) {
throw new NullPointerException();
}
setTitle( Messages.getString( "WelcomePane.title" ) ); // NON-NLS
this.reportDesignerContext = reportDesignerContext;
this.newReportAction = new NewReportAction();
this.newReportAction.setReportDesignerContext( reportDesignerContext );
this.closeActionListener = new CloseActionListener();
showOnStartupCheckbox = new JCheckBox(
Messages.getString( "WelcomePane.showAtStartup" ), WorkspaceSettings.getInstance().isShowLauncher() ); // NON-NLS
showOnStartupCheckbox.addActionListener( new TriggerShowWelcomePaneAction() );
backgroundImage = Toolkit.getDefaultToolkit().createImage(
IconLoader.class.getResource( "/org/pentaho/reporting/designer/core/icons/WelcomeBackground.png" ) ); // NON-NLS
final WaitingImageObserver obs = new WaitingImageObserver( backgroundImage );
obs.waitImageLoaded();
setResizable( false );
initGUI();
pack();
final JComponent contentPane = (JComponent) getContentPane();
final InputMap inputMap = contentPane.getInputMap();
final ActionMap actionMap = contentPane.getActionMap();
inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ), "cancel" ); // NON-NLS
actionMap.put( "cancel", new CloseActionListener() ); // NON-NLS
}