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)

scope componentPopupMenu.

// TODO: get rid of map (just look for adapter in listeners)

static Map<JComponent, L> componentPopupMenu_map;
static ThreadLocal<MouseEvent> #mouseEvent;

svoid componentPopupMenu_init {
  swing {
    if (componentPopupMenu_map == null)
      componentPopupMenu_map = (Map) vm_generalMap_get('componentPopupMenu_map);
    if (componentPopupMenu_map == null)
      vm_generalMap_put(componentPopupMenu_map := componentPopupMenu_map = newWeakHashMap());
      //componentPopupMenu_map = or((Map<JComponent, L>) getOpt(creator(), 'componentPopupMenu_map), (Map<JComponent, L>) (Map) newWeakHashMap());
    if (mouseEvent == null)
      //mouseEvent = or((ThreadLocal<MouseEvent>) getOpt(creator(), 'componentPopupMenu_mouseEvent), new ThreadLocal);
      mouseEvent = (ThreadLocal<MouseEvent>) vm_generalMap_get('mouseEvent);
    if (mouseEvent == null)
      vm_generalMap_put(componentPopupMenu_mouseEvent := mouseEvent = new ThreadLocal);
  }
}

// menuMaker = voidfunc(JPopupMenu)
static void componentPopupMenu(final JComponent component, fO menuMaker) {
  componentPopupMenu_init();
  if (component != null && menuMaker != null) swing {
    L maker = componentPopupMenu_map.get(component);
    if (maker == null) {
      componentPopupMenu_map.put(component, maker = new L);
      if (!component instanceof JInternalFrame) // lil' hack
        component.addMouseListener(new componentPopupMenu_Adapter(maker));
    }
    maker.add(menuMaker);
  }
}

sclass componentPopupMenu_Adapter extends MouseAdapter {
  L maker;
  
  *(L *maker) {}
  
  public void mousePressed(MouseEvent e) { displayMenu(e); }
  public void mouseReleased(MouseEvent e) { displayMenu(e); }

  void displayMenu(MouseEvent e) {
    if (e.isPopupTrigger()) displayMenu2(e);
  }
    
  void displayMenu2(MouseEvent e) {
    new JPopupMenu menu;
    int emptyCount = menu.getComponentCount();
    
    temp tempSetTL(mouseEvent, e);
    for (O menuMaker : maker)
      pcallF(menuMaker, menu);
      
    vmBus_send('showingPopupMenu, e.getComponent(), menu);
    
    // show menu if any items in it
    if (menu.getComponentCount() != emptyCount)
      menu.show(e.getComponent(), e.getX(), e.getY());
  }
}

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: 284 / 315
Referenced in: [show references]