src/share/classes/java/awt/EventDispatchThread.java
branchjavafx
changeset 5229 4ed8674de305
parent 5155 d45bc4307996
     1.1 --- a/src/share/classes/java/awt/EventDispatchThread.java	Tue Mar 06 20:34:38 2012 +0000
     1.2 +++ b/src/share/classes/java/awt/EventDispatchThread.java	Wed May 23 11:51:09 2012 +0200
     1.3 @@ -25,19 +25,15 @@
     1.4  
     1.5  package java.awt;
     1.6  
     1.7 -import java.awt.event.InputEvent;
     1.8  import java.awt.event.MouseEvent;
     1.9  import java.awt.event.ActionEvent;
    1.10  import java.awt.event.WindowEvent;
    1.11  import java.lang.reflect.Method;
    1.12 -import java.security.AccessController;
    1.13 -import sun.security.action.GetPropertyAction;
    1.14 -import sun.awt.AWTAutoShutdown;
    1.15 -import sun.awt.SunToolkit;
    1.16 -import sun.awt.AppContext;
    1.17  
    1.18  import java.util.ArrayList;
    1.19 -import java.util.List;
    1.20 +import java.util.concurrent.BlockingQueue;
    1.21 +import java.util.concurrent.Executor;
    1.22 +import java.util.concurrent.LinkedBlockingQueue;
    1.23  import sun.util.logging.PlatformLogger;
    1.24  
    1.25  import sun.awt.dnd.SunDragSourceContextPeer;
    1.26 @@ -62,7 +58,7 @@
    1.27   *
    1.28   * @since 1.1
    1.29   */
    1.30 -class EventDispatchThread extends Thread {
    1.31 +final class EventDispatchThread {
    1.32  
    1.33      private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventDispatchThread");
    1.34  
    1.35 @@ -70,14 +66,51 @@
    1.36      private boolean doDispatch = true;
    1.37      private volatile boolean shutdown = false;
    1.38  
    1.39 -    private static final int ANY_EVENT = -1;
    1.40 +    static final int ANY_EVENT = -1;
    1.41  
    1.42 +    private final Executor executor;
    1.43      private ArrayList<EventFilter> eventFilters = new ArrayList<EventFilter>();
    1.44  
    1.45 -    EventDispatchThread(ThreadGroup group, String name, EventQueue queue) {
    1.46 -        super(group, name);
    1.47 +    EventDispatchThread(ThreadGroup group, String name, EventQueue queue, ClassLoader l) {
    1.48 +        // right now using simple executor that sort of mimics the old
    1.49 +        // behavior of the run() method of EventDispatchThread. But when
    1.50 +        // running together with FX, the executor should dispatch the runnable
    1.51 +        // using Platform.invokeLater to share the same processing thread
    1.52 +        executor = defaultThread(group, name, l);
    1.53          setEventQueue(queue);
    1.54      }
    1.55 +    
    1.56 +    private static Executor defaultThread(ThreadGroup group, String name, ClassLoader classLoader) {
    1.57 +        class ExecRuns implements Executor, Runnable {
    1.58 +            final BlockingQueue<Runnable> toRun = new LinkedBlockingQueue<>();
    1.59 +            @Override
    1.60 +            public void execute(Runnable command) {
    1.61 +                toRun.add(command);
    1.62 +            }
    1.63 +
    1.64 +            @Override
    1.65 +            public void run() {
    1.66 +                while (true) {
    1.67 +                    Runnable r;
    1.68 +                    try {
    1.69 +                        r = toRun.take();
    1.70 +                    } catch (InterruptedException ex) {
    1.71 +                        eventLog.severe(ex.getMessage(), ex);
    1.72 +                        continue;
    1.73 +                    }
    1.74 +                    r.run();
    1.75 +                }
    1.76 +            }
    1.77 +        }
    1.78 +        ExecRuns er = new ExecRuns();
    1.79 +        
    1.80 +        Thread t = new Thread(group, name);
    1.81 +        t.setContextClassLoader(classLoader);
    1.82 +        t.setPriority(Thread.NORM_PRIORITY + 1);
    1.83 +        t.setDaemon(false);
    1.84 +        t.start();
    1.85 +        return er;
    1.86 +    }
    1.87  
    1.88      /*
    1.89       * Must be called on EDT only, that's why no synchronization
    1.90 @@ -88,23 +121,29 @@
    1.91  
    1.92      public void interrupt() {
    1.93          shutdown = true;
    1.94 -        super.interrupt();
    1.95 +//XXX        super.interrupt();
    1.96      }
    1.97  
    1.98 -    public void run() {
    1.99 -        while (true) {
   1.100 -            try {
   1.101 -                pumpEvents(new Conditional() {
   1.102 -                    public boolean evaluate() {
   1.103 -                        return true;
   1.104 +    public void processEvents() {
   1.105 +        executor.execute(new Runnable() {
   1.106 +            @Override
   1.107 +            public void run() {
   1.108 +                try {
   1.109 +                    // XXX: the meaning now should be: the pumpEvents processes
   1.110 +                    // currently pending events, and then it returns without
   1.111 +                    // any blocking
   1.112 +                    pumpEvents(new Conditional() {
   1.113 +                        public boolean evaluate() {
   1.114 +                            return true;
   1.115 +                        }
   1.116 +                    });
   1.117 +                } finally {
   1.118 +                    if(getEventQueue().detachDispatchThread(this, shutdown)) {
   1.119 +                        return;
   1.120                      }
   1.121 -                });
   1.122 -            } finally {
   1.123 -                if(getEventQueue().detachDispatchThread(this, shutdown)) {
   1.124 -                    break;
   1.125                  }
   1.126              }
   1.127 -        }
   1.128 +        });
   1.129      }
   1.130  
   1.131      // MacOSX change:
   1.132 @@ -155,6 +194,11 @@
   1.133          pumpEventsForFilter(ANY_EVENT, cond, filter);
   1.134      }
   1.135  
   1.136 +    //XXX
   1.137 +    boolean isInterrupted() {
   1.138 +        return false;
   1.139 +    }
   1.140 +    
   1.141      void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) {
   1.142          addEventFilter(filter);
   1.143          doDispatch = true;
   1.144 @@ -195,6 +239,10 @@
   1.145              eventFilters.remove(filter);
   1.146          }
   1.147      }
   1.148 +    
   1.149 +    boolean blockWhenNoEvent() {
   1.150 +        return true;
   1.151 +    }
   1.152  
   1.153      void pumpOneEventForFilters(int id) {
   1.154          AWTEvent event = null;
   1.155 @@ -210,7 +258,10 @@
   1.156                  if (delegate != null && id == ANY_EVENT) {
   1.157                      event = delegate.getNextEvent(eq);
   1.158                  } else {
   1.159 -                    event = (id == ANY_EVENT) ? eq.getNextEvent() : eq.getNextEvent(id);
   1.160 +                    event = eq.getNextEvent(id, blockWhenNoEvent());
   1.161 +                }
   1.162 +                if (event == null) {
   1.163 +                    return;
   1.164                  }
   1.165  
   1.166                  eventOK = true;
   1.167 @@ -263,7 +314,12 @@
   1.168          if (eventLog.isLoggable(PlatformLogger.FINE)) {
   1.169              eventLog.fine("Processing exception: " + e);
   1.170          }
   1.171 -        getUncaughtExceptionHandler().uncaughtException(this, e);
   1.172 +//XXX        getUncaughtExceptionHandler().uncaughtException(this, e);
   1.173 +    }
   1.174 +    
   1.175 +    public static EventDispatchThread findCurrent() {
   1.176 +//XXX        return (EventDispatchThread)Thread.currentThread();
   1.177 +        return null;
   1.178      }
   1.179  
   1.180      public synchronized EventQueue getEventQueue() {