01: package net.refractions.udig.internal.ui;
02:
03: import java.io.BufferedInputStream;
04: import java.io.IOException;
05: import java.io.InputStream;
06: import java.net.URL;
07:
08: import org.eclipse.jface.resource.ImageDescriptor;
09: import org.eclipse.swt.SWT;
10: import org.eclipse.swt.SWTException;
11: import org.eclipse.swt.graphics.ImageData;
12:
13: /**
14: * An ImageDescriptor that gets its information from a URL.
15: * This class is not public API. Use ImageDescriptor#createFromURL
16: * to create a descriptor that uses a URL.
17: */
18: public class URLImageDescriptor extends ImageDescriptor {
19: private URL url;
20:
21: /**
22: * Creates a new URLImageDescriptor.
23: * @param url The URL to load the image from. Must be non-null.
24: */
25: public URLImageDescriptor(URL url) {
26: this .url = url;
27: }
28:
29: /* (non-Javadoc)
30: * Method declared on Object.
31: */
32: public boolean equals(Object o) {
33: if (!(o instanceof URLImageDescriptor)) {
34: return false;
35: }
36: return ((URLImageDescriptor) o).url.equals(this .url);
37: }
38:
39: /* (non-Javadoc)
40: * Method declared on ImageDesciptor.
41: * Returns null if the image data cannot be read.
42: */
43: public ImageData getImageData() {
44: ImageData result = null;
45: InputStream in = getStream();
46: if (in != null) {
47: try {
48: result = new ImageData(in);
49: } catch (SWTException e) {
50: if (e.code != SWT.ERROR_INVALID_IMAGE)
51: throw e;
52: // fall through otherwise
53: } finally {
54: try {
55: in.close();
56: } catch (IOException e) {
57: //System.err.println(getClass().getName()+".getImageData(): "+
58: // "Exception while closing InputStream : "+e);
59: }
60: }
61: }
62: return result;
63: }
64:
65: /**
66: * Returns a stream on the image contents. Returns
67: * null if a stream could not be opened.
68: * @return the stream for loading the data
69: */
70: protected InputStream getStream() {
71: try {
72: return new BufferedInputStream(url.openStream());
73: } catch (IOException e) {
74: return null;
75: }
76: }
77:
78: /* (non-Javadoc)
79: * Method declared on Object.
80: */
81: public int hashCode() {
82: return url.hashCode();
83: }
84:
85: /* (non-Javadoc)
86: * Method declared on Object.
87: */
88: /**
89: * The <code>URLImageDescriptor</code> implementation of this <code>Object</code> method
90: * returns a string representation of this object which is suitable only for debugging.
91: */
92: public String toString() {
93: return "URLImageDescriptor(" + url + ")"; //$NON-NLS-1$ //$NON-NLS-2$
94: }
95: }
|