jdk/src/share/classes/javax/swing/text/LayoutQueue.java
author peterz
Wed, 04 Feb 2009 18:48:24 +0300
changeset 2481 3aa9107f1b72
parent 1287 a04aca99c77a
child 2484 9f3b45efb17d
permissions -rw-r--r--
6588003: LayoutQueue shares mutable implementation across AppContexts Summary: DefaultQueue property is made per-AppContext Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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;
2481
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
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
2481
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    39
    private static final Object DEFAULT_QUEUE = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
2481
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    41
    private Vector<Runnable> tasks;
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
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() {
2481
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    55
        AppContext ac = AppContext.getAppContext();
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    56
        synchronized (DEFAULT_QUEUE) {
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    57
            LayoutQueue defaultQueue = (LayoutQueue) ac.get(DEFAULT_QUEUE);
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    58
            if (defaultQueue == null) {
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    59
                defaultQueue = new LayoutQueue();
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    60
                ac.put(DEFAULT_QUEUE, defaultQueue);
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    61
            }
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
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
     *
2481
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    69
     * @param defaultQueue the new queue.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
2481
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    71
    public static void setDefaultQueue(LayoutQueue defaultQueue) {
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    72
        synchronized (DEFAULT_QUEUE) {
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    73
            AppContext ac = AppContext.getAppContext();
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    74
            if (defaultQueue == null) {
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    75
                ac.remove(DEFAULT_QUEUE);
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    76
            } else {
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    77
                ac.put(DEFAULT_QUEUE, defaultQueue);
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    78
            }
3aa9107f1b72 6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents: 1287
diff changeset
    79
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Add a task that is not needed immediately because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * the results are not believed to be visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public synchronized void addTask(Runnable task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (worker == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            worker = new LayoutThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            worker.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        tasks.addElement(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Used by the worker thread to get a new task to execute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected synchronized Runnable waitForWork() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        while (tasks.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   106
        Runnable work = tasks.firstElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        tasks.removeElementAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return work;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * low priority thread to perform layout work forever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    class LayoutThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        LayoutThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            super("text-layout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            setPriority(Thread.MIN_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            Runnable work;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                work = waitForWork();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                if (work != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    work.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            } while (work != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
}