summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/gui/panels/BatchTaskProgressPanel.java
blob: eae6f0cbbe7cf3d09d392c7de1ebf043da8db5df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package bjc.utils.gui.panels;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * A Swing panel that groups multiple background tasks into collapsible "batches"
 * using CollapsiblePanel, similar to a download manager.
 *
 * Each batch:
 *   - Appears as a CollapsiblePanel with a title.
 *   - Contains a vertical list of task rows.
 *
 * Each task:
 *   - Has a description label.
 *   - A progress bar (0..max).
 *   - A small note/status line.
 *   - Optional Cancel button.
 *
 * You typically:
 *
 *   BatchTaskProgressPanel progressPanel = new BatchTaskProgressPanel();
 *
 *   BatchTaskProgressPanel.BatchHandle batch =
 *       progressPanel.startBatch("Saving Batch 1");
 *
 *   SwingWorker<Void, Void> w1 = ...;
 *   batch.monitorSwingWorker(w1, "Collection A", true);
 *   w1.execute();
 *
 *   SwingWorker<Void, Void> w2 = ...;
 *   batch.monitorSwingWorker(w2, "Collection B", true);
 *   w2.execute();
 */
public class BatchTaskProgressPanel extends JPanel {
	private static final long serialVersionUID = 758145786594145134L;
	
	private final JPanel batchesPanel;
	private final JScrollPane scrollPane;
	private final JButton clearFinishedButton;

	// UI for each batch
	private static class BatchUI {
		final CollapsiblePanel collapsible;
		final JPanel taskListPanel; // BoxLayout.Y_AXIS

		BatchUI(CollapsiblePanel collapsible, JPanel taskListPanel) {
			this.collapsible = collapsible;
			this.taskListPanel = taskListPanel;
		}
	}

	private final Map<BatchHandle, BatchUI> batches = new LinkedHashMap<>();
	private final Map<TaskHandle, TaskPanel> taskPanels = new LinkedHashMap<>();

	/**
	 * Create a new BatchTaskProgressPanel
	 */
	public BatchTaskProgressPanel() {
		super(new BorderLayout());

		batchesPanel = new JPanel();
		batchesPanel.setLayout(new BoxLayout(batchesPanel, BoxLayout.Y_AXIS));

		scrollPane = new JScrollPane(batchesPanel);
		scrollPane.setBorder(BorderFactory.createEmptyBorder());

		clearFinishedButton = new JButton(new AbstractAction("Clear finished") {
			@Override
			public void actionPerformed(ActionEvent e) {
				clearFinishedTasksAndEmptyBatches();
			}
		});

		JPanel header = new JPanel(new BorderLayout());
		header.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
		header.add(new JLabel("Background tasks"), BorderLayout.WEST);
		header.add(clearFinishedButton, BorderLayout.EAST);

		add(header, BorderLayout.NORTH);
		add(scrollPane, BorderLayout.CENTER);
	}

	/**
	 * Start a new batch section with a given title.
	 * 
	 * @param batchTitle The title for the batch
	 * @return The new batch
	 */
	public BatchHandle startBatch(String batchTitle) {
		BatchHandle handle = new BatchHandle(this);
		runOnEdt(() -> {
			CollapsiblePanel collapsible = new CollapsiblePanel(batchTitle);

			JPanel taskListPanel = new JPanel();
			taskListPanel.setLayout(new BoxLayout(taskListPanel, BoxLayout.Y_AXIS));

			collapsible.add(taskListPanel);

			BatchUI batchUI = new BatchUI(collapsible, taskListPanel);
			batches.put(handle, batchUI);
			batchesPanel.add(collapsible);
			batchesPanel.add(Box.createVerticalStrut(4));

			batchesPanel.revalidate();
			batchesPanel.repaint();
		});
		return handle;
	}

	/**
	 * Remove all finished tasks, and any batches that become empty afterwards.
	 */
	public void clearFinishedTasksAndEmptyBatches() {
		runOnEdt(() -> {
			// Remove finished tasks from each batch
			taskPanels.entrySet().removeIf(entry -> {
				TaskPanel tp = entry.getValue();
				if (tp.isFinished()) {
					Container parent = tp.getParent();
					if (parent != null) {
						parent.remove(tp);
					}
					return true;
				}
				return false;
			});

			// Now remove empty batches
			batches.entrySet().removeIf(entry -> {
				BatchUI batchUI = entry.getValue();
				if (batchUI.taskListPanel.getComponentCount() == 0) {
					batchesPanel.remove(batchUI.collapsible);
					return true;
				}
				return false;
			});

			batchesPanel.revalidate();
			batchesPanel.repaint();
		});
	}

	// === Batch creation API internal helpers ===

