4
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.resource.FileImageDescriptor;
|
|
14
|
|
15 import dwtx.jface.resource.ImageDescriptor;
|
|
16
|
|
17 // import java.io.BufferedInputStream;
|
|
18 // import java.io.FileInputStream;
|
|
19 // import java.io.FileNotFoundException;
|
|
20 // import java.io.IOException;
|
|
21 // import java.io.InputStream;
|
|
22
|
|
23 import dwt.DWT;
|
|
24 import dwt.DWTException;
|
|
25 import dwt.graphics.ImageData;
|
|
26
|
|
27 import dwt.dwthelper.utils;
|
|
28 import dwt.dwthelper.InputStream;
|
|
29 import dwt.dwthelper.FileInputStream;
|
|
30 import dwt.dwthelper.BufferedInputStream;
|
|
31
|
|
32 /**
|
|
33 * An image descriptor that loads its image information
|
|
34 * from a file.
|
|
35 */
|
|
36 class FileImageDescriptor : ImageDescriptor {
|
|
37
|
|
38 /**
|
|
39 * The class whose resource directory contain the file,
|
|
40 * or <code>null</code> if none.
|
|
41 */
|
|
42 private ClassInfo location;
|
|
43
|
|
44 /**
|
|
45 * The name of the file.
|
|
46 */
|
|
47 private String name;
|
|
48
|
|
49 /**
|
|
50 * Creates a new file image descriptor.
|
|
51 * The file has the given file name and is located
|
|
52 * in the given class's resource directory. If the given
|
|
53 * class is <code>null</code>, the file name must be absolute.
|
|
54 * <p>
|
|
55 * Note that the file is not accessed until its
|
|
56 * <code>getImageDate</code> method is called.
|
|
57 * </p>
|
|
58 *
|
|
59 * @param clazz class for resource directory, or
|
|
60 * <code>null</code>
|
|
61 * @param filename the name of the file
|
|
62 */
|
|
63 this(ClassInfo clazz, String filename) {
|
|
64 this.location = clazz;
|
|
65 this.name = filename;
|
|
66 }
|
|
67
|
|
68 /* (non-Javadoc)
|
|
69 * Method declared on Object.
|
|
70 */
|
|
71 public bool equals(Object o) {
|
|
72 if (!( cast(FileImageDescriptor)o )) {
|
|
73 return false;
|
|
74 }
|
|
75 FileImageDescriptor other = cast(FileImageDescriptor) o;
|
|
76 if (location !is null) {
|
|
77 if ( location.name != other.location.name ) {
|
|
78 return false;
|
|
79 }
|
|
80 } else {
|
|
81 if (other.location !is null) {
|
|
82 return false;
|
|
83 }
|
|
84 }
|
|
85 return name == other.name;
|
|
86 }
|
|
87
|
|
88 /* (non-Javadoc)
|
|
89 * Method declared on ImageDesciptor.
|
|
90 * Returns null if the image data cannot be read.
|
|
91 */
|
|
92 public ImageData getImageData() {
|
|
93 InputStream in_ = getStream();
|
|
94 ImageData result = null;
|
|
95 if (in_ !is null) {
|
|
96 try {
|
|
97 result = new ImageData(in_);
|
|
98 } catch (DWTException e) {
|
|
99 if (e.code !is DWT.ERROR_INVALID_IMAGE) {
|
|
100 throw e;
|
|
101 // fall through otherwise
|
|
102 }
|
|
103 } finally {
|
|
104 in_.close();
|
|
105 }
|
|
106 }
|
|
107 return result;
|
|
108 }
|
|
109
|
|
110 /**
|
|
111 * Returns a stream on the image contents. Returns
|
|
112 * null if a stream could not be opened.
|
|
113 *
|
|
114 * @return the buffered stream on the file or <code>null</code>
|
|
115 * if the file cannot be found
|
|
116 */
|
|
117 private InputStream getStream() {
|
|
118 InputStream is_ = null;
|
|
119
|
|
120 if (location !is null) {
|
|
121 is_ = ClassInfoGetResourceAsStream( location, name);
|
|
122
|
|
123 } else {
|
|
124 try {
|
|
125 is_ = new FileInputStream(name);
|
|
126 } catch (/+FileNotFoundException+/ IOException e) {
|
|
127 return null;
|
|
128 }
|
|
129 }
|
|
130 if (is_ is null) {
|
|
131 return null;
|
|
132 } else {
|
|
133 return new BufferedInputStream(is_);
|
|
134 }
|
|
135 }
|
|
136
|
|
137 /* (non-Javadoc)
|
|
138 * Method declared on Object.
|
|
139 */
|
|
140 public override hash_t toHash() {
|
|
141 int code = dwt.dwthelper.utils.toHash(name);
|
|
142 if (location !is null) {
|
|
143 code += location.toHash();
|
|
144 }
|
|
145 return code;
|
|
146 }
|
|
147
|
|
148 /* (non-Javadoc)
|
|
149 * Method declared on Object.
|
|
150 */
|
|
151 /**
|
|
152 * The <code>FileImageDescriptor</code> implementation of this <code>Object</code> method
|
|
153 * returns a string representation of this object which is suitable only for debugging.
|
|
154 */
|
|
155 public override String toString() {
|
|
156 return "FileImageDescriptor(location=" ~ location.toString() ~ ", name=" ~ name ~ ")";//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
|
|
157 }
|
|
158 }
|