Home » openjdk-7 » javax » swing » text » [javadoc | source]

    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        */
   54       public static LayoutQueue getDefaultQueue() {
   55           AppContext ac = AppContext.getAppContext();
   56           synchronized (DEFAULT_QUEUE) {
   57               LayoutQueue defaultQueue = (LayoutQueue) ac.get(DEFAULT_QUEUE);
   58               if (defaultQueue == null) {
   59                   defaultQueue = new LayoutQueue();
   60                   ac.put(DEFAULT_QUEUE, defaultQueue);
   61               }
   62               return defaultQueue;
   63           }
   64       }
   65   
   66       /**
   67        * Set the default layout queue.
   68        *
   69        * @param q the new queue.
   70        */
   71       public static void setDefaultQueue(LayoutQueue q) {
   72           synchronized (DEFAULT_QUEUE) {
   73               AppContext.getAppContext().put(DEFAULT_QUEUE, q);
   74           }
   75       }
   76   
   77       /**
   78        * Add a task that is not needed immediately because
   79        * the results are not believed to be visible.
   80        */
   81       public synchronized void addTask(Runnable task) {
   82           if (worker == null) {
   83               worker = new LayoutThread();
   84               worker.start();
   85           }
   86           tasks.addElement(task);
   87           notifyAll();
   88       }
   89   
   90       /**
   91        * Used by the worker thread to get a new task to execute
   92        */
   93       protected synchronized Runnable waitForWork() {
   94           while (tasks.size() == 0) {
   95               try {
   96                   wait();
   97               } catch (InterruptedException ie) {
   98                   return null;
   99               }
  100           }
  101           Runnable work = tasks.firstElement();
  102           tasks.removeElementAt(0);
  103           return work;
  104       }
  105   
  106       /**
  107        * low priority thread to perform layout work forever
  108        */
  109       class LayoutThread extends Thread {
  110   
  111           LayoutThread() {
  112               super("text-layout");
  113               setPriority(Thread.MIN_PRIORITY);
  114           }
  115   
  116           public void run() {
  117               Runnable work;
  118               do {
  119                   work = waitForWork();
  120                   if (work != null) {
  121                       work.run();
  122                   }
  123               } while (work != null);
  124           }
  125   
  126   
  127       }
  128   
  129   }

Home » openjdk-7 » javax » swing » text » [javadoc | source]