001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import java.awt.Image;
023: import java.awt.Toolkit;
024: import java.awt.image.ImageObserver;
025: import java.net.URL;
026:
027: /**
028: * Loads an image asynchronously, that is in another thread.
029: */
030: class BackgroundImageLoader implements ImageObserver {
031: final int desiredWidth;
032: final int desiredHeight;
033:
034: final Image image;
035:
036: private int imageWidth = -1;
037: private int imageHeight = -1;
038:
039: private volatile boolean ready;
040: private volatile boolean error;
041:
042: private boolean synchronous;
043: Object lock = new Object();
044:
045: final Toolkit tk = Toolkit.getDefaultToolkit();
046:
047: BackgroundImageLoader(final URL url, boolean synchronous,
048: final int desiredWidth, final int desiredHeight) {
049: this .desiredWidth = desiredWidth;
050: this .desiredHeight = desiredHeight;
051:
052: this .synchronous = synchronous;
053:
054: error = url == null;
055: if (!error) {
056: image = tk.createImage(url);
057: if (synchronous) {
058: waitForImage();
059: } else {
060: tk.prepareImage(image, desiredWidth, desiredHeight,
061: this );
062: }
063: } else {
064: image = null;
065: }
066: }
067:
068: public boolean imageUpdate(final Image image, final int flags,
069: final int x, final int y, final int width, final int height) {
070: if ((flags & WIDTH) != 0) {
071: imageWidth = desiredWidth == -1 ? width : desiredWidth;
072: }
073: if ((flags & HEIGHT) != 0) {
074: imageHeight = desiredHeight == -1 ? height : desiredHeight;
075: }
076: if ((flags & (FRAMEBITS | ALLBITS)) != 0) {
077: ready = true;
078: onReady();
079: }
080: if ((flags & (ERROR | ABORT)) != 0) {
081: error = true;
082: onError();
083: }
084:
085: if (synchronous) {
086: synchronized (lock) {
087: lock.notify();
088: }
089: }
090:
091: return (flags & ALLBITS) == 0;
092: }
093:
094: public final Image getImage() {
095: return image;
096: }
097:
098: public final int getWidth() {
099: return imageWidth;
100: }
101:
102: public final int getHeight() {
103: return imageHeight;
104: }
105:
106: public final boolean isReady() {
107: return ready;
108: }
109:
110: public final boolean isError() {
111: return error;
112: }
113:
114: public final void waitForImage() {
115: synchronized (lock) {
116: if (!tk.prepareImage(image, desiredWidth, desiredHeight,
117: this )) {
118: while (!(error | ready)) {
119: try {
120: lock.wait(1000);
121: } catch (InterruptedException e) {
122: Thread.currentThread().interrupt();
123: }
124: }
125: }
126: }
127: }
128:
129: protected void onReady() {
130: }
131:
132: protected void onError() {
133: }
134: }
|