Java Swing Menu createActionMenu(JTextComponent text, boolean includeModifying)

Here you can find the source of createActionMenu(JTextComponent text, boolean includeModifying)

Description

Creates a menu for a text component with select, copy and optionally cut and paste actions.

License

Open Source License

Parameter

Parameter Description
text the component to create the popup for.
includeModifying whether or not to include the modifying actions (cut and paste) in the popup.

Return

a popup menu that has actions for the component.

Declaration

public static JPopupMenu createActionMenu(JTextComponent text, boolean includeModifying) 

Method Source Code

//package com.java2s;
/*//from  w w w. j a va  2s  .c om
 * SwingUtilities.java (Class: com.madphysicist.tools.swing.SwingUtilities)
 *
 * Mad Physicist JTools Project (Swing Utilities)
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013 by Joseph Fox-Rabinovitz
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.HashMap;
import java.util.Map;
import javax.swing.Action;

import javax.swing.JPopupMenu;

import javax.swing.text.DefaultEditorKit;
import javax.swing.text.JTextComponent;

public class Main {
    /**
     * Creates a menu for a text component with select, copy and optionally cut
     * and paste actions.
     *
     * @param text the component to create the popup for.
     * @param includeModifying whether or not to include the modifying actions
     * (cut and paste) in the popup.
     * @return a popup menu that has actions for the component.
     * @since 1.1.2
     */
    public static JPopupMenu createActionMenu(JTextComponent text, boolean includeModifying) {
        JPopupMenu menu = new JPopupMenu();
        Action[] actions = text.getActions();
        Map<String, Action> actionMap = new HashMap<>(actions.length);
        for (Action action : actions) {
            actionMap.put((String) action.getValue(Action.NAME), action);
        }
        menu.add(setActionName("Select All", actionMap.get(DefaultEditorKit.selectAllAction)));
        menu.add(setActionName("Copy", actionMap.get(DefaultEditorKit.copyAction)));
        if (includeModifying) {
            menu.add(setActionName("Cut", actionMap.get(DefaultEditorKit.cutAction)));
            menu.add(setActionName("Paste", actionMap.get(DefaultEditorKit.pasteAction)));
        }
        return menu;
    }

    /**
     * Changes the name but not the command of an action. Use this method with
     * care.
     *
     * @param name the new name to assign to the action.
     * @param action the action to change the name of.
     * @return the input action.
     * @since 1.1.2
     */
    private static Action setActionName(String name, Action action) {
        action.putValue(Action.NAME, name);
        return action;
    }
}

Related

  1. addRCMenuMouseListener(final JTextComponent text)
  2. appendMenuSubElements(MenuElement element, StringBuilder builder, String indent)
  3. applyContextMenuFontRecurse(MenuElement item, Font font)
  4. buildManualsMenu(File appDir)
  5. constructViewMenu(ActionListener act, boolean showUMLOption, boolean showShortenedSourceOption, boolean showJavadocsOption, boolean showSourceOption)
  6. createMenu(final String label, final String mnemonic, final String accessibleDescription)
  7. createMenu(final String name, final int mnemonic)
  8. createMenu(final String text)
  9. createMenu(final String text, final char mnemonic)