0N/A/*
2362N/A * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing.text;
0N/A
0N/Aimport java.util.Vector;
1494N/Aimport sun.awt.AppContext;
0N/A
0N/A/**
0N/A * A queue of text layout tasks.
0N/A *
0N/A * @author Timothy Prinzing
0N/A * @see AsyncBoxView
0N/A * @since 1.3
0N/A */
0N/Apublic class LayoutQueue {
0N/A
1494N/A private static final Object DEFAULT_QUEUE = new Object();
0N/A
1494N/A private Vector<Runnable> tasks;
1494N/A private Thread worker;
0N/A
0N/A /**
0N/A * Construct a layout queue.
0N/A */
0N/A public LayoutQueue() {
611N/A tasks = new Vector<Runnable>();
0N/A }
0N/A
0N/A /**
0N/A * Fetch the default layout queue.
0N/A */
0N/A public static LayoutQueue getDefaultQueue() {
1494N/A AppContext ac = AppContext.getAppContext();
1494N/A synchronized (DEFAULT_QUEUE) {
1494N/A LayoutQueue defaultQueue = (LayoutQueue) ac.get(DEFAULT_QUEUE);
1494N/A if (defaultQueue == null) {
1494N/A defaultQueue = new LayoutQueue();
1494N/A ac.put(DEFAULT_QUEUE, defaultQueue);
1494N/A }
1494N/A return defaultQueue;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Set the default layout queue.
0N/A *
0N/A * @param q the new queue.
0N/A */
0N/A public static void setDefaultQueue(LayoutQueue q) {
1494N/A synchronized (DEFAULT_QUEUE) {
1494N/A AppContext.getAppContext().put(DEFAULT_QUEUE, q);
1494N/A }
0N/A }
0N/A
0N/A /**
0N/A * Add a task that is not needed immediately because
0N/A * the results are not believed to be visible.
0N/A */
0N/A public synchronized void addTask(Runnable task) {
0N/A if (worker == null) {
0N/A worker = new LayoutThread();
0N/A worker.start();
0N/A }
0N/A tasks.addElement(task);
0N/A notifyAll();
0N/A }
0N/A
0N/A /**
0N/A * Used by the worker thread to get a new task to execute
0N/A */
0N/A protected synchronized Runnable waitForWork() {
0N/A while (tasks.size() == 0) {
0N/A try {
0N/A wait();
0N/A } catch (InterruptedException ie) {
0N/A return null;
0N/A }
0N/A }
611N/A Runnable work = tasks.firstElement();
0N/A tasks.removeElementAt(0);
0N/A return work;
0N/A }
0N/A
0N/A /**
0N/A * low priority thread to perform layout work forever
0N/A */
0N/A class LayoutThread extends Thread {
0N/A
0N/A LayoutThread() {
0N/A super("text-layout");
0N/A setPriority(Thread.MIN_PRIORITY);
0N/A }
0N/A
0N/A public void run() {
0N/A Runnable work;
0N/A do {
0N/A work = waitForWork();
0N/A if (work != null) {
0N/A work.run();
0N/A }
0N/A } while (work != null);
0N/A }
0N/A
0N/A
0N/A }
0N/A
0N/A}