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

93
LINES

< > BotCompany Repo | #1034127 // BackgroundProcessesUI

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (11568L/65K).

sclass BackgroundProcessesUI is IBackgroundProcesses {
  Set<Entry> processes = setWithNotify(syncLinkedHashSet(), l0 updateCount);
  SimpleLiveValue<Int> lvCount = new SimpleLiveValue<Int>(Int, 0);
  
  record Entry(S name) is IBackgroundProcess {
    settable JMenuItem menuItem;
    settable Runnable interruptAction;
    
    toString { ret name; }
    
    close { processes.remove(this); }
  }
  
  JLabel shortLabel() {
    var lbl = bindToolTipToTransformedLiveValue(
      n -> makeToolTip(), lvCount,
        simpleTransformedLiveValueLabel(n -> n2(n), lvCount));
    
    onMouseDown_anyButton(lbl, e -> {
      var l = cloneList(processes);
      
      // make sure title is displayed fully
      JPopupMenu menu = new;/* {
        public Dimension getMinimumSize() {
          TitledBorder border = optCast TitledBorder(getBorder());
          _print(+border);
          ret maxDimension(super.getMinimumSize(), border?.getMinimumSize(this));
        }
      };*/
      
      int n = componentCount(menu);
      for (Entry a : l)
        addMenuItem(menu, processToMenuItem(a));
      if (componentCount(menu) != n) {
        var border = jRaisedSectionBorder("Background Processes");
        setBorder(menu, border);
        showPopupMenu(menu, e);
        var size = menu.getSize();
        var borderSize = dimensionPlus(10 /* hack */, 0, border.getMinimumSize(menu));
        printVars(+size, +borderSize);
        menu.setPopupSize(maxDimension(size, borderSize));
      }
    });
    
    ret lbl;
  }
  
  void add(Entry process) {
    processes.add(process);
  }
  
  void remove(Entry process) {
    processes.remove(process);
  }
  
  void addOrRemove(bool add, Entry process) {
    if (add) add(process); else remove(process);
  }
  
  public Entry tempAdd(S name) {
    ret tempAdd(new Entry(or2(name, "Unnamed process")));
  }
  
  public Entry tempAdd(Entry process) {
    if (process == null) null;
    processes.add(process);
    ret process;
  }
  
  void updateCount {
    lvCount.set(l(processes));
  }
  
  S makeToolTip() {
    var l = listProcesses();
    ret empty(l) ? "No background processes"
      : n2(l, "background process", "background processes")
      + ": " + joinWithComma(processes);
  }
  
  swappable JMenuItem processToMenuItem(Entry process) {
    try object process.getMenuItem();
    if (process.interruptAction() != null)
      ret jMenuItem("Interrupt " + process, rThread(process.interruptAction()));
    ret jMenuItem(process + " [no action available]");
  }
  
  L<Entry> listProcesses() { ret cloneList(processes); }
  
  Cl<Entry> processesNamedIC(S name) {
    ret filter(listProcesses(), entry -> eqic(entry.name, name));
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034127
Snippet name: BackgroundProcessesUI
Eternal ID of this version: #1034127/40
Text MD5: 9d4ff71445866c53c6b4f699f14886ae
Transpilation MD5: d52ed0a1da8b4f450ffab1f287c4a3ed
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-11 15:32:49
Source code size: 2785 bytes / 93 lines
Pitched / IR pitched: No / No
Views / Downloads: 242 / 526
Version history: 39 change(s)
Referenced in: #1034163 - BackgroundProcessesUI [backup]
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)