/*
* File : Actions.java
* Created : 18-dec-2001 10:48
* By : fbusquets
*
* JClic - Authoring and playing system for educational activities
*
* Copyright (C) 2000 - 2005 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.Iterator;
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.net)
* @version 1.0.0.0
*/
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 HashMap getActionKeys(JComponent jc){
HashMap result=new HashMap();
ActionMap am=jc.getActionMap();
Object[] amKeys=am.allKeys();
for(int i=0; i> "+am.get(value[i]));
}
}
public static void mapAction(JComponent jc, HashMap actionKeys, Action act){
if(actionKeys==null)
actionKeys=getActionKeys(jc);
ActionMap am=jc.getActionMap();
Object name=act.getValue(Action.NAME);
Object[] keys=(Object[])actionKeys.get(name);
boolean replaced=false;
if(keys!=null)
for(int i=0; i