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

86
LINES

< > BotCompany Repo | #1034571 // componentPopupMenu - backup

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

Author comment

Began life as a copy of #1005719

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034571
Snippet name: componentPopupMenu - backup
Eternal ID of this version: #1034571/1
Text MD5: 7f1ef023c2ea42d4bb97dc64aa9d0f14
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-16 01:37:08
Source code size: 2563 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 64 / 78
Referenced in: [show references]