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