// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Objects;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.JTextComponent;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());
    JTextField pf1 = new JTextField(30);
    pf1.setComponentPopupMenu(new TextFieldPopupMenu());

    JTextField pf2 = new JTextField(30);
    pf2.setComponentPopupMenu(new TextFieldPopupMenu());

    Box panel = Box.createVerticalBox();
    panel.setBorder(BorderFactory.createTitledBorder("E-mail Address"));
    panel.add(pf1);
    panel.add(Box.createVerticalStrut(5));
    panel.add(new JLabel("Please enter your email address twice for confirmation:"));
    panel.add(pf2);
    panel.add(Box.createVerticalStrut(5));
    setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    add(panel, BorderLayout.NORTH);
    add(new JScrollPane(new JTextArea("Dummy")));
    setPreferredSize(new Dimension(320, 240));
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(MainPanel::createAndShowGui);
  }

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
    JFrame frame = new JFrame("@title@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.setResizable(false);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

class TextFieldPopupMenu extends JPopupMenu {
  protected TextFieldPopupMenu() {
    super();
    add(new DefaultEditorKit.CutAction());
    add(new DefaultEditorKit.CopyAction());
    add(new DefaultEditorKit.PasteAction());
    add(new AbstractAction("delete") {
      @Override public void actionPerformed(ActionEvent e) {
        Component c = getInvoker();
        if (c instanceof JTextComponent) {
          ((JTextComponent) c).replaceSelection(null);
        }
      }
    });
    addSeparator();
    add(new AbstractAction("cut2") {
      @Override public void actionPerformed(ActionEvent e) {
        Component c = getInvoker();
        if (c instanceof JTextComponent) {
          ((JTextComponent) c).cut();
        }
      }
    });
  }

  @Override public void show(Component c, int x, int y) {
    if (c instanceof JTextComponent) {
      JTextComponent tc = (JTextComponent) c;
      boolean hasSelectedText = Objects.nonNull(tc.getSelectedText());
      for (MenuElement menuElement: getSubElements()) {
        Component m = menuElement.getComponent();
        if (m instanceof JMenuItem && ((JMenuItem) m).getAction() instanceof DefaultEditorKit.PasteAction) {
          continue;
        }
        m.setEnabled(hasSelectedText);
      }
      super.show(c, x, y);
    }
  }
}

@@ -117,43 +117,8 @@
 .*;%0A
-import java.awt.event.ActionEvent;%0A
 impo
@@ -2021,102 +2021,43 @@
 add(
-new AbstractAction(%22delete%22) %7B%0A      @Override public void actionPerformed(ActionEvent e)
+%22delete%22).addActionListener(e -%3E
  %7B%0A
-  
     
@@ -2084,34 +2084,32 @@
 nvoker();%0A      
-  
 if (c instanceof
@@ -2127,34 +2127,32 @@
 nent) %7B%0A        
- 
- 
 ((JTextComponent
@@ -2180,26 +2180,16 @@
 (null);%0A
-        %7D%0A
       %7D%0A
@@ -2228,100 +2228,41 @@
 add(
-new AbstractAction(%22cut2%22) %7B%0A      @Override public void actionPerformed(ActionEvent e)
+%22cut2%22).addActionListener(e -%3E
  %7B%0A
-  
     
@@ -2293,26 +2293,24 @@
 er();%0A      
-  
 if (c instan
@@ -2336,26 +2336,24 @@
 ) %7B%0A        
- 
- 
 ((JTextCompo
@@ -2368,26 +2368,16 @@
 .cut();%0A
-        %7D%0A
       %7D%0A

use lambda instead of new AbstractAction(...)