class ContextMenuMouseListener extends MouseAdapter {

    private static final Logger LOG = Logger.getLogger(ContextMenuMouseListener.class.getName());

    private final JPopupMenu _popup;
    private final Action _cutAction;
    private final Action _copyAction;
    private final Action _pasteAction;
    private final Action _undoAction;
    private final Action _selectAllAction;
    private JTextComponent _textComponent;
    private String _savedString;
    private _Actions _lastActionSelected;

    public ContextMenuMouseListener() {
        _savedString = "";
        _popup = new JPopupMenu();
        _undoAction = new AbstractAction("Undo") {

            @Override
            public void actionPerformed(ActionEvent ae) {
                _textComponent.setText("");
                _textComponent.replaceSelection(_savedString);
                _lastActionSelected = _Actions.UNDO;
            }
        };

        _popup.add(_undoAction);
        _popup.addSeparator();
        _cutAction = new AbstractAction("Cut") {

            @Override
            public void actionPerformed(ActionEvent ae) {
                _lastActionSelected = _Actions.CUT;
                _savedString = _textComponent.getText();
                _textComponent.cut();
            }
        };

        _popup.add(_cutAction);

        _copyAction = new AbstractAction("Copy") {

            @Override
            public void actionPerformed(ActionEvent ae) {
                _lastActionSelected = _Actions.COPY;
                _textComponent.copy();
            }
        };

        _popup.add(_copyAction);

        _pasteAction = new AbstractAction("Paste") {

            @Override
            public void actionPerformed(ActionEvent ae) {
                _lastActionSelected = _Actions.PASTE;
                _savedString = _textComponent.getText();
                _textComponent.paste();
            }
        };

        _popup.add(_pasteAction);
        _popup.addSeparator();

        _selectAllAction = new AbstractAction("Select All") {

            @Override
            public void actionPerformed(ActionEvent ae) {
                _lastActionSelected = _Actions.SELECT_ALL;
                _textComponent.selectAll();
            }
        };

        _popup.add(_selectAllAction);
    }

    @Override
    public void mouseClicked(MouseEvent e) {<FILL_FUNCTION_BODY>}

    private enum _Actions {
        UNDO, CUT, COPY, PASTE, SELECT_ALL
    }
}

        if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
            if (!(e.getSource() instanceof JTextComponent)) {

                return;
            }

            _textComponent = (JTextComponent) e.getSource();
            _textComponent.requestFocus();

            boolean enabled = _textComponent.isEnabled();
            boolean editable = _textComponent.isEditable();
            boolean nonempty = !(_textComponent.getText() == null || _textComponent.getText().isEmpty());
            boolean marked = _textComponent.getSelectedText() != null;

            boolean pasteAvailable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).isDataFlavorSupported(DataFlavor.stringFlavor);

            _undoAction.setEnabled(enabled && editable && (_lastActionSelected == _Actions.CUT || _lastActionSelected == _Actions.PASTE));
            _cutAction.setEnabled(enabled && editable && marked);
            _copyAction.setEnabled(enabled && marked);
            _pasteAction.setEnabled(enabled && editable && pasteAvailable);
            _selectAllAction.setEnabled(enabled && nonempty);

            int nx = e.getX();

            if (nx > 500) {
                nx -= _popup.getSize().width;
            }

            _popup.show(e.getComponent(), nx, e.getY() - _popup.getSize().height);
        }
    
<methods>public void mouseClicked(java.awt.event.MouseEvent) ,public void mouseDragged(java.awt.event.MouseEvent) ,public void mouseEntered(java.awt.event.MouseEvent) ,public void mouseExited(java.awt.event.MouseEvent) ,public void mouseMoved(java.awt.event.MouseEvent) ,public void mousePressed(java.awt.event.MouseEvent) ,public void mouseReleased(java.awt.event.MouseEvent) ,public void mouseWheelMoved(java.awt.event.MouseWheelEvent) <variables>