scope componentPopupMenu.

static ThreadLocal<MouseEvent> #mouseEvent;

svoid componentPopupMenu_init {
  swing {
    if (mouseEvent == null)
      mouseEvent = (ThreadLocal<MouseEvent>) vm_generalMap_get('mouseEvent);
    if (mouseEvent == null)
      vm_generalMap_put(componentPopupMenu_mouseEvent := mouseEvent = new ThreadLocal);
  }
}

// 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 JPopupMenu menu;
    int emptyCount = menu.getComponentCount();
    populate(menu, e);
    if (menu.getComponentCount() == emptyCount)
      ret;
    
    if (allowScrolling) {
      menu = new JPopupMenu;
      JMenuScroller scroller = JMenuScroller.setScrollerFor(menu);
      scroller.fillMenu = voidfunc(JPopupMenu m) { populate(m, e) };
    }
    
    menu.show(e.getComponent(), e.getX(), e.getY());
  }
}

end scope