	private TaskHandle createTaskInBatch(BatchHandle batchHandle, String description, int min, int max,
			Runnable onCancel) {
		TaskHandle taskHandle = new TaskHandle(this, batchHandle);

		runOnEdt(() -> {
			BatchUI batchUI = batches.get(batchHandle);
			if (batchUI == null) {
				return; // batch removed or not present
			}
			TaskPanel panel = new TaskPanel(taskHandle, description, min, max, onCancel);
			taskPanels.put(taskHandle, panel);
			batchUI.taskListPanel.add(panel);
			batchUI.taskListPanel.revalidate();
			batchUI.taskListPanel.repaint();
		});

		return taskHandle;
	}

	private TaskHandle monitorSwingWorkerInBatch(BatchHandle batchHandle, final SwingWorker<?, ?> worker,
			String description, boolean cancellable) {
		Runnable onCancel = cancellable ? () -> worker.cancel(true) : null;
		final TaskHandle handle = createTaskInBatch(batchHandle, description, 0, 100, onCancel);

		worker.addPropertyChangeListener(new PropertyChangeListener() {
			@Override
			public void propertyChange(PropertyChangeEvent evt) {
				switch (evt.getPropertyName()) {
				case "progress":
					int intValue = (Integer) evt.getNewValue();
					handle.setProgress(intValue);
					break;
				case "state":
					SwingWorker.StateValue state = (SwingWorker.StateValue) evt.getNewValue();
					if (state == SwingWorker.StateValue.DONE) {
						handle.done();
					}
					break;
				case "note":
					String strValue = (String) evt.getNewValue();
					handle.setNote(strValue);
					break;
				default:
					// ignore other properties
				}
			}
		});

		return handle;
	}

	// === Internal helpers used by TaskHandle ===

	private void updateProgress(TaskHandle handle, int value) {
		runOnEdt(() -> {
			TaskPanel panel = taskPanels.get(handle);
			if (panel != null) {
				panel.setProgress(value);
			}
		});
	}

	// TODO add functionality to view previous notes in a 'Details' window
	private void updateNote(TaskHandle handle, String note) {
		runOnEdt(() -> {
			TaskPanel panel = taskPanels.get(handle);
			if (panel != null) {
				panel.setNote(note);
			}
		});
	}

	private void markDone(TaskHandle handle) {
		runOnEdt(() -> {
			TaskPanel panel = taskPanels.get(handle);
			if (panel != null) {
				panel.setFinished(true);
			}
		});
	}

	private void markCancelled(TaskHandle handle) {
		runOnEdt(() -> {
			TaskPanel panel = taskPanels.get(handle);
			if (panel != null) {
				panel.setCancelled();
			}
		});
	}

	/**
	 * Remove a single task row from the UI (used by the "Remove" button), and
	 * remove the batch if it becomes empty.
	 */
	private void removeTask(TaskHandle handle) {
		runOnEdt(() -> {
			TaskPanel panel = taskPanels.remove(handle);
			if (panel != null) {
				Container parent = panel.getParent();
				if (parent != null) {
					parent.remove(panel);
					parent.revalidate();
					parent.repaint();
				}
			}

			// Remove batch if it has no tasks left
			BatchUI batchUI = batches.get(handle.getBatch());
			if (batchUI != null && batchUI.taskListPanel.getComponentCount() == 0) {
				batches.remove(handle.getBatch());
				batchesPanel.remove(batchUI.collapsible);
				batchesPanel.revalidate();
				batchesPanel.repaint();
			}
		});
	}

	void runOnEdt(Runnable r) {
		if (SwingUtilities.isEventDispatchThread()) {
			r.run();
		} else {
			SwingUtilities.invokeLater(r);
		}
	}

	// === Public handles ===

	/**
	 * Handle representing a batch (one CollapsiblePanel section).
	 */
	public static class BatchHandle {
		private final BatchTaskProgressPanel owner;

		private BatchHandle(BatchTaskProgressPanel owner) {
			this.owner = owner;
		}

		/**
		 * Start a task in this batch
		 * 
		 * @param description The description for the task
		 * @param min The minimum progress value
		 * @param max The maximum progress value
		 * @param onCancel The function to run on cancelling
		 * @return A handle to the task
		 */
		public TaskHandle startTask(String description, int min, int max, Runnable onCancel) {
			return owner.createTaskInBatch(this, description, min, max, onCancel);
		}

		/**
		 * Start a task in this batch
		 * 
		 * @param description The description for the task
		 * @param min The minimum progress value
		 * @param max The maximum progress value
		 * @return A handle to the task
		 */
		public TaskHandle startTask(String description, int min, int max) {
			return startTask(description, min, max, null);
		}

		/**
		 * Start a task monitoring a given SwingWorker
		 * 
		 * @param worker The SwingWorker to monitor
		 * @param description The description for the task
		 * @param cancellable Is the task cancellable?
		 * @return A handle to the task
		 */
		public TaskHandle monitorSwingWorker(SwingWorker<?, ?> worker, String description, boolean cancellable) {
			return owner.monitorSwingWorkerInBatch(this, worker, description, cancellable);
		}

