jdk/src/share/classes/sun/awt/image/ImageWatched.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 12388 5a31b31ab09b
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.ImageObserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public abstract class ImageWatched {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    public static Link endlink = new Link();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    public Link watcherList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public ImageWatched() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        watcherList = endlink;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * This class defines a node on a linked list of ImageObservers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * The base class defines the dummy implementation used for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * last link on all chains and a subsequent subclass then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * defines the standard implementation that manages a weak
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * reference to a real ImageObserver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static class Link {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
         * Check if iw is the referent of this Link or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
         * subsequent Link objects on this chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        public boolean isWatcher(ImageObserver iw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            return false;  // No "iw" down here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
         * Remove this Link from the chain if its referent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
         * is the indicated target or if it has been nulled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
         * out by the garbage collector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
         * Return the new remainder of the chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
         * The argument may be null which will trigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         * the chain to remove only the dead (null) links.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         * This method is only ever called inside a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
         * synchronized block so Link.next modifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         * will be safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        public Link removeWatcher(ImageObserver iw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return this;  // Leave me as the end link.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         * Deliver the indicated image update information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         * to the referent of this Link and return a boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         * indicating whether or not some referent became
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         * null or has indicated a lack of interest in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         * further updates to its imageUpdate() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         * This method is not called inside a synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
         * block so Link.next modifications are not safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public boolean newInfo(Image img, int info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                               int x, int y, int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return false;  // No disinterested parties down here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Standard Link implementation to manage a Weak Reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * to an ImageObserver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static class WeakLink extends Link {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        private WeakReference<ImageObserver> myref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        private Link next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        public WeakLink(ImageObserver obs, Link next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            myref = new WeakReference<ImageObserver>(obs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        public boolean isWatcher(ImageObserver iw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return (myref.get() == iw || next.isWatcher(iw));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        public Link removeWatcher(ImageObserver iw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            ImageObserver myiw = myref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (myiw == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                // Remove me from the chain, but continue recursion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                return next.removeWatcher(iw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            // At this point myiw is not null so we know this test will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // never succeed if this is a pruning pass (iw == null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (myiw == iw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                // Remove me from the chain and end the recursion here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            // I am alive, but not the one to be removed, recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // and update my next link and leave me in the chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            next = next.removeWatcher(iw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        public boolean newInfo(Image img, int info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                               int x, int y, int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            // Note tail recursion because items are added LIFO.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            boolean ret = next.newInfo(img, info, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            ImageObserver myiw = myref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (myiw == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                // My referent is null so we must prune in a second pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                ret = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            } else if (myiw.imageUpdate(img, info, x, y, w, h) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                // My referent has lost interest so clear it and ask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                // for a pruning pass to remove it later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                myref.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                ret = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public synchronized void addWatcher(ImageObserver iw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (iw != null && !isWatcher(iw)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            watcherList = new WeakLink(iw, watcherList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public synchronized boolean isWatcher(ImageObserver iw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return watcherList.isWatcher(iw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void removeWatcher(ImageObserver iw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            watcherList = watcherList.removeWatcher(iw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (watcherList == endlink) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            notifyWatcherListEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public boolean isWatcherListEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            watcherList = watcherList.removeWatcher(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return (watcherList == endlink);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public void newInfo(Image img, int info, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (watcherList.newInfo(img, info, x, y, w, h)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // Some Link returned true so we now need to prune dead links.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            removeWatcher(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    protected abstract void notifyWatcherListEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}