2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1995, 2003, Oracle and/or its affiliates. All rights reserved.
|
2
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.awt.image;
|
|
27 |
|
|
28 |
import java.lang.ref.WeakReference;
|
|
29 |
import java.awt.Image;
|
|
30 |
import java.awt.image.ImageObserver;
|
|
31 |
|
|
32 |
public abstract class ImageWatched {
|
|
33 |
public static Link endlink = new Link();
|
|
34 |
|
|
35 |
public Link watcherList;
|
|
36 |
|
|
37 |
public ImageWatched() {
|
|
38 |
watcherList = endlink;
|
|
39 |
}
|
|
40 |
|
|
41 |
/*
|
|
42 |
* This class defines a node on a linked list of ImageObservers.
|
|
43 |
* The base class defines the dummy implementation used for the
|
|
44 |
* last link on all chains and a subsequent subclass then
|
|
45 |
* defines the standard implementation that manages a weak
|
|
46 |
* reference to a real ImageObserver.
|
|
47 |
*/
|
|
48 |
public static class Link {
|
|
49 |
/*
|
|
50 |
* Check if iw is the referent of this Link or any
|
|
51 |
* subsequent Link objects on this chain.
|
|
52 |
*/
|
|
53 |
public boolean isWatcher(ImageObserver iw) {
|
|
54 |
return false; // No "iw" down here.
|
|
55 |
}
|
|
56 |
|
|
57 |
/*
|
|
58 |
* Remove this Link from the chain if its referent
|
|
59 |
* is the indicated target or if it has been nulled
|
|
60 |
* out by the garbage collector.
|
|
61 |
* Return the new remainder of the chain.
|
|
62 |
* The argument may be null which will trigger
|
|
63 |
* the chain to remove only the dead (null) links.
|
|
64 |
* This method is only ever called inside a
|
|
65 |
* synchronized block so Link.next modifications
|
|
66 |
* will be safe.
|
|
67 |
*/
|
|
68 |
public Link removeWatcher(ImageObserver iw) {
|
|
69 |
return this; // Leave me as the end link.
|
|
70 |
}
|
|
71 |
|
|
72 |
/*
|
|
73 |
* Deliver the indicated image update information
|
|
74 |
* to the referent of this Link and return a boolean
|
|
75 |
* indicating whether or not some referent became
|
|
76 |
* null or has indicated a lack of interest in
|
|
77 |
* further updates to its imageUpdate() method.
|
|
78 |
* This method is not called inside a synchronized
|
|
79 |
* block so Link.next modifications are not safe.
|
|
80 |
*/
|
|
81 |
public boolean newInfo(Image img, int info,
|
|
82 |
int x, int y, int w, int h)
|
|
83 |
{
|
|
84 |
return false; // No disinterested parties down here.
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
/*
|
|
89 |
* Standard Link implementation to manage a Weak Reference
|
|
90 |
* to an ImageObserver.
|
|
91 |
*/
|
|
92 |
public static class WeakLink extends Link {
|
|
93 |
private WeakReference<ImageObserver> myref;
|
|
94 |
private Link next;
|
|
95 |
|
|
96 |
public WeakLink(ImageObserver obs, Link next) {
|
|
97 |
myref = new WeakReference<ImageObserver>(obs);
|
|
98 |
this.next = next;
|
|
99 |
}
|
|
100 |
|
|
101 |
public boolean isWatcher(ImageObserver iw) {
|
|
102 |
return (myref.get() == iw || next.isWatcher(iw));
|
|
103 |
}
|
|
104 |
|
|
105 |
public Link removeWatcher(ImageObserver iw) {
|
|
106 |
ImageObserver myiw = myref.get();
|
|
107 |
if (myiw == null) {
|
|
108 |
// Remove me from the chain, but continue recursion.
|
|
109 |
return next.removeWatcher(iw);
|
|
110 |
}
|
|
111 |
// At this point myiw is not null so we know this test will
|
|
112 |
// never succeed if this is a pruning pass (iw == null).
|
|
113 |
if (myiw == iw) {
|
|
114 |
// Remove me from the chain and end the recursion here.
|
|
115 |
return next;
|
|
116 |
}
|
|
117 |
// I am alive, but not the one to be removed, recurse
|
|
118 |
// and update my next link and leave me in the chain.
|
|
119 |
next = next.removeWatcher(iw);
|
|
120 |
return this;
|
|
121 |
}
|
|
122 |
|
|
123 |
public boolean newInfo(Image img, int info,
|
|
124 |
int x, int y, int w, int h)
|
|
125 |
{
|
|
126 |
// Note tail recursion because items are added LIFO.
|
|
127 |
boolean ret = next.newInfo(img, info, x, y, w, h);
|
|
128 |
ImageObserver myiw = myref.get();
|
|
129 |
if (myiw == null) {
|
|
130 |
// My referent is null so we must prune in a second pass.
|
|
131 |
ret = true;
|
|
132 |
} else if (myiw.imageUpdate(img, info, x, y, w, h) == false) {
|
|
133 |
// My referent has lost interest so clear it and ask
|
|
134 |
// for a pruning pass to remove it later.
|
|
135 |
myref.clear();
|
|
136 |
ret = true;
|
|
137 |
}
|
|
138 |
return ret;
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
public synchronized void addWatcher(ImageObserver iw) {
|
|
143 |
if (iw != null && !isWatcher(iw)) {
|
|
144 |
watcherList = new WeakLink(iw, watcherList);
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
public synchronized boolean isWatcher(ImageObserver iw) {
|
|
149 |
return watcherList.isWatcher(iw);
|
|
150 |
}
|
|
151 |
|
|
152 |
public void removeWatcher(ImageObserver iw) {
|
|
153 |
synchronized (this) {
|
|
154 |
watcherList = watcherList.removeWatcher(iw);
|
|
155 |
}
|
|
156 |
if (watcherList == endlink) {
|
|
157 |
notifyWatcherListEmpty();
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
public boolean isWatcherListEmpty() {
|
|
162 |
synchronized (this) {
|
|
163 |
watcherList = watcherList.removeWatcher(null);
|
|
164 |
}
|
|
165 |
return (watcherList == endlink);
|
|
166 |
}
|
|
167 |
|
|
168 |
public void newInfo(Image img, int info, int x, int y, int w, int h) {
|
|
169 |
if (watcherList.newInfo(img, info, x, y, w, h)) {
|
|
170 |
// Some Link returned true so we now need to prune dead links.
|
|
171 |
removeWatcher(null);
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
protected abstract void notifyWatcherListEmpty();
|
|
176 |
}
|