package example;
//-*- mode:java; encoding:utf8n; coding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class MainPanel extends JPanel{
    public MainPanel() {
        super(new BorderLayout());
        JTextArea textArea = new JTextArea("ComponentPopupMenu Test\naaaaaaaaaaa\nbbbbbbbbbbbbbb\ncccccccccccccc");
        textArea.setComponentPopupMenu(new TextComponentPopupMenu());
        add(new JScrollPane(textArea));
        setPreferredSize(new Dimension(320, 240));
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        try{
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }catch(Exception e) {
            e.printStackTrace();
        }
        JFrame frame = new JFrame("@title@");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class TextComponentPopupMenu extends JPopupMenu {
    private final Action cutAction = new DefaultEditorKit.CutAction();
    private final Action copyAction = new DefaultEditorKit.CopyAction();
    private final Action pasteAction = new DefaultEditorKit.PasteAction();
    private final Action deleteAction;
    private final Action selectAllAction;
    public TextComponentPopupMenu() {
        super();

//         addPopupMenuListener(new PopupMenuListener() {
//             public void popupMenuCanceled(PopupMenuEvent e) {}
//             public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
//             public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
//                 boolean flg = textArea.getSelectedText()!=null;
//                 cutAction.setEnabled(flg);
//                 copyAction.setEnabled(flg);
//                 deleteAction.setEnabled(flg);
//             }
//         });

        add(cutAction);
        add(copyAction);
        add(pasteAction);
        addSeparator();
        add(deleteAction = new AbstractAction("delete") {
            @Override public void actionPerformed(ActionEvent e) {
                ((JTextComponent)getInvoker()).replaceSelection(null);
            }
        });
        addSeparator();
        add(selectAllAction = new AbstractAction("select all") {
            @Override public void actionPerformed(ActionEvent e) {
                ((JTextComponent)getInvoker()).selectAll();
            }
        });
    }
    @Override public void show(Component c, int x, int y) {
        JTextComponent textArea = (JTextComponent)c;
        boolean flg = textArea.getSelectedText()!=null;
        cutAction.setEnabled(flg);
        copyAction.setEnabled(flg);
        deleteAction.setEnabled(flg);
        super.show(c, x, y);
    }
}

@@ -609,16 +609,1889 @@
 %0A    %7D%0A%0A
+//     //Another way:%0A//     private static JPopupMenu makePopupMenu() %7B%0A//         JPopupMenu popup = new JPopupMenu();%0A//         final Action cutAction = new DefaultEditorKit.CutAction();%0A//         final Action copyAction = new DefaultEditorKit.CopyAction();%0A//         final Action pasteAction = new DefaultEditorKit.PasteAction();%0A//         final Action deleteAction = new AbstractAction(%22delete%22) %7B%0A//             @Override public void actionPerformed(ActionEvent e) %7B%0A//                 JPopupMenu p = (JPopupMenu)e.getSource();%0A//                 ((JTextComponent)p.getInvoker()).replaceSelection(null);%0A//             %7D%0A//         %7D;%0A//         final Action selectAllAction = new AbstractAction(%22select all%22) %7B%0A//             @Override public void actionPerformed(ActionEvent e) %7B%0A//                 JPopupMenu p = (JPopupMenu)e.getSource();%0A//                 ((JTextComponent)p.getInvoker()).selectAll();%0A//             %7D%0A//         %7D;%0A//         popup.add(cutAction);%0A//         popup.add(copyAction);%0A//         popup.add(pasteAction);%0A//         popup.addSeparator();%0A//         popup.add(deleteAction);%0A//         popup.addSeparator();%0A//         popup.add(selectAllAction);%0A//         popup.addPopupMenuListener(new PopupMenuListener() %7B%0A//             public void popupMenuCanceled(PopupMenuEvent e) %7B%7D%0A//             public void popupMenuWillBecomeInvisible(PopupMenuEvent e) %7B%7D%0A//             public void popupMenuWillBecomeVisible(PopupMenuEvent e) %7B%0A//                 JPopupMenu p = (JPopupMenu)e.getSource();%0A//                 JTextComponent c = (JTextComponent)p.getInvoker();%0A//                 boolean flg = c.getSelectedText()!=null;%0A//                 cutAction.setEnabled(flg);%0A//                 copyAction.setEnabled(flg);%0A//                 deleteAction.setEnabled(flg);%0A//             %7D%0A//         %7D);%0A//         return popup;%0A//     %7D%0A%0A
     publ
@@ -3556,24 +3556,24 @@
 pupMenu() %7B%0A
+
         supe
@@ -3581,526 +3581,8 @@
 ();%0A
-%0A//         addPopupMenuListener(new PopupMenuListener() %7B%0A//             public void popupMenuCanceled(PopupMenuEvent e) %7B%7D%0A//             public void popupMenuWillBecomeInvisible(PopupMenuEvent e) %7B%7D%0A//             public void popupMenuWillBecomeVisible(PopupMenuEvent e) %7B%0A//                 boolean flg = textArea.getSelectedText()!=null;%0A//                 cutAction.setEnabled(flg);%0A//                 copyAction.setEnabled(flg);%0A//                 deleteAction.setEnabled(flg);%0A//             %7D%0A//         %7D);%0A%0A
     

add comment: addPopupMenuListener