KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > resource > URLImageDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.resource;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.URL JavaDoc;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.SWTException;
20 import org.eclipse.swt.graphics.ImageData;
21
22 /**
23  * An ImageDescriptor that gets its information from a URL.
24  * This class is not public API. Use ImageDescriptor#createFromURL
25  * to create a descriptor that uses a URL.
26  */

27 class URLImageDescriptor extends ImageDescriptor {
28     private URL JavaDoc url;
29
30     /**
31      * Creates a new URLImageDescriptor.
32      * @param url The URL to load the image from. Must be non-null.
33      */

34     URLImageDescriptor(URL JavaDoc url) {
35         this.url = url;
36     }
37
38     /* (non-Javadoc)
39      * Method declared on Object.
40      */

41     public boolean equals(Object JavaDoc o) {
42         if (!(o instanceof URLImageDescriptor)) {
43             return false;
44         }
45         return ((URLImageDescriptor) o).url.equals(this.url);
46     }
47
48     /* (non-Javadoc)
49      * Method declared on ImageDesciptor.
50      * Returns null if the image data cannot be read.
51      */

52     public ImageData getImageData() {
53         ImageData result = null;
54         InputStream JavaDoc in = getStream();
55         if (in != null) {
56             try {
57                 result = new ImageData(in);
58             } catch (SWTException e) {
59                 if (e.code != SWT.ERROR_INVALID_IMAGE) {
60                     throw e;
61                 // fall through otherwise
62
}
63             } finally {
64                 try {
65                     in.close();
66                 } catch (IOException JavaDoc e) {
67                     //System.err.println(getClass().getName()+".getImageData(): "+
68
// "Exception while closing InputStream : "+e);
69
}
70             }
71         }
72         return result;
73     }
74
75     /**
76      * Returns a stream on the image contents. Returns
77      * null if a stream could not be opened.
78      * @return the stream for loading the data
79      */

80     protected InputStream JavaDoc getStream() {
81         try {
82             return new BufferedInputStream JavaDoc(url.openStream());
83         } catch (IOException JavaDoc e) {
84             return null;
85         }
86     }
87
88     /* (non-Javadoc)
89      * Method declared on Object.
90      */

91     public int hashCode() {
92         return url.hashCode();
93     }
94
95     /* (non-Javadoc)
96      * Method declared on Object.
97      */

98     /**
99      * The <code>URLImageDescriptor</code> implementation of this <code>Object</code> method
100      * returns a string representation of this object which is suitable only for debugging.
101      */

102     public String JavaDoc toString() {
103         return "URLImageDescriptor(" + url + ")"; //$NON-NLS-1$ //$NON-NLS-2$
104
}
105 }
106
Popular Tags