/* * File : Actions.java * Created : 18-dec-2001 10:48 * By : fbusquets * * JClic - Authoring and playing system for educational activities * * Copyright (C) 2000 - 2018 Francesc Busquets & Departament * d'Educacio de la Generalitat de Catalunya * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details (see the LICENSE file). */ package edu.xtec.util; import java.awt.Component; import java.awt.Container; import java.awt.event.ActionEvent; import java.util.HashMap; import java.util.Map; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.ActionMap; import javax.swing.JComponent; /** * Miscellaneous utilities for managing {@link javax.swing.Action} objects. * *
* EXAMPLES: * *
* Creation of a new action, with reference to a original key:
* AbstractAction beginAction=new AbstractAction(DefaultEditorKit.beginAction){
* public void actionPerformed(ActionEvent e){
* // ...
* }
* };
*
Creation of a new action, with a new key:
* AbstractAction prevTargetAction=new AbstractAction("prev-target"){
* public void actionPerformed(ActionEvent e){
* // ...
* }
* };
*
Action that references an existing one:
* Action kitUpAction=null;
* AbstractAction upAction=new AbstractAction(DefaultEditorKit.upAction){
* public void actionPerformed(ActionEvent e){
* if(kitUpAction!=null){
* // .... pre-action
* kitUpAction.actionPerformed(e);
* // .... post-action
* }
* }
* };
*
Actions mapping:
* protected void setActions(){
* // get existing actions:
* kitUpAction=getActionMap().get(DefaultEditorKit.upAction);
*
* // actionKeys init:
* java.util.HashMap actionKeys=Actions.getActionKeys(this);
*
* // build new ActionMap:
* ActionMap am=new ActionMap();
* am.setParent(getActionMap());
* setActionMap(am);
*
* //Actions derived to trace (only for debug purposes):
* Actions.mapTraceAction(this, actionKeys, DefaultEditorKit.readOnlyAction);
* Actions.mapTraceAction(this, actionKeys, DefaultEditorKit.writableAction);
* Actions.mapTraceAction(this, actionKeys, "requestFocus");
* Actions.mapTraceAction(this, actionKeys, "toggle-componentOrientation");
*
* //Actions to disable:
* Actions.mapNullAction(this, actionKeys, DefaultEditorKit.beepAction);
*
* //New actions:
* Actions.mapAction(this, actionKeys, beginAction);
* Actions.mapAction(this, actionKeys, upAction);
*
* //Original actions mapped to other methods:
* Actions.mapAction(this, actionKeys, beginAction, DefaultEditorKit.previousWordAction);
* Actions.mapAction(this, actionKeys, prevTargetAction, DefaultEditorKit.beginParagraphAction);
*
* //Uncomment this line only if you want to hide all other actions:
* //am.setParent(null);
* //
*
* // Assign keystrokes to a new action:
* getInputMap().put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_TAB, java.awt.Event.SHIFT_MASK), prevTargetAction.getValue(Action.NAME));
* }
*
*
* @author Francesc Busquets (fbusquets@xtec.cat)
* @version 13.08.08
*/
public abstract class Actions {
/**
* 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