0N/A/*
4900N/A * Copyright (c) 1995, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.awt.image;
0N/A
0N/Aimport java.lang.ref.WeakReference;
0N/Aimport java.awt.Image;
0N/Aimport java.awt.image.ImageObserver;
0N/A
0N/Apublic abstract class ImageWatched {
0N/A public static Link endlink = new Link();
0N/A
0N/A public Link watcherList;
0N/A
0N/A public ImageWatched() {
0N/A watcherList = endlink;
0N/A }
0N/A
0N/A /*
0N/A * This class defines a node on a linked list of ImageObservers.
0N/A * The base class defines the dummy implementation used for the
0N/A * last link on all chains and a subsequent subclass then
0N/A * defines the standard implementation that manages a weak
0N/A * reference to a real ImageObserver.
0N/A */
0N/A public static class Link {
0N/A /*
0N/A * Check if iw is the referent of this Link or any
0N/A * subsequent Link objects on this chain.
0N/A */
0N/A public boolean isWatcher(ImageObserver iw) {
0N/A return false; // No "iw" down here.
0N/A }
0N/A
0N/A /*
0N/A * Remove this Link from the chain if its referent
0N/A * is the indicated target or if it has been nulled
0N/A * out by the garbage collector.
0N/A * Return the new remainder of the chain.
0N/A * The argument may be null which will trigger
0N/A * the chain to remove only the dead (null) links.
0N/A * This method is only ever called inside a
0N/A * synchronized block so Link.next modifications
0N/A * will be safe.
0N/A */
0N/A public Link removeWatcher(ImageObserver iw) {
0N/A return this; // Leave me as the end link.
0N/A }
0N/A
0N/A /*
0N/A * Deliver the indicated image update information
0N/A * to the referent of this Link and return a boolean
0N/A * indicating whether or not some referent became
0N/A * null or has indicated a lack of interest in
0N/A * further updates to its imageUpdate() method.
0N/A * This method is not called inside a synchronized
0N/A * block so Link.next modifications are not safe.
0N/A */
0N/A public boolean newInfo(Image img, int info,
0N/A int x, int y, int w, int h)
0N/A {
0N/A return false; // No disinterested parties down here.
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Standard Link implementation to manage a Weak Reference
0N/A * to an ImageObserver.
0N/A */
0N/A public static class WeakLink extends Link {
0N/A private WeakReference<ImageObserver> myref;
0N/A private Link next;
0N/A
0N/A public WeakLink(ImageObserver obs, Link next) {
0N/A myref = new WeakReference<ImageObserver>(obs);
0N/A this.next = next;
0N/A }
0N/A
0N/A public boolean isWatcher(ImageObserver iw) {
0N/A return (myref.get() == iw || next.isWatcher(iw));
0N/A }
0N/A
0N/A public Link removeWatcher(ImageObserver iw) {
0N/A ImageObserver myiw = myref.get();
0N/A if (myiw == null) {
0N/A // Remove me from the chain, but continue recursion.
0N/A return next.removeWatcher(iw);
0N/A }
0N/A // At this point myiw is not null so we know this test will
0N/A // never succeed if this is a pruning pass (iw == null).
0N/A if (myiw == iw) {
0N/A // Remove me from the chain and end the recursion here.
0N/A return next;
0N/A }
0N/A // I am alive, but not the one to be removed, recurse
0N/A // and update my next link and leave me in the chain.
0N/A next = next.removeWatcher(iw);
0N/A return this;
0N/A }
0N/A
0N/A public boolean newInfo(Image img, int info,
0N/A int x, int y, int w, int h)
0N/A {
0N/A // Note tail recursion because items are added LIFO.
0N/A boolean ret = next.newInfo(img, info, x, y, w, h);
0N/A ImageObserver myiw = myref.get();
0N/A if (myiw == null) {
0N/A // My referent is null so we must prune in a second pass.
0N/A ret = true;
0N/A } else if (myiw.imageUpdate(img, info, x, y, w, h) == false) {
0N/A // My referent has lost interest so clear it and ask
0N/A // for a pruning pass to remove it later.
0N/A myref.clear();
0N/A ret = true;
0N/A }
0N/A return ret;
0N/A }
0N/A }
0N/A
0N/A public synchronized void addWatcher(ImageObserver iw) {
0N/A if (iw != null && !isWatcher(iw)) {
0N/A watcherList = new WeakLink(iw, watcherList);
0N/A }
4900N/A watcherList = watcherList.removeWatcher(null);
0N/A }
0N/A
0N/A public synchronized boolean isWatcher(ImageObserver iw) {
0N/A return watcherList.isWatcher(iw);
0N/A }
0N/A
0N/A public void removeWatcher(ImageObserver iw) {
0N/A synchronized (this) {
0N/A watcherList = watcherList.removeWatcher(iw);
0N/A }
0N/A if (watcherList == endlink) {
0N/A notifyWatcherListEmpty();
0N/A }
0N/A }
0N/A
0N/A public boolean isWatcherListEmpty() {
0N/A synchronized (this) {
0N/A watcherList = watcherList.removeWatcher(null);
0N/A }
0N/A return (watcherList == endlink);
0N/A }
0N/A
0N/A public void newInfo(Image img, int info, int x, int y, int w, int h) {
0N/A if (watcherList.newInfo(img, info, x, y, w, h)) {
0N/A // Some Link returned true so we now need to prune dead links.
0N/A removeWatcher(null);
0N/A }
0N/A }
0N/A
0N/A protected abstract void notifyWatcherListEmpty();
0N/A}