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

49
LINES

< > BotCompany Repo | #1009960 // listPopupMenu - adds to the menu if it already exists

JavaX fragment (include)

sclass listPopupMenu_Maker {
  new L menuMakers;
}

static Map<JList, listPopupMenu_Maker> listPopupMenu_map = new WeakHashMap;

static new ThreadLocal<MouseEvent> listPopupMenu_mouseEvent;
static new ThreadLocal<Bool> listPopupMenu_first;

// menuMaker = voidfunc(JPopupMenu, S item)
static void listPopupMenu(final JList list, fO menuMaker) {
  final bool first = isTrue(getAndClearThreadLocal(listPopupMenu_first));
  awtIfNecessary {
    listPopupMenu_Maker maker = listPopupMenu_map.get(list);
    if (maker == null) {
      listPopupMenu_map.put(list, maker = new listPopupMenu_Maker);
      final listPopupMenu_Maker _maker = maker;
      list.addMouseListener(new MouseAdapter {
        public void mousePressed(MouseEvent e) { displayMenu(e); }
        public void mouseReleased(MouseEvent e) { displayMenu(e); }
      
        void displayMenu(MouseEvent e) {
          if (e.isPopupTrigger()) {
            new JPopupMenu menu;
            
            int idx = list.locationToIndex(e.getPoint());
            O item = list.getModel().getElementAt(idx);
            list.setSelectedIndex(idx);
            int emptyCount = menu.getComponentCount();
            
            listPopupMenu_mouseEvent.set(e);
            for (O menuMaker : _maker.menuMakers)
              pcallF(menuMaker, menu, item);
            
            // show menu if any items in it
            if (menu.getComponentCount() == emptyCount) ret;
            
            vmBus_send('showingPopupMenu, list, menu);
            menu.show(e.getComponent(), e.getX(), e.getY());
          }
        }
      });
    }
    if (first)
      maker.menuMakers.add(0, menuMaker);
    else
      maker.menuMakers.add(menuMaker);
  }
}

Author comment

Began life as a copy of #1004446

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1009960
Snippet name: listPopupMenu - adds to the menu if it already exists
Eternal ID of this version: #1009960/5
Text MD5: 98bcc64f78ab64a7be82459bf96ab901
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-01-02 17:09:14
Source code size: 1757 bytes / 49 lines
Pitched / IR pitched: No / No
Views / Downloads: 623 / 617
Version history: 4 change(s)
Referenced in: [show references]