// show popup menu on right click only when a condition is met svoid makePopupMenuConditional(JComponent c, F0 condition) { if (c != null && condition != null) swing { componentPopupMenu_Adapter adapter = firstInstanceOf(c.getMouseListeners(), componentPopupMenu_Adapter); c.removeMouseListener(adapter); c.addMouseListener(new MouseAdapter { public void mousePressed(MouseEvent e) { if (!callF(condition)) adapter.mousePressed(e); } public void mouseReleased(MouseEvent e) { if (!callF(condition)) adapter.mouseReleased(e); } }); } }