2
|
1 |
/*
|
|
2 |
* Copyright 1999 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
package javax.swing.text;
|
|
26 |
|
|
27 |
import java.util.Vector;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* A queue of text layout tasks.
|
|
31 |
*
|
|
32 |
* @author Timothy Prinzing
|
|
33 |
* @see AsyncBoxView
|
|
34 |
* @since 1.3
|
|
35 |
*/
|
|
36 |
public class LayoutQueue {
|
|
37 |
|
|
38 |
Vector tasks;
|
|
39 |
Thread worker;
|
|
40 |
|
|
41 |
static LayoutQueue defaultQueue;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Construct a layout queue.
|
|
45 |
*/
|
|
46 |
public LayoutQueue() {
|
|
47 |
tasks = new Vector();
|
|
48 |
}
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Fetch the default layout queue.
|
|
52 |
*/
|
|
53 |
public static LayoutQueue getDefaultQueue() {
|
|
54 |
if (defaultQueue == null) {
|
|
55 |
defaultQueue = new LayoutQueue();
|
|
56 |
}
|
|
57 |
return defaultQueue;
|
|
58 |
}
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Set the default layout queue.
|
|
62 |
*
|
|
63 |
* @param q the new queue.
|
|
64 |
*/
|
|
65 |
public static void setDefaultQueue(LayoutQueue q) {
|
|
66 |
defaultQueue = q;
|
|
67 |
}
|
|
68 |
|
|
69 |
/**
|
|
70 |
* Add a task that is not needed immediately because
|
|
71 |
* the results are not believed to be visible.
|
|
72 |
*/
|
|
73 |
public synchronized void addTask(Runnable task) {
|
|
74 |
if (worker == null) {
|
|
75 |
worker = new LayoutThread();
|
|
76 |
worker.start();
|
|
77 |
}
|
|
78 |
tasks.addElement(task);
|
|
79 |
notifyAll();
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
|
83 |
* Used by the worker thread to get a new task to execute
|
|
84 |
*/
|
|
85 |
protected synchronized Runnable waitForWork() {
|
|
86 |
while (tasks.size() == 0) {
|
|
87 |
try {
|
|
88 |
wait();
|
|
89 |
} catch (InterruptedException ie) {
|
|
90 |
return null;
|
|
91 |
}
|
|
92 |
}
|
|
93 |
Runnable work = (Runnable) tasks.firstElement();
|
|
94 |
tasks.removeElementAt(0);
|
|
95 |
return work;
|
|
96 |
}
|
|
97 |
|
|
98 |
/**
|
|
99 |
* low priority thread to perform layout work forever
|
|
100 |
*/
|
|
101 |
class LayoutThread extends Thread {
|
|
102 |
|
|
103 |
LayoutThread() {
|
|
104 |
super("text-layout");
|
|
105 |
setPriority(Thread.MIN_PRIORITY);
|
|
106 |
}
|
|
107 |
|
|
108 |
public void run() {
|
|
109 |
Runnable work;
|
|
110 |
do {
|
|
111 |
work = waitForWork();
|
|
112 |
if (work != null) {
|
|
113 |
work.run();
|
|
114 |
}
|
|
115 |
} while (work != null);
|
|
116 |
}
|
|
117 |
|
|
118 |
|
|
119 |
}
|
|
120 |
|
|
121 |
}
|