package org.helioviewer.jhv.threads;

import java.awt.EventQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import javax.annotation.Nonnull;

import com.google.common.util.concurrent.ForwardingListeningExecutorService;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;

public class EventQueueCallbackExecutor extends ForwardingListeningExecutorService {

    private static class EventQueueExecutor implements Executor {

        @Override
        public void execute(@Nonnull Runnable command) {
            EventQueue.invokeLater(command);
        }

    }

    private static final Executor eventQueue = new EventQueueExecutor();

    public static final EventQueueCallbackExecutor pool =
            new EventQueueCallbackExecutor(
                    MoreExecutors.listeningDecorator(
                            MoreExecutors.getExitingExecutorService(
                                    (ThreadPoolExecutor) Executors.newCachedThreadPool())));

    private final ListeningExecutorService delegate;

    public EventQueueCallbackExecutor(ListeningExecutorService _delegate) {
        delegate = _delegate;
    }

    @Override
    public ListeningExecutorService delegate() {
        return delegate;
    }

    public <T> ListenableFuture<T> submit(Callable<T> callable, FutureCallback<T> callback) {
        ListenableFuture<T> futureTask = submit(callable);
        Futures.addCallback(futureTask, callback, eventQueue);
        return futureTask;
    }

}

@@ -135,16 +135,18 @@
 ecutor;%0A
+//
 import j
@@ -176,16 +176,18 @@
 cutors;%0A
+//
 import j
@@ -563,32 +563,34 @@
 xecutorService;%0A
+//
 import com.googl
@@ -629,16 +629,16 @@
 cutors;%0A
-
 %0Apublic 
@@ -994,17 +994,27 @@
 utor();%0A
-%0A
+    /*%0A    
     publ
@@ -1075,16 +1075,20 @@
         
+    
 new Even
@@ -1123,32 +1123,36 @@
                 
+  
+  
 MoreExecutors.li
@@ -1165,24 +1165,28 @@
 gDecorator(%0A
+    
             
@@ -1278,16 +1278,20 @@
         
+  
+  
 (ThreadP
@@ -1339,16 +1339,22 @@
 l())));%0A
+    */
 %0A    pri

Comment code