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

76
LINES

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

JavaX fragment (include)

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;
      component.addMouseListener(a);
      adapter = a;
    }
    ret adapter;
  });
}

sclass componentPopupMenu_Adapter extends MouseAdapter {
  new L maker;
  bool internalFrameLeftButtonMagic;
  
  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 displayMenu2(MouseEvent e) {
    new JPopupMenu menu;
    int emptyCount = menu.getComponentCount();
    
    temp tempSetTL(mouseEvent, e);
    for (O menuMaker : maker)
      pcallF(menuMaker, menu);
      
    vmBus_send('showingPopupMenu, e.getComponent(), menu);
    
    // show menu if any items in it
    if (menu.getComponentCount() != emptyCount)
      menu.show(e.getComponent(), e.getX(), e.getY());
  }
}

end scope

Author comment

Began life as a copy of #1004446

download  show line numbers  debug dex   

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

No comments. add comment

Snippet ID: #1005719
Snippet name: componentPopupMenu - adds to the popup menu of any JComponent
Eternal ID of this version: #1005719/38
Text MD5: c1e43c447ef53ec23d99f4fd4f75fb02
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-05-02 15:30:14
Source code size: 2233 bytes / 76 lines
Pitched / IR pitched: No / No
Views / Downloads: 479 / 515
Version history: 37 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1005719 & http://1005719.tinybrain.de