1 /*
   2  * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.text;
  26 
  27 import java.util.Vector;
  28 import sun.awt.AppContext;
  29 
  30 /**
  31  * A queue of text layout tasks.
  32  *
  33  * @author  Timothy Prinzing
  34  * @see     AsyncBoxView
  35  * @since   1.3
  36  */
  37 public class LayoutQueue {
  38 
  39     private static final Object DEFAULT_QUEUE = new Object();
  40 
  41     private Vector<Runnable> tasks;
  42     private Thread worker;
  43 
  44     /**
  45      * Construct a layout queue.
  46      */
  47     public LayoutQueue() {
  48         tasks = new Vector<Runnable>();
  49     }
  50 
  51     /**
  52      * Fetch the default layout queue.
  53      * @return the default layout queue
  54      */
  55     public static LayoutQueue getDefaultQueue() {
  56         AppContext ac = AppContext.getAppContext();
  57         synchronized (DEFAULT_QUEUE) {
  58             LayoutQueue defaultQueue = (LayoutQueue) ac.get(DEFAULT_QUEUE);
  59             if (defaultQueue == null) {
  60                 defaultQueue = new LayoutQueue();
  61                 ac.put(DEFAULT_QUEUE, defaultQueue);
  62             }
  63             return defaultQueue;
  64         }
  65     }
  66 
  67     /**
  68      * Set the default layout queue.
  69      *
  70      * @param q the new queue.
  71      */
  72     public static void setDefaultQueue(LayoutQueue q) {
  73         synchronized (DEFAULT_QUEUE) {
  74             AppContext.getAppContext().put(DEFAULT_QUEUE, q);
  75         }
  76     }
  77 
  78     /**
  79      * Add a task that is not needed immediately because
  80      * the results are not believed to be visible.
  81      * @param task the task to add to the queue
  82      */
  83     public synchronized void addTask(Runnable task) {
  84         if (worker == null) {
  85             worker = new LayoutThread();
  86             worker.start();
  87         }
  88         tasks.addElement(task);
  89         notifyAll();
  90     }
  91 
  92     /**
  93      * Used by the worker thread to get a new task to execute.
  94      * @return a task from the queue
  95      */
  96     protected synchronized Runnable waitForWork() {
  97         while (tasks.size() == 0) {
  98             try {
  99                 wait();
 100             } catch (InterruptedException ie) {
 101                 return null;
 102             }
 103         }
 104         Runnable work = tasks.firstElement();
 105         tasks.removeElementAt(0);
 106         return work;
 107     }
 108 
 109     /**
 110      * low priority thread to perform layout work forever
 111      */
 112     class LayoutThread extends Thread {
 113 
 114         LayoutThread() {
 115             super("text-layout");
 116             setPriority(Thread.MIN_PRIORITY);
 117         }
 118 
 119         public void run() {
 120             Runnable work;
 121             do {
 122                 work = waitForWork();
 123                 if (work != null) {
 124                     work.run();
 125                 }
 126             } while (work != null);
 127         }
 128 
 129 
 130     }
 131 
 132 }