2
* Copyright (c) 2007, Sun Microsystems, Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* * Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* * Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in
12
* the documentation and/or other materials provided with the distribution.
13
* * Neither the name of Sun Microsystems, Inc. nor the names of its
14
* contributors may be used to endorse or promote products derived
15
* from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
package marsroverviewer;
32
import org.jdesktop.application.Application;
33
import org.jdesktop.application.Task;
34
import java.awt.image.BufferedImage;
35
import java.io.IOException;
37
import java.util.Iterator;
38
import java.util.concurrent.TimeUnit;
39
import java.util.logging.Level;
40
import java.util.logging.Logger;
41
import javax.imageio.ImageIO;
42
import javax.imageio.ImageReader;
43
import javax.imageio.event.IIOReadProgressListener;
44
import javax.imageio.stream.ImageInputStream;
47
* A Task that loads an image from a URL. Loading and decoding
48
* progress is reported via the Task <code>progress</code> property
49
* and messages are generated when the Task begins and when it
50
* finishes. If errors occur then warnings are logged and the
51
* Task's value is null.
53
* Applications would typically use LoadImageTask by creating
54
* a private subclass that overrode the <code>done</code> method.
55
* See SingleFrameExample5.java for an example.
57
* Resources for this class are in the
58
* <code>resources/LoadImageTask.properties</code> ResourceBundle.
61
public class LoadImageTask extends Task<BufferedImage, Void> {
62
private static Logger logger = Logger.getLogger(LoadImageTask.class.getName());
63
private final URL url;
64
private ImageReader imageReader = null;
66
public LoadImageTask(Application app, URL url) {
67
super(app, "LoadImageTask"); // init title/description/messages
71
protected final URL getImageURL() {
75
@Override protected BufferedImage doInBackground() {
76
IIOReadProgressListener rpl = new IIOReadProgressAdapter() {
77
public void imageProgress(ImageReader r, float p) {
78
setProgress(p, 0.0f, 100.0f);
81
message("startedLoadingImage", url);
82
imageReader = findImageReader(url);
83
return loadImage(imageReader, rpl);
86
private void completionMessage(String resourceKey) {
87
message(resourceKey, url, getExecutionDuration(TimeUnit.MILLISECONDS));
90
@Override protected void cancelled() {
91
if (imageReader != null) {
94
completionMessage("cancelledLoadingImage");
97
@Override protected void interrupted(InterruptedException e) {
98
if (imageReader != null) {
101
completionMessage("cancelledLoadingImage");
104
@Override protected void succeeded(BufferedImage image) {
105
completionMessage("finishedLoadingImage");
108
@Override protected void failed(Throwable e) {
109
completionMessage("failedLoadingImage");
112
@Override protected void finished() {
117
/* The methods below are what's required by the Java imaging
118
* API to enable tracking the progress of an ImageIO read()
119
* and optionally aborting it. If we weren't interested in
120
* tracking image-loading progress or supporting Task.cancel()
121
* it would be enough to just use ImageIO.read().
123
private ImageReader findImageReader(URL url) {
124
ImageInputStream input = null;
126
input = ImageIO.createImageInputStream(url.openStream());
128
catch(IOException e) {
129
logger.log(Level.WARNING, "bad image URL " + url, e);
131
ImageReader reader = null;
133
Iterator readers = ImageIO.getImageReaders(input);
134
while((reader == null) && (readers != null) && readers.hasNext()) {
135
reader = (ImageReader)readers.next();
137
reader.setInput(input);
142
private BufferedImage loadImage(ImageReader reader, IIOReadProgressListener listener) {
143
BufferedImage image = null;
145
if (listener != null) {
146
reader.addIIOReadProgressListener(listener);
148
int index = reader.getMinIndex();
149
image = reader.read(index);
151
catch (IOException e) {
152
logger.log(Level.WARNING, "loadImage failed", e);
155
ImageInputStream input = (ImageInputStream)(reader.getInput());
157
try { input.close(); } catch (IOException e) { }
159
if (reader != null) {
160
reader.removeAllIIOReadProgressListeners();
167
/* Makes creating an IIOReadProgressListener less horrible looking; see
168
* LoadImageTask.doInBackground() above.
170
private static class IIOReadProgressAdapter implements IIOReadProgressListener {
171
public void imageStarted(ImageReader rdr, int imageIndex) { }
172
public void imageProgress(ImageReader rdr, float percentageDone) { }
173
public void imageComplete(ImageReader rdr) { }
174
public void readAborted(ImageReader rdr) { }
175
public void sequenceStarted(ImageReader rdr, int minIndex) { }
176
public void sequenceComplete(ImageReader rdr) { }
177
public void thumbnailStarted(ImageReader rdr, int imageIndex, int thumbIndex) { }
178
public void thumbnailProgress(ImageReader rdr, float percentageDone) { }
179
public void thumbnailComplete(ImageReader rdr) { }