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

53
LINES

< > BotCompany Repo | #1021794 // componentPopupMenu - adds to the popup menu of any JComponent - v2 without the weak map

JavaX fragment (include)

1  
scope componentPopupMenu.
2  
3  
static ThreadLocal<MouseEvent> #mouseEvent;
4  
5  
svoid componentPopupMenu_init {
6  
  swing {
7  
    if (mouseEvent == null)
8  
      mouseEvent = (ThreadLocal<MouseEvent>) vm_generalMap_get('mouseEvent);
9  
    if (mouseEvent == null)
10  
      vm_generalMap_put(componentPopupMenu_mouseEvent := mouseEvent = new ThreadLocal);
11  
  }
12  
}
13  
14  
// menuMaker = voidfunc(JPopupMenu)
15  
static void componentPopupMenu(final JComponent component, fO menuMaker) {
16  
  componentPopupMenu_init();
17  
  if (component != null && menuMaker != null) swing {
18  
    O adapter = findComponentPopupMenuListener_gen(component);
19  
    if (adapter == null)
20  
      component.addMouseListener(adapter = new componentPopupMenu_Adapter);
21  
    }
22  
    ((L) get(adapter, 'maker)).add(menuMaker);
23  
  }
24  
}
25  
26  
sclass componentPopupMenu_Adapter extends MouseAdapter {
27  
  new L maker; // list of "menu makers" that fill the menu (IVF1<JPopupMenu>)
28  
  
29  
  public void mousePressed(MouseEvent e) { displayMenu(e); }
30  
  public void mouseReleased(MouseEvent e) { displayMenu(e); }
31  
32  
  void displayMenu(MouseEvent e) {
33  
    if (e.getSource() instanceof JInternalFrame) ret;
34  
    if (e.isPopupTrigger()) displayMenu2(e);
35  
  }
36  
    
37  
  void displayMenu2(MouseEvent e) {
38  
    new JPopupMenu menu;
39  
    int emptyCount = menu.getComponentCount();
40  
    
41  
    temp tempSetTL(mouseEvent, e);
42  
    for (O menuMaker : maker)
43  
      pcallF(menuMaker, menu);
44  
      
45  
    vmBus_send('showingPopupMenu, e.getComponent(), menu);
46  
    
47  
    // show menu if any items in it
48  
    if (menu.getComponentCount() != emptyCount)
49  
      menu.show(e.getComponent(), e.getX(), e.getY());
50  
  }
51  
}
52  
53  
end scope

Author comment

Began life as a copy of #1005719

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1021794
Snippet name: componentPopupMenu - adds to the popup menu of any JComponent - v2 without the weak map
Eternal ID of this version: #1021794/3
Text MD5: 7f6a653c3ec24e2c592f5b34db1e1abb
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-14 00:54:59
Source code size: 1652 bytes / 53 lines
Pitched / IR pitched: No / No
Views / Downloads: 202 / 238
Version history: 2 change(s)
Referenced in: [show references]