author | peterz |
Wed, 04 Feb 2009 18:48:24 +0300 | |
changeset 2481 | 3aa9107f1b72 |
parent 1287 | a04aca99c77a |
child 2484 | 9f3b45efb17d |
permissions | -rw-r--r-- |
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; |
|
2481
3aa9107f1b72
6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents:
1287
diff
changeset
|
28 |
import sun.awt.AppContext; |
2 | 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 |
||
2481
3aa9107f1b72
6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents:
1287
diff
changeset
|
39 |
private static final Object DEFAULT_QUEUE = new Object(); |
2 | 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 | 43 |
|
44 |
/** |
|
45 |
* Construct a layout queue. |
|
46 |
*/ |
|
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 | 49 |
} |
50 |
||
51 |
/** |
|
52 |
* Fetch the default layout queue. |
|
53 |
*/ |
|
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 | 63 |
} |
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* Set the default layout queue. |
|
68 |
* |
|
2481
3aa9107f1b72
6588003: LayoutQueue shares mutable implementation across AppContexts
peterz
parents:
1287
diff
changeset
|
69 |
* @param defaultQueue the new queue. |
2 | 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 | 80 |
} |
81 |
||
82 |
/** |
|
83 |
* Add a task that is not needed immediately because |
|
84 |
* the results are not believed to be visible. |
|
85 |
*/ |
|
86 |
public synchronized void addTask(Runnable task) { |
|
87 |
if (worker == null) { |
|
88 |
worker = new LayoutThread(); |
|
89 |
worker.start(); |
|
90 |
} |
|
91 |
tasks.addElement(task); |
|
92 |
notifyAll(); |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Used by the worker thread to get a new task to execute |
|
97 |
*/ |
|
98 |
protected synchronized Runnable waitForWork() { |
|
99 |
while (tasks.size() == 0) { |
|
100 |
try { |
|
101 |
wait(); |
|
102 |
} catch (InterruptedException ie) { |
|
103 |
return null; |
|
104 |
} |
|
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 | 107 |
tasks.removeElementAt(0); |
108 |
return work; |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* low priority thread to perform layout work forever |
|
113 |
*/ |
|
114 |
class LayoutThread extends Thread { |
|
115 |
||
116 |
LayoutThread() { |
|
117 |
super("text-layout"); |
|
118 |
setPriority(Thread.MIN_PRIORITY); |
|
119 |
} |
|
120 |
||
121 |
public void run() { |
|
122 |
Runnable work; |
|
123 |
do { |
|
124 |
work = waitForWork(); |
|
125 |
if (work != null) { |
|
126 |
work.run(); |
|
127 |
} |
|
128 |
} while (work != null); |
|
129 |
} |
|
130 |
||
131 |
||
132 |
} |
|
133 |
||
134 |
} |