2015-04-20 15:36:01 -07:00
|
|
|
//: gui/MonitoredLongRunningCallable.java
|
|
|
|
// Displaying task progress with ProgressMonitors.
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
import net.mindview.util.*;
|
|
|
|
import static net.mindview.util.SwingConsole.*;
|
|
|
|
|
|
|
|
class MonitoredCallable implements Callable<String> {
|
|
|
|
private static int counter = 0;
|
|
|
|
private final int id = counter++;
|
|
|
|
private final ProgressMonitor monitor;
|
|
|
|
private final static int MAX = 8;
|
|
|
|
public MonitoredCallable(ProgressMonitor monitor) {
|
|
|
|
this.monitor = monitor;
|
|
|
|
monitor.setNote(toString());
|
|
|
|
monitor.setMaximum(MAX - 1);
|
|
|
|
monitor.setMillisToPopup(500);
|
|
|
|
}
|
2015-05-05 11:20:13 -07:00
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
public String call() {
|
|
|
|
System.out.println(this + " started");
|
|
|
|
try {
|
|
|
|
for(int i = 0; i < MAX; i++) {
|
|
|
|
TimeUnit.MILLISECONDS.sleep(500);
|
|
|
|
if(monitor.isCanceled())
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
final int progress = i;
|
2015-05-05 14:05:39 -07:00
|
|
|
SwingUtilities.invokeLater(() -> {
|
|
|
|
monitor.setProgress(progress);
|
|
|
|
});
|
2015-04-20 15:36:01 -07:00
|
|
|
}
|
|
|
|
} catch(InterruptedException e) {
|
|
|
|
monitor.close();
|
|
|
|
System.out.println(this + " interrupted");
|
|
|
|
return "Result: " + this + " interrupted";
|
|
|
|
}
|
|
|
|
System.out.println(this + " completed");
|
|
|
|
return "Result: " + this + " completed";
|
|
|
|
}
|
2015-05-05 11:20:13 -07:00
|
|
|
@Override
|
2015-04-20 15:36:01 -07:00
|
|
|
public String toString() { return "Task " + id; }
|
|
|
|
};
|
|
|
|
|
|
|
|
public class MonitoredLongRunningCallable extends JFrame {
|
|
|
|
private JButton
|
|
|
|
b1 = new JButton("Start Long Running Task"),
|
|
|
|
b2 = new JButton("End Long Running Task"),
|
|
|
|
b3 = new JButton("Get results");
|
|
|
|
private TaskManager<String,MonitoredCallable> manager =
|
2015-05-05 11:20:13 -07:00
|
|
|
new TaskManager<>();
|
2015-04-20 15:36:01 -07:00
|
|
|
public MonitoredLongRunningCallable() {
|
2015-05-06 12:09:38 -07:00
|
|
|
b1.addActionListener(e -> {
|
2015-05-05 14:05:39 -07:00
|
|
|
MonitoredCallable task = new MonitoredCallable(
|
|
|
|
new ProgressMonitor(
|
|
|
|
MonitoredLongRunningCallable.this,
|
|
|
|
"Long-Running Task", "", 0, 0)
|
|
|
|
);
|
|
|
|
manager.add(task);
|
|
|
|
System.out.println(task + " added to the queue");
|
2015-04-20 15:36:01 -07:00
|
|
|
});
|
2015-05-06 12:09:38 -07:00
|
|
|
b2.addActionListener(e -> {
|
2015-05-05 14:05:39 -07:00
|
|
|
for(String result : manager.purge())
|
|
|
|
System.out.println(result);
|
2015-04-20 15:36:01 -07:00
|
|
|
});
|
2015-05-06 12:09:38 -07:00
|
|
|
b3.addActionListener(e -> {
|
2015-05-05 14:05:39 -07:00
|
|
|
for(String result : manager.getResults())
|
|
|
|
System.out.println(result);
|
2015-04-20 15:36:01 -07:00
|
|
|
});
|
|
|
|
setLayout(new FlowLayout());
|
|
|
|
add(b1);
|
|
|
|
add(b2);
|
|
|
|
add(b3);
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
run(new MonitoredLongRunningCallable(), 200, 500);
|
|
|
|
}
|
|
|
|
} ///:~
|