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

65
LINES

< > BotCompany Repo | #1021795 // componentPopupMenu v1 (old) - adds to the popup menu of any JComponent

JavaX fragment (include)

1  
scope componentPopupMenu.
2  
3  
// TODO: get rid of map (just look for adapter in listeners)
4  
5  
static Map<JComponent, L> componentPopupMenu_map;
6  
static ThreadLocal<MouseEvent> #mouseEvent;
7  
8  
svoid componentPopupMenu_init {
9  
  swing {
10  
    if (componentPopupMenu_map == null)
11  
      componentPopupMenu_map = (Map) vm_generalMap_get('componentPopupMenu_map);
12  
    if (componentPopupMenu_map == null)
13  
      vm_generalMap_put(componentPopupMenu_map := componentPopupMenu_map = newWeakHashMap());
14  
      //componentPopupMenu_map = or((Map<JComponent, L>) getOpt(creator(), 'componentPopupMenu_map), (Map<JComponent, L>) (Map) newWeakHashMap());
15  
    if (mouseEvent == null)
16  
      //mouseEvent = or((ThreadLocal<MouseEvent>) getOpt(creator(), 'componentPopupMenu_mouseEvent), new ThreadLocal);
17  
      mouseEvent = (ThreadLocal<MouseEvent>) vm_generalMap_get('mouseEvent);
18  
    if (mouseEvent == null)
19  
      vm_generalMap_put(componentPopupMenu_mouseEvent := mouseEvent = new ThreadLocal);
20  
  }
21  
}
22  
23  
// menuMaker = voidfunc(JPopupMenu)
24  
static void componentPopupMenu(final JComponent component, fO menuMaker) {
25  
  componentPopupMenu_init();
26  
  if (component != null && menuMaker != null) swing {
27  
    L maker = componentPopupMenu_map.get(component);
28  
    if (maker == null) {
29  
      componentPopupMenu_map.put(component, maker = new L);
30  
      if (!component instanceof JInternalFrame) // lil' hack
31  
        component.addMouseListener(new componentPopupMenu_Adapter(maker));
32  
    }
33  
    maker.add(menuMaker);
34  
  }
35  
}
36  
37  
sclass componentPopupMenu_Adapter extends MouseAdapter {
38  
  L maker;
39  
  
40  
  *(L *maker) {}
41  
  
42  
  public void mousePressed(MouseEvent e) { displayMenu(e); }
43  
  public void mouseReleased(MouseEvent e) { displayMenu(e); }
44  
45  
  void displayMenu(MouseEvent e) {
46  
    if (e.isPopupTrigger()) displayMenu2(e);
47  
  }
48  
    
49  
  void displayMenu2(MouseEvent e) {
50  
    new JPopupMenu menu;
51  
    int emptyCount = menu.getComponentCount();
52  
    
53  
    temp tempSetTL(mouseEvent, e);
54  
    for (O menuMaker : maker)
55  
      pcallF(menuMaker, menu);
56  
      
57  
    vmBus_send('showingPopupMenu, e.getComponent(), menu);
58  
    
59  
    // show menu if any items in it
60  
    if (menu.getComponentCount() != emptyCount)
61  
      menu.show(e.getComponent(), e.getX(), e.getY());
62  
  }
63  
}
64  
65  
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: #1021795
Snippet name: componentPopupMenu v1 (old) - adds to the popup menu of any JComponent
Eternal ID of this version: #1021795/1
Text MD5: 5ef73ab5597cee36aacb55bbaed6d82e
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-27 12:52:57
Source code size: 2285 bytes / 65 lines
Pitched / IR pitched: No / No
Views / Downloads: 286 / 318
Referenced in: [show references]