OnJava8-Examples/gui/MonitoredLongRunningCallable.java
2015-04-20 15:36:01 -07:00

88 lines
2.7 KiB
Java

//: gui/MonitoredLongRunningCallable.java
// Displaying task progress with ProgressMonitors.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
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);
}
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;
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
monitor.setProgress(progress);
}
}
);
}
} catch(InterruptedException e) {
monitor.close();
System.out.println(this + " interrupted");
return "Result: " + this + " interrupted";
}
System.out.println(this + " completed");
return "Result: " + this + " completed";
}
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 =
new TaskManager<String,MonitoredCallable>();
public MonitoredLongRunningCallable() {
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MonitoredCallable task = new MonitoredCallable(
new ProgressMonitor(
MonitoredLongRunningCallable.this,
"Long-Running Task", "", 0, 0)
);
manager.add(task);
System.out.println(task + " added to the queue");
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(String result : manager.purge())
System.out.println(result);
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(String result : manager.getResults())
System.out.println(result);
}
});
setLayout(new FlowLayout());
add(b1);
add(b2);
add(b3);
}
public static void main(String[] args) {
run(new MonitoredLongRunningCallable(), 200, 500);
}
} ///:~