KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.SWTException;
21 import org.eclipse.swt.graphics.ImageData;
22
23 /**
24  * An image descriptor that loads its image information
25  * from a file.
26  */

27 class FileImageDescriptor extends ImageDescriptor {
28
29     /**
30      * The class whose resource directory contain the file,
31      * or <code>null</code> if none.
32      */

33     private Class JavaDoc location;
34
35     /**
36      * The name of the file.
37      */

38     private String JavaDoc name;
39
40     /**
41      * Creates a new file image descriptor.
42      * The file has the given file name and is located
43      * in the given class's resource directory. If the given
44      * class is <code>null</code>, the file name must be absolute.
45      * <p>
46      * Note that the file is not accessed until its
47      * <code>getImageDate</code> method is called.
48      * </p>
49      *
50      * @param clazz class for resource directory, or
51      * <code>null</code>
52      * @param filename the name of the file
53      */

54     FileImageDescriptor(Class JavaDoc clazz, String JavaDoc filename) {
55         this.location = clazz;
56         this.name = filename;
57     }
58
59     /* (non-Javadoc)
60      * Method declared on Object.
61      */

62     public boolean equals(Object JavaDoc o) {
63         if (!(o instanceof FileImageDescriptor)) {
64             return false;
65         }
66         FileImageDescriptor other = (FileImageDescriptor) o;
67         if (location != null) {
68             if (!location.equals(other.location)) {
69                 return false;
70             }
71         } else {
72             if (other.location != null) {
73                 return false;
74             }
75         }
76         return name.equals(other.name);
77     }
78
79     /* (non-Javadoc)
80      * Method declared on ImageDesciptor.
81      * Returns null if the image data cannot be read.
82      */

83     public ImageData getImageData() {
84         InputStream JavaDoc in = getStream();
85         ImageData result = null;
86         if (in != null) {
87             try {
88                 result = new ImageData(in);
89             } catch (SWTException e) {
90                 if (e.code != SWT.ERROR_INVALID_IMAGE) {
91                     throw e;
92                 // fall through otherwise
93
}
94             } finally {
95                 try {
96                     in.close();
97                 } catch (IOException JavaDoc e) {
98                     //System.err.println(getClass().getName()+".getImageData(): "+
99
// "Exception while closing InputStream : "+e);
100
}
101             }
102         }
103         return result;
104     }
105
106     /**
107      * Returns a stream on the image contents. Returns
108      * null if a stream could not be opened.
109      *
110      * @return the buffered stream on the file or <code>null</code>
111      * if the file cannot be found
112      */

113     private InputStream JavaDoc getStream() {
114         InputStream JavaDoc is = null;
115
116         if (location != null) {
117             is = location.getResourceAsStream(name);
118
119         } else {
120             try {
121                 is = new FileInputStream JavaDoc(name);
122             } catch (FileNotFoundException JavaDoc e) {
123                 return null;
124             }
125         }
126         if (is == null) {
127             return null;
128         } else {
129             return new BufferedInputStream JavaDoc(is);
130         }
131     }
132
133     /* (non-Javadoc)
134      * Method declared on Object.
135      */

136     public int hashCode() {
137         int code = name.hashCode();
138         if (location != null) {
139             code += location.hashCode();
140         }
141         return code;
142     }
143
144     /* (non-Javadoc)
145      * Method declared on Object.
146      */

147     /**
148      * The <code>FileImageDescriptor</code> implementation of this <code>Object</code> method
149      * returns a string representation of this object which is suitable only for debugging.
150      */

151     public String JavaDoc toString() {
152         return "FileImageDescriptor(location=" + location + ", name=" + name + ")";//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
153
}
154 }
155
Popular Tags