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