scope componentPopupMenu. static ThreadLocal #mouseEvent; svoid componentPopupMenu_init { swing { if (mouseEvent == null) mouseEvent = (ThreadLocal) vm_generalMap_get('mouseEvent); if (mouseEvent == null) vm_generalMap_put(componentPopupMenu_mouseEvent := mouseEvent = new ThreadLocal); } } svoid componentPopupMenu(final JComponent component, IVF1 menuMaker) { componentPopupMenu(component, (O) menuMaker); } // menuMaker = voidfunc(JPopupMenu) svoid componentPopupMenu(final JComponent component, fO menuMaker) { if (component == null || menuMaker == null) ret; swing { O adapter = componentPopupMenu_initForComponent(component); ((L) _get(adapter, 'maker)).add(menuMaker); } } sO componentPopupMenu_initForComponent(final JComponent component) { ret component == null ? null : swing(func { componentPopupMenu_init(); O adapter = findComponentPopupMenuListener_gen(component); if (adapter == null) { new componentPopupMenu_Adapter a; //addMouseListener_inFront(component, a); component.addMouseListener(a); adapter = a; } ret adapter; }); } sclass componentPopupMenu_Adapter extends MouseAdapter { new L maker; bool internalFrameLeftButtonMagic; bool allowScrolling = true; Point pressedAt; public void mousePressed(MouseEvent e) { displayMenu(e); pressedAt = internalFrameLeftButtonMagic && e.getClickCount() == 1 && internalFrameActive(e.getComponent()) ? e.getLocationOnScreen() : null; } public void mouseReleased(MouseEvent e) { // TODO: show a little less often on left mouse click if (internalFrameLeftButtonMagic && eq(pressedAt, e.getLocationOnScreen())) displayMenu2(e); else displayMenu(e); } void displayMenu(MouseEvent e) { if (e.getSource() instanceof JInternalFrame) ret; if (e.isPopupTrigger()) displayMenu2(e); } void populate(JPopupMenu menu, MouseEvent e) { temp tempSetTL(mouseEvent, e); for (O menuMaker : maker) pcallF(menuMaker, menu); vmBus_send('showingPopupMenu, e.getComponent(), menu); } void displayMenu2(MouseEvent e) { new PopupMenuMaker(e, menu -> populate(menu, e)) .allowScrolling(allowScrolling).run(); } } end scope