001: /*
002: * Copyright 1995-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.image;
027:
028: import java.lang.ref.WeakReference;
029: import java.awt.Image;
030: import java.awt.image.ImageObserver;
031:
032: public abstract class ImageWatched {
033: public static Link endlink = new Link();
034:
035: public Link watcherList;
036:
037: public ImageWatched() {
038: watcherList = endlink;
039: }
040:
041: /*
042: * This class defines a node on a linked list of ImageObservers.
043: * The base class defines the dummy implementation used for the
044: * last link on all chains and a subsequent subclass then
045: * defines the standard implementation that manages a weak
046: * reference to a real ImageObserver.
047: */
048: public static class Link {
049: /*
050: * Check if iw is the referent of this Link or any
051: * subsequent Link objects on this chain.
052: */
053: public boolean isWatcher(ImageObserver iw) {
054: return false; // No "iw" down here.
055: }
056:
057: /*
058: * Remove this Link from the chain if its referent
059: * is the indicated target or if it has been nulled
060: * out by the garbage collector.
061: * Return the new remainder of the chain.
062: * The argument may be null which will trigger
063: * the chain to remove only the dead (null) links.
064: * This method is only ever called inside a
065: * synchronized block so Link.next modifications
066: * will be safe.
067: */
068: public Link removeWatcher(ImageObserver iw) {
069: return this ; // Leave me as the end link.
070: }
071:
072: /*
073: * Deliver the indicated image update information
074: * to the referent of this Link and return a boolean
075: * indicating whether or not some referent became
076: * null or has indicated a lack of interest in
077: * further updates to its imageUpdate() method.
078: * This method is not called inside a synchronized
079: * block so Link.next modifications are not safe.
080: */
081: public boolean newInfo(Image img, int info, int x, int y,
082: int w, int h) {
083: return false; // No disinterested parties down here.
084: }
085: }
086:
087: /*
088: * Standard Link implementation to manage a Weak Reference
089: * to an ImageObserver.
090: */
091: public static class WeakLink extends Link {
092: private WeakReference<ImageObserver> myref;
093: private Link next;
094:
095: public WeakLink(ImageObserver obs, Link next) {
096: myref = new WeakReference<ImageObserver>(obs);
097: this .next = next;
098: }
099:
100: public boolean isWatcher(ImageObserver iw) {
101: return (myref.get() == iw || next.isWatcher(iw));
102: }
103:
104: public Link removeWatcher(ImageObserver iw) {
105: ImageObserver myiw = myref.get();
106: if (myiw == null) {
107: // Remove me from the chain, but continue recursion.
108: return next.removeWatcher(iw);
109: }
110: // At this point myiw is not null so we know this test will
111: // never succeed if this is a pruning pass (iw == null).
112: if (myiw == iw) {
113: // Remove me from the chain and end the recursion here.
114: return next;
115: }
116: // I am alive, but not the one to be removed, recurse
117: // and update my next link and leave me in the chain.
118: next = next.removeWatcher(iw);
119: return this ;
120: }
121:
122: public boolean newInfo(Image img, int info, int x, int y,
123: int w, int h) {
124: // Note tail recursion because items are added LIFO.
125: boolean ret = next.newInfo(img, info, x, y, w, h);
126: ImageObserver myiw = myref.get();
127: if (myiw == null) {
128: // My referent is null so we must prune in a second pass.
129: ret = true;
130: } else if (myiw.imageUpdate(img, info, x, y, w, h) == false) {
131: // My referent has lost interest so clear it and ask
132: // for a pruning pass to remove it later.
133: myref.clear();
134: ret = true;
135: }
136: return ret;
137: }
138: }
139:
140: public synchronized void addWatcher(ImageObserver iw) {
141: if (iw != null && !isWatcher(iw)) {
142: watcherList = new WeakLink(iw, watcherList);
143: }
144: }
145:
146: public synchronized boolean isWatcher(ImageObserver iw) {
147: return watcherList.isWatcher(iw);
148: }
149:
150: public void removeWatcher(ImageObserver iw) {
151: synchronized (this ) {
152: watcherList = watcherList.removeWatcher(iw);
153: }
154: if (watcherList == endlink) {
155: notifyWatcherListEmpty();
156: }
157: }
158:
159: public boolean isWatcherListEmpty() {
160: synchronized (this ) {
161: watcherList = watcherList.removeWatcher(null);
162: }
163: return (watcherList == endlink);
164: }
165:
166: public void newInfo(Image img, int info, int x, int y, int w, int h) {
167: if (watcherList.newInfo(img, info, x, y, w, h)) {
168: // Some Link returned true so we now need to prune dead links.
169: removeWatcher(null);
170: }
171: }
172:
173: protected abstract void notifyWatcherListEmpty();
174: }
|