Java Swing Key Action augmentList(Action[] list1, Action[] list2)

Here you can find the source of augmentList(Action[] list1, Action[] list2)

Description

Takes one list of commands and augments it with another list of commands.

License

Apache License

Parameter

Parameter Description
list1 the first list, may be empty but not <code>null</code>
list2 the second list, may be empty but not <code>null</code>

Return

the augmented list

Declaration

public static final Action[] augmentList(Action[] list1, Action[] list2) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.swing.*;

import java.util.*;

public class Main {
    /**/*from  w  w  w  .  jav  a2s.com*/
     * Takes one list of
     * commands and augments it with another list
     * of commands.  The second list takes precedence
     * over the first list; that is, when both lists
     * contain a command with the same name, the command
     * from the second list is used.
     *
     * @param list1 the first list, may be empty but not
     *              <code>null</code>
     * @param list2 the second list, may be empty but not
     *              <code>null</code>
     * @return the augmented list
     */
    public static final Action[] augmentList(Action[] list1, Action[] list2) {
        Map<String, Action> h = new HashMap<String, Action>();
        addActionsToMap(list1, h);
        addActionsToMap(list2, h);
        Action[] actions = mapToActionList(h);
        return actions;
    }

    /**
     * add a col;lections of actions to a Map where the key is the action name
     *
     * @param list1 non-noll list of actions
     * @param h     non-null map as above
     */
    public static void addActionsToMap(Action[] list1, Map<String, Action> h) {
        for (int i = 0; i < list1.length; i++) {
            Action a = list1[i];
            String value = (String) a.getValue(Action.NAME);
            h.put((value != null ? value : ""), a);
        }
    }

    public static Action[] mapToActionList(Map<String, Action> h) {
        Action[] actions = new Action[h.size()];
        h.values().toArray(actions);
        return actions;
    }
}

Related

  1. addEnterKey(JComponent control)
  2. addEscKeyAction(javax.swing.JComponent component, javax.swing.InputMap inputMap, javax.swing.Action action)
  3. addHotKey(int key, JComponent to, String actionName, Action action)
  4. addIcons(Action action, String[][] iconRoles)
  5. addStrokeToName(Action action)
  6. dispatchEvent(final KeyEvent ke, final Component comp)
  7. escapeKeyAction(JComponent component, javax.swing.AbstractAction abstractAction)
  8. formatKeyStroke(final KeyStroke keyStroke)
  9. getActionID(final Action a)