Example usage for javax.swing Action getValue

List of usage examples for javax.swing Action getValue

Introduction

In this page you can find the example usage for javax.swing Action getValue.

Prototype

public Object getValue(String key);

Source Link

Document

Gets one of this object's properties using the associated key.

Usage

From source file:Main.java

/**
 * Binds an action to a component. Uses the <code>Action.ACTION_COMMAND_KEY</code> and <code>Action.ACCELERATOR_KEY</code> action
 * properties./*www  .j av  a 2s. c o m*/
 * 
 * @param component
 *            the component.
 * @param action
 *            the action to bind.
 */
public static void bindAction(JComponent component, Action action) {
    component.getInputMap().put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY),
            action.getValue(Action.ACTION_COMMAND_KEY));
    component.getActionMap().put(action.getValue(Action.ACTION_COMMAND_KEY), action);
}

From source file:Main.java

public static HashMap<Object, Action> createActionTable(JTextComponent textComponent) {
    HashMap<Object, Action> actions = new HashMap<>();
    Action[] actionsArray = textComponent.getActions();
    for (int i = 0; i < actionsArray.length; i++) {
        Action a = actionsArray[i];
        actions.put(a.getValue(Action.NAME), a);
    }//www . j a v a2 s  .c o  m
    return actions;
}

From source file:Main.java

/**
 * //www  .j ava2 s .  c om
 * @param comp
 * @param action
 * @param condition - see {@link JComponent}
 * (WHEN_FOCUSED, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,WHEN_IN_FOCUSED_WINDOW)
 */
public static void registerKeyBoardAction(JComponent comp, Action action, int condition) {
    comp.getInputMap(condition).put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY),
            action.getValue(Action.NAME));
    comp.getActionMap().put(action.getValue(Action.NAME), action);
}

From source file:Main.java

public static void registerKeyBoardAction(JComponent comp, Action action, KeyStroke stroke) {
    comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, action.getValue(Action.NAME));
    comp.getActionMap().put(action.getValue(Action.NAME), action);
}

From source file:Main.java

/**
 * Search the given text component's list of actions for an action with the given name.
 * Returns {@code null} if no such action is found.  See {@link DefaultEditorKit} for a list of
 * action name constants.//from ww w. j a va2 s .  co  m
 */
public static Action getAction(JTextComponent component, String actionName) {
    for (Action a : component.getActions()) {
        if (actionName.equals(a.getValue(Action.NAME))) {
            return a;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Creates a suitable menu item.//from   www . j  a  va 2  s.  co m
 * 
 * @param action
 * @return
 */
public static JMenuItem createMenuItem(Action action) {
    final JMenuItem mi;
    final Boolean selected = (Boolean) action.getValue(Action.SELECTED_KEY);
    if (selected != null) {
        mi = new JCheckBoxMenuItem(action);
    } else {
        mi = new JMenuItem(action);
    }
    mi.setHorizontalTextPosition(JButton.TRAILING);
    mi.setVerticalTextPosition(JButton.CENTER);
    return mi;
}

From source file:Main.java

/**
 * Registers the keystroke of the given action as "command" of the given
 * component.//w  ww  .j  a v  a 2  s  .c om
 * <p>
 * This code is based on the Sulky-tools, found at
 * &lt;http://github.com/huxi/sulky&gt;.
 * </p>
 * 
 * @param aComponent
 *          the component that should react on the keystroke, cannot be
 *          <code>null</code>;
 * @param aAction
 *          the action of the keystroke, cannot be <code>null</code>;
 * @param aCommandName
 *          the name of the command to register the keystore under.
 */
public static void registerKeystroke(final JComponent aComponent, final Action aAction,
        final String aCommandName) {
    final KeyStroke keyStroke = (KeyStroke) aAction.getValue(Action.ACCELERATOR_KEY);
    if (keyStroke == null) {
        return;
    }

    InputMap inputMap = aComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = aComponent.getActionMap();
    inputMap.put(keyStroke, aCommandName);
    actionMap.put(aCommandName, aAction);

    inputMap = aComponent.getInputMap(JComponent.WHEN_FOCUSED);
    Object value = inputMap.get(keyStroke);
    if (value != null) {
        inputMap.put(keyStroke, aCommandName);
    }

    inputMap = aComponent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    value = inputMap.get(keyStroke);
    if (value != null) {
        inputMap.put(keyStroke, aCommandName);
    }
}

From source file:Main.java

/**
 * Create a map view of {@code component.getActions()}, where the keys are the action
 * names.  See {@link DefaultEditorKit} for a list of action name constants.
 *///from w w  w.  j  ava  2s .  c om
public static Map<String, Action> getActions(JTextComponent component) {
    Map<String, Action> result = new HashMap<String, Action>();
    for (Action a : component.getActions()) {
        // Documentation for Action.NAME asserts that it will always be a String
        result.put((String) a.getValue(Action.NAME), a);
    }
    return result;
}

From source file:Main.java

public static boolean invoke(Action action, Object source) {
    if (action == null || !action.isEnabled()) {
        return false;
    }// w ww. java 2s. c  o  m
    ActionEvent evt = new ActionEvent(source, ActionEvent.ACTION_PERFORMED,
            (String) action.getValue(Action.ACTION_COMMAND_KEY), 0);
    action.actionPerformed(evt);
    return true;
}

From source file:Main.java

/**
 * Adds a component action./*w  ww. jav a  2  s. co m*/
 *
 * @param component
 *            The compoennt to add the action to
 * @param action
 *            The action to add
 */

public static void addComponentAction(final JComponent component, final Action action) {
    final InputMap imap = component
            .getInputMap(component.isFocusable() ? JComponent.WHEN_FOCUSED : JComponent.WHEN_IN_FOCUSED_WINDOW);
    final ActionMap amap = component.getActionMap();
    final KeyStroke ks = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
    imap.put(ks, action.getValue(Action.NAME));
    amap.put(action.getValue(Action.NAME), action);
}