Home | History | Annotate | Download | only in wtk
      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 /**
     18  * @author Pavel Dolgov
     19  * @version $Revision$
     20  */
     21 package org.apache.harmony.awt.wtk;
     22 
     23 /**
     24  * Shutdown Watchdog
     25  */
     26 public final class ShutdownWatchdog {
     27 
     28     private boolean nativeQueueEmpty = true;
     29     private boolean awtQueueEmpty = true;
     30     private boolean windowListEmpty = true;
     31 
     32     private boolean forcedShutdown = false;
     33 
     34     private ShutdownThread thread;
     35 
     36     public synchronized void setNativeQueueEmpty(boolean empty) {
     37         nativeQueueEmpty = empty;
     38         checkShutdown();
     39     }
     40 
     41     public synchronized void setAwtQueueEmpty(boolean empty) {
     42         awtQueueEmpty = empty;
     43         checkShutdown();
     44     }
     45 
     46     public synchronized void setWindowListEmpty(boolean empty) {
     47         windowListEmpty = empty;
     48         checkShutdown();
     49     }
     50 
     51     public synchronized void forceShutdown() {
     52         forcedShutdown = true;
     53         shutdown();
     54     }
     55 
     56     public synchronized void start() {
     57         keepAlive();
     58     }
     59 
     60     private void checkShutdown() {
     61         if (canShutdown()) {
     62             shutdown();
     63         } else {
     64             keepAlive();
     65         }
     66     }
     67 
     68     private boolean canShutdown() {
     69         return (nativeQueueEmpty && awtQueueEmpty && windowListEmpty) ||
     70                 forcedShutdown;
     71     }
     72 
     73     private void keepAlive() {
     74         if (thread == null) {
     75             thread = new ShutdownThread();
     76             thread.start();
     77         }
     78     }
     79 
     80     private void shutdown() {
     81         if (thread != null) {
     82             thread.shutdown();
     83             thread = null;
     84         }
     85     }
     86 }
     87