		/**
		 * Set the header component for this batch
		 * @param comp
		 */
		public void setHeaderComponent(final Component comp) {
			owner.runOnEdt(() -> {
				BatchUI ui = owner.batches.get(this);
				if (ui != null) {
					ui.collapsible.setHeaderComponent(comp);
				}
			});
		}
	}

	/**
	 * Handle that your worker code uses to update a specific task.
	 */
	public static class TaskHandle {
		private final BatchTaskProgressPanel owner;
		private final BatchHandle batch;
		private volatile boolean cancelled = false;
		private volatile boolean done = false;

		private TaskHandle(BatchTaskProgressPanel owner, BatchHandle batch) {
			this.owner = owner;
			this.batch = batch;
		}

		/**
		 * Return the handle to the underlying batch
		 * @return The underlying batch
		 */
		public BatchHandle getBatch() {
			return batch;
		}

		/**
		 * Set the progress for the task
		 * @param value The progress value
		 */
		public void setProgress(int value) {
			if (!done) {
				owner.updateProgress(this, value);
			}
		}

		/**
		 * Set the note for the task
		 * @param note The note for the task
		 */
		public void setNote(String note) {
			if (!done) {
				owner.updateNote(this, note);
			}
		}

		/**
		 * Mark the task as done.
		 */
		public void done() {
			done = true;
			owner.markDone(this);
		}

		/**
		 * Mark the task as cancelled.
		 */
		public void cancel() {
			cancelled = true;
			owner.markCancelled(this);
		}

		/**
		 * Is the task cancelled?
		 * 
		 * @return If the task is cancelled or not
		 */
		public boolean isCancelled() {
			return cancelled;
		}

		/**
		 * Is the task done?
		 * 
		 * @return If the task is done or not
		 */
		public boolean isDone() {
			return done;
		}

		/**
		 * Called by the "Remove" button to drop this task row from the UI.
		 */
		public void dismiss() {
			owner.removeTask(this);
		}
	}

	// === UI for a single task row ===

	private static class TaskPanel extends JPanel {
		private static final long serialVersionUID = 7163540519142582817L;
		
		private final TaskHandle handle;
		private final JLabel descriptionLabel;
		private final JLabel noteLabel;
		private final JProgressBar progressBar;
		private final JButton cancelButton;
		private boolean finished = false;

		TaskPanel(TaskHandle handle, String description, int min, int max, Runnable onCancel) {
			this.handle = handle;
			setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY),
					BorderFactory.createEmptyBorder(4, 4, 4, 4)));
			setLayout(new BorderLayout(4, 4));

			descriptionLabel = new JLabel(description);
			descriptionLabel.setFont(descriptionLabel.getFont().deriveFont(Font.BOLD));

			noteLabel = new JLabel(" ");
			noteLabel.setFont(noteLabel.getFont().deriveFont(Font.ITALIC, 11f));
			noteLabel.setForeground(Color.DARK_GRAY);

			progressBar = new JProgressBar(min, max);
			progressBar.setStringPainted(true);

			JPanel textPanel = new JPanel();
			textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS));
			textPanel.add(descriptionLabel);
			textPanel.add(Box.createVerticalStrut(2));
			textPanel.add(noteLabel);

			cancelButton = new JButton("Cancel");
			if (onCancel == null) {
				// Non-cancellable: hide until it becomes a "Remove" button
				cancelButton.setVisible(false);
			} else {
				cancelButton.addActionListener(e -> {
					handle.cancel();
					onCancel.run();
				});
			}

			JPanel bottomPanel = new JPanel(new BorderLayout(4, 4));
			bottomPanel.add(progressBar, BorderLayout.CENTER);
			bottomPanel.add(cancelButton, BorderLayout.EAST);

			add(textPanel, BorderLayout.CENTER);
			add(bottomPanel, BorderLayout.SOUTH);
		}

		void setProgress(int value) {
			progressBar.setValue(value);
		}

		void setNote(String note) {
			noteLabel.setText((note == null || note.isEmpty()) ? " " : note);
		}

		void setFinished(boolean finished) {
			this.finished = finished;
			progressBar.setIndeterminate(false);
			progressBar.setValue(progressBar.getMaximum());
			noteLabel.setText("Completed");
			configureAsDismissButton();
		}

		void setCancelled() {
			this.finished = true;
			progressBar.setIndeterminate(false);
			noteLabel.setText("Cancelled");
			progressBar.setForeground(Color.GRAY);
			configureAsDismissButton();
		}

		boolean isFinished() {
			return finished;
		}

		/**
		 * Re-purpose the Cancel button as a "Remove" button that removes this task row.
		 */
		private void configureAsDismissButton() {
			// Ensure the button is visible
			cancelButton.setVisible(true);

			// Remove existing listeners (Cancel behavior)
			for (ActionListener l : cancelButton.getActionListeners()) {
				cancelButton.removeActionListener(l);
			}

			cancelButton.setText("Remove");
			cancelButton.setEnabled(true);
			cancelButton.addActionListener(e -> handle.dismiss());
		}
	}
}