Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

79
LINES

< > BotCompany Repo | #1005719 // componentPopupMenu - adds to the popup menu of any JComponent

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (8403L/48K).

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);
  }
}

svoid componentPopupMenu(final JComponent component, IVF1<JPopupMenu> 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

Author comment

Began life as a copy of #1004446

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, ekrmjmnbrukm, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1005719
Snippet name: componentPopupMenu - adds to the popup menu of any JComponent
Eternal ID of this version: #1005719/47
Text MD5: 71943e376ff38c527d0620b48bc53edb
Transpilation MD5: 11ba20de629831c54a9b32bae8d8ba2d
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-30 18:31:20
Source code size: 2374 bytes / 79 lines
Pitched / IR pitched: No / No
Views / Downloads: 652 / 814
Version history: 46 change(s)
Referenced in: [show references]