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).

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  
svoid componentPopupMenu(final JComponent component, IVF1<JPopupMenu> menuMaker) {
15  
  componentPopupMenu(component, (O) menuMaker);
16  
}
17  
18  
// menuMaker = voidfunc(JPopupMenu)
19  
svoid componentPopupMenu(final JComponent component, fO menuMaker) {
20  
  if (component == null || menuMaker == null) ret;
21  
  swing {
22  
    O adapter = componentPopupMenu_initForComponent(component);
23  
    ((L) _get(adapter, 'maker)).add(menuMaker);
24  
  }
25  
}
26  
27  
sO componentPopupMenu_initForComponent(final JComponent component) {
28  
  ret component == null ? null : swing(func {
29  
    componentPopupMenu_init();
30  
    O adapter = findComponentPopupMenuListener_gen(component);
31  
    if (adapter == null) {
32  
      new componentPopupMenu_Adapter a;
33  
      //addMouseListener_inFront(component, a);
34  
      component.addMouseListener(a);
35  
      adapter = a;
36  
    }
37  
    ret adapter;
38  
  });
39  
}
40  
41  
sclass componentPopupMenu_Adapter extends MouseAdapter {
42  
  new L maker;
43  
  bool internalFrameLeftButtonMagic;
44  
  bool allowScrolling = true;
45  
  
46  
  Point pressedAt;
47  
48  
  public void mousePressed(MouseEvent e) {
49  
    displayMenu(e);
50  
    pressedAt = internalFrameLeftButtonMagic && e.getClickCount() == 1 && internalFrameActive(e.getComponent()) ? e.getLocationOnScreen() : null;
51  
  }
52  
  
53  
  public void mouseReleased(MouseEvent e) {
54  
    // TODO: show a little less often on left mouse click
55  
    if (internalFrameLeftButtonMagic && eq(pressedAt, e.getLocationOnScreen()))
56  
      displayMenu2(e);
57  
    else
58  
      displayMenu(e);
59  
  }
60  
61  
  void displayMenu(MouseEvent e) {
62  
    if (e.getSource() instanceof JInternalFrame) ret;
63  
    if (e.isPopupTrigger()) displayMenu2(e);
64  
  }
65  
66  
  void populate(JPopupMenu menu, MouseEvent e) {  
67  
    temp tempSetTL(mouseEvent, e);
68  
    for (O menuMaker : maker)
69  
      pcallF(menuMaker, menu);
70  
    vmBus_send('showingPopupMenu, e.getComponent(), menu);
71  
  }
72  
    
73  
  void displayMenu2(MouseEvent e) {
74  
    new PopupMenuMaker(e, menu -> populate(menu, e))
75  
      .allowScrolling(allowScrolling).run();
76  
  }
77  
}
78  
79  
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: 839 / 1036
Version history: 46 change(s)
Referenced in: [show references]