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

1  
sclass BackgroundProcessesUI is IBackgroundProcesses {
2  
  Set<Entry> processes = setWithNotify(syncLinkedHashSet(), l0 updateCount);
3  
  SimpleLiveValue<Int> lvCount = new SimpleLiveValue<Int>(Int, 0);
4  
  
5  
  record Entry(S name) is IBackgroundProcess {
6  
    settable JMenuItem menuItem;
7  
    settable Runnable interruptAction;
8  
    
9  
    toString { ret name; }
10  
    
11  
    close { processes.remove(this); }
12  
  }
13  
  
14  
  JLabel shortLabel() {
15  
    var lbl = bindToolTipToTransformedLiveValue(
16  
      n -> makeToolTip(), lvCount,
17  
        simpleTransformedLiveValueLabel(n -> n2(n), lvCount));
18  
    
19  
    onMouseDown_anyButton(lbl, e -> {
20  
      var l = cloneList(processes);
21  
      
22  
      // make sure title is displayed fully
23  
      JPopupMenu menu = new;/* {
24  
        public Dimension getMinimumSize() {
25  
          TitledBorder border = optCast TitledBorder(getBorder());
26  
          _print(+border);
27  
          ret maxDimension(super.getMinimumSize(), border?.getMinimumSize(this));
28  
        }
29  
      };*/
30  
      
31  
      int n = componentCount(menu);
32  
      for (Entry a : l)
33  
        addMenuItem(menu, processToMenuItem(a));
34  
      if (componentCount(menu) != n) {
35  
        var border = jRaisedSectionBorder("Background Processes");
36  
        setBorder(menu, border);
37  
        showPopupMenu(menu, e);
38  
        var size = menu.getSize();
39  
        var borderSize = dimensionPlus(10 /* hack */, 0, border.getMinimumSize(menu));
40  
        printVars(+size, +borderSize);
41  
        menu.setPopupSize(maxDimension(size, borderSize));
42  
      }
43  
    });
44  
    
45  
    ret lbl;
46  
  }
47  
  
48  
  void add(Entry process) {
49  
    processes.add(process);
50  
  }
51  
  
52  
  void remove(Entry process) {
53  
    processes.remove(process);
54  
  }
55  
  
56  
  void addOrRemove(bool add, Entry process) {
57  
    if (add) add(process); else remove(process);
58  
  }
59  
  
60  
  public Entry tempAdd(S name) {
61  
    ret tempAdd(new Entry(or2(name, "Unnamed process")));
62  
  }
63  
  
64  
  public Entry tempAdd(Entry process) {
65  
    if (process == null) null;
66  
    processes.add(process);
67  
    ret process;
68  
  }
69  
  
70  
  void updateCount {
71  
    lvCount.set(l(processes));
72  
  }
73  
  
74  
  S makeToolTip() {
75  
    var l = listProcesses();
76  
    ret empty(l) ? "No background processes"
77  
      : n2(l, "background process", "background processes")
78  
      + ": " + joinWithComma(processes);
79  
  }
80  
  
81  
  swappable JMenuItem processToMenuItem(Entry process) {
82  
    try object process.getMenuItem();
83  
    if (process.interruptAction() != null)
84  
      ret jMenuItem("Interrupt " + process, rThread(process.interruptAction()));
85  
    ret jMenuItem(process + " [no action available]");
86  
  }
87  
  
88  
  L<Entry> listProcesses() { ret cloneList(processes); }
89  
  
90  
  Cl<Entry> processesNamedIC(S name) {
91  
    ret filter(listProcesses(), entry -> eqic(entry.name, name));
92  
  }
93  
}

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: 241 / 526
Version history: 39 change(s)
Referenced in: [show references]