Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.log4j.Logger;
- import javax.swing.*;
- import java.util.concurrent.CancellationException;
- import java.util.concurrent.ExecutionException;
- public abstract class Worker {
- SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
- @Override
- protected Void doInBackground() throws Exception {
- Worker.this.doInBackground();
- return null;
- }
- @Override
- public final void done() {
- try {
- get();
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- } catch (ExecutionException e) {
- throw new RuntimeException(e);
- } catch (CancellationException e) {
- cancelled();
- }
- afterDone();
- }
- };
- protected void cancelled() {
- Logger.getLogger(this.getClass()).error(toString() + " cancelled");
- }
- protected abstract void doInBackground() throws Exception;
- protected void afterDone() {
- }
- public final void execute() {
- worker.execute();
- }
- public final boolean isDone() {
- return worker.isDone();
- }
- public final void cancel(boolean interrupt) {
- worker.cancel(interrupt);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement