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

public final class MainPanel extends JPanel {
    private MainPanel() {
        super(new BorderLayout());
        JTree tree = new JTree();

//         UIManager.put("PopupMenu.consumeEventOnClose", Boolean.FALSE);
//         tree.addMouseListener(new MouseAdapter() {
//             @Override public void mousePressed(MouseEvent e) {
//                 JTree tree = (JTree) e.getSource();
//                 TreePath path = tree.getPathForLocation(e.getX(), e.getY());
//                 if (!tree.getSelectionModel().isPathSelected(path)) {
//                     tree.getSelectionModel().setSelectionPath(path);
//                 }
//             }
//         });

        tree.setCellEditor(new DefaultTreeCellEditor(tree, (DefaultTreeCellRenderer) tree.getCellRenderer()) {
//             @Override protected boolean shouldStartEditingTimer(EventObject e) {
//                 return false;
//             }
//             @Override protected boolean canEditImmediately(EventObject e) {
//                 //((MouseEvent) e).getClickCount() - 2 >= 0
//                 return !(e instanceof MouseEvent) && super.canEditImmediately(e);
//             }
            @Override public boolean isCellEditable(EventObject e) {
                return !(e instanceof MouseEvent) && super.isCellEditable(e);
            }
        });
        tree.setEditable(true);
        tree.setComponentPopupMenu(new TreePopupMenu());

        add(new JScrollPane(tree));
        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 (ClassNotFoundException | InstantiationException
               | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.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 TreePopupMenu extends JPopupMenu {
    protected TreePath path;
    protected final JTextField textField = new JTextField();
    private final Action editAction = new AbstractAction("Edit") {
        @Override public void actionPerformed(ActionEvent e) {
            if (Objects.nonNull(path)) {
                JTree tree = (JTree) getInvoker();
                tree.startEditingAtPath(path);
            }
        }
    };
    private final Action editDialogAction = new AbstractAction("Edit Dialog") {
        @Override public void actionPerformed(ActionEvent e) {
            if (Objects.isNull(path)) {
                return;
            }
            Object node = path.getLastPathComponent();
            if (node instanceof DefaultMutableTreeNode) {
                DefaultMutableTreeNode leaf = (DefaultMutableTreeNode) node;
                textField.setText(leaf.getUserObject().toString());
                JTree tree = (JTree) getInvoker();
                int result = JOptionPane.showConfirmDialog(tree, textField, "Rename", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
                if (result == JOptionPane.OK_OPTION) {
                    Optional.ofNullable(textField.getText().trim())
                      .filter(str -> !str.isEmpty())
                      .ifPresent(str -> ((DefaultTreeModel) tree.getModel()).valueForPathChanged(path, str));
//                     String str = textField.getText().trim();
//                     if (!str.isEmpty()) {
//                         ((DefaultTreeModel) tree.getModel()).valueForPathChanged(path, str);
//                     }
                }
            }
        }
    };
    protected TreePopupMenu() {
        super();
        textField.addAncestorListener(new AncestorListener() {
            @Override public void ancestorAdded(AncestorEvent e) {
                textField.requestFocusInWindow();
            }
            @Override public void ancestorMoved(AncestorEvent e)   { /* not needed */ }
            @Override public void ancestorRemoved(AncestorEvent e) { /* not needed */ }
        });
        add(editAction);
        add(editDialogAction);
        add(new JMenuItem("dummy"));
    }
    @Override public void show(Component c, int x, int y) {
        if (c instanceof JTree) {
            JTree tree = (JTree) c;
            ////Test:
            //path = tree.getPathForLocation(x, y);
            //if (tree.getSelectionModel().isPathSelected(path)) {
            //    super.show(c, x, y);
            //}
            TreePath[] tsp = tree.getSelectionPaths();
            path = tree.getPathForLocation(x, y); //Test: path = tree.getClosestPathForLocation(x, y);
            boolean isEditable = tsp.length == 1 && tsp[0].equals(path);
            //Test: if (Objects.nonNull(path) && Arrays.asList(tsp).contains(path)) {
            editAction.setEnabled(isEditable);
            editDialogAction.setEnabled(isEditable);
            super.show(c, x, y);
        }
    }
}

@@ -2696,129 +2696,592 @@
   pr
-ivate final Action editAction = new AbstractAction(%22Edit%22) %7B%0A        @Override public void actionPerformed(ActionEvent e)
+otected final JMenuItem editItem;%0A    protected final JMenuItem editDialogItem;%0A%0A    protected TreePopupMenu() %7B%0A        super();%0A        textField.addAncestorListener(new AncestorListener() %7B%0A            @Override public void ancestorAdded(AncestorEvent e) %7B%0A                textField.requestFocusInWindow();%0A            %7D%0A            @Override public void ancestorMoved(AncestorEvent e)   %7B /* not needed */ %7D%0A            @Override public void ancestorRemoved(AncestorEvent e) %7B /* not needed */ %7D%0A        %7D);%0A%0A        editItem = add(%22Edit%22);%0A        editItem.addActionListener(e -%3E
  %7B%0A 
@@ -3445,78 +3445,40 @@
    %7D
-%0A    %7D
+)
 ;%0A
+%0A
     
-private final Action editDialogAction = new AbstractAction
+    editDialogItem = add
 (%22Ed
@@ -3488,18 +3488,17 @@
 Dialog%22)
- %7B
+;
 %0A       
@@ -3502,60 +3502,45 @@
     
-@Override public void actionPerformed(ActionEvent e)
+editDialogItem.addActionListener(e -%3E
  %7B%0A 
@@ -4630,528 +4630,23 @@
    %7D
-%0A    %7D;%0A    protected TreePopupMenu() %7B%0A        super();%0A        textField.addAncestorListener(new AncestorListener() %7B%0A            @Override public void ancestorAdded(AncestorEvent e) %7B%0A                textField.requestFocusInWindow();%0A            %7D%0A            @Override public void ancestorMoved(AncestorEvent e)   %7B /* not needed */ %7D%0A            @Override public void ancestorRemoved(AncestorEvent e) %7B /* not needed */ %7D%0A        %7D);%0A        add(editAction);%0A        add(editDialogAction);%0A        add(new JMenuItem
+);%0A%0A        add
 (%22du
@@ -4650,17 +4650,16 @@
 %22dummy%22)
-)
 ;%0A    %7D%0A
@@ -5317,22 +5317,20 @@
     edit
-Action
+Item
 .setEnab
@@ -5368,22 +5368,20 @@
 itDialog
-Action
+Item
 .setEnab

use JPopupMenu#add(String) instead of JPopupMenu#add(Action)