Home » openjdk-7 » java » awt » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package java.awt;
   18   
   19   final class HeadlessEventDispatchThread extends EventDispatchThread  {
   20   
   21       HeadlessEventDispatchThread(Toolkit toolkit, Dispatcher dispatcher ) {
   22           super(toolkit, dispatcher);
   23       }
   24   
   25       /**
   26        * Initialise and run the main event loop
   27        */
   28       @Override
   29       public void run() {
   30           try {
   31               runModalLoop(null);
   32           } finally {
   33               toolkit.shutdownWatchdog.forceShutdown();
   34           }
   35       }
   36       
   37       @Override
   38       void runModalLoop(ModalContext context) {
   39           long lastPaintTime = System.currentTimeMillis();
   40           while (!shutdownPending && (context == null || context.isModalLoopRunning())) {
   41               try {
   42                   EventQueue eventQueue = toolkit.getSystemEventQueueImpl();
   43       
   44                   toolkit.shutdownWatchdog.setNativeQueueEmpty(true);
   45                   AWTEvent ae = eventQueue.getNextEventNoWait();
   46                   if (ae != null) {
   47                       eventQueue.dispatchEvent(ae);
   48                       long curTime = System.currentTimeMillis();
   49                       if (curTime - lastPaintTime > 10) {
   50                           toolkit.onQueueEmpty();
   51                           lastPaintTime = System.currentTimeMillis();
   52                       }
   53                   } else {
   54                       toolkit.shutdownWatchdog.setAwtQueueEmpty(true);
   55                       toolkit.onQueueEmpty();
   56                       lastPaintTime = System.currentTimeMillis();
   57                       waitForAnyEvent();
   58                   }
   59               } catch (Throwable t) {
   60                   t.printStackTrace();
   61               }
   62           }
   63       }   
   64   
   65       private void waitForAnyEvent() {
   66           EventQueue eventQueue = toolkit.getSystemEventQueueImpl();
   67           if (!eventQueue.isEmpty()) {
   68               return;
   69           }
   70           
   71           Object eventMonitor = toolkit.getEventMonitor();
   72           synchronized(eventMonitor) {
   73               try {
   74                   eventMonitor.wait();
   75               } catch (InterruptedException e) {}
   76           }
   77       }
   78   
   79   }

Home » openjdk-7 » java » awt » [javadoc | source]