Mercurial > projects > dwt-addons
comparison dwtx/jface/resource/FileImageDescriptor.d @ 70:46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 01:36:46 +0200 |
parents | ea8ff534f622 |
children | 4878bef4a38e |
comparison
equal
deleted
inserted
replaced
69:07b9d96fd764 | 70:46a6e0e6ccd4 |
---|---|
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2006 IBM Corporation and others. | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
20 // import java.io.IOException; | 20 // import java.io.IOException; |
21 // import java.io.InputStream; | 21 // import java.io.InputStream; |
22 | 22 |
23 import dwt.DWT; | 23 import dwt.DWT; |
24 import dwt.DWTException; | 24 import dwt.DWTException; |
25 import dwt.graphics.Device; | |
26 import dwt.graphics.Image; | |
25 import dwt.graphics.ImageData; | 27 import dwt.graphics.ImageData; |
28 import dwtx.core.runtime.Path; | |
26 | 29 |
27 import dwt.dwthelper.utils; | 30 import dwt.dwthelper.utils; |
28 import dwt.dwthelper.InputStream; | 31 import dwt.dwthelper.InputStream; |
29 import dwt.dwthelper.FileInputStream; | 32 import dwt.dwthelper.FileInputStream; |
30 import dwt.dwthelper.BufferedInputStream; | 33 import dwt.dwthelper.BufferedInputStream; |
31 import dwt.dwthelper.ByteArrayInputStream; | 34 import dwt.dwthelper.ByteArrayInputStream; |
32 | 35 |
33 import tango.util.log.Trace; | 36 import tango.util.log.Trace; |
37 import tango.text.convert.Format; | |
34 | 38 |
35 /** | 39 /** |
36 * An image descriptor that loads its image information | 40 * An image descriptor that loads its image information from a file. |
37 * from a file. | |
38 */ | 41 */ |
39 class FileImageDescriptor : ImageDescriptor { | 42 class FileImageDescriptor : ImageDescriptor { |
40 | 43 |
41 /** | 44 /** |
42 * The class whose resource directory contain the file, | 45 * The class whose resource directory contain the file, or <code>null</code> |
43 * or <code>null</code> if none. | 46 * if none. |
44 */ | 47 */ |
45 // private ClassInfo location; | 48 // private ClassInfo location; |
46 | 49 |
47 /** | 50 /** |
48 * The name of the file. | 51 * The name of the file. |
49 */ | 52 */ |
50 private String name; | 53 private String name; |
51 private void[] importdata; | 54 private void[] importdata; |
52 | 55 |
53 /** | 56 /** |
54 * Creates a new file image descriptor. | 57 * Creates a new file image descriptor. The file has the given file name and |
55 * The file has the given file name and is located | 58 * is located in the given class's resource directory. If the given class is |
56 * in the given class's resource directory. If the given | 59 * <code>null</code>, the file name must be absolute. |
57 * class is <code>null</code>, the file name must be absolute. | |
58 * <p> | 60 * <p> |
59 * Note that the file is not accessed until its | 61 * Note that the file is not accessed until its <code>getImageDate</code> |
60 * <code>getImageDate</code> method is called. | 62 * method is called. |
61 * </p> | 63 * </p> |
62 * | 64 * |
63 * @param clazz class for resource directory, or | 65 * @param clazz |
64 * <code>null</code> | 66 * class for resource directory, or <code>null</code> |
65 * @param filename the name of the file | 67 * @param filename |
68 * the name of the file | |
66 */ | 69 */ |
67 this(ImportData importdata) { | 70 this(ImportData importdata) { |
68 // this.location = clazz; | 71 // this.location = clazz; |
69 this.name = importdata.name; | 72 this.name = importdata.name; |
70 this.importdata = importdata.data; | 73 this.importdata = importdata.data; |
71 } | 74 } |
72 | 75 |
73 /* (non-Javadoc) | 76 /* |
74 * Method declared on Object. | 77 * (non-Javadoc) Method declared on Object. |
75 */ | 78 */ |
76 public override int opEquals(Object o) { | 79 public override int opEquals(Object o) { |
77 if (!( cast(FileImageDescriptor)o )) { | 80 if (!( cast(FileImageDescriptor)o )) { |
78 return false; | 81 return false; |
79 } | 82 } |
88 // } | 91 // } |
89 // } | 92 // } |
90 return importdata == other.importdata; | 93 return importdata == other.importdata; |
91 } | 94 } |
92 | 95 |
93 /* (non-Javadoc) | 96 /** |
94 * Method declared on ImageDesciptor. | 97 * @see dwtx.jface.resource.ImageDescriptor#getImageData() The |
95 * Returns null if the image data cannot be read. | 98 * FileImageDescriptor implementation of this method is not used by |
99 * {@link ImageDescriptor#createImage(bool, Device)} as of version | |
100 * 3.4 so that the DWT OS optimised loading can be used. | |
96 */ | 101 */ |
97 public override ImageData getImageData() { | 102 public override ImageData getImageData() { |
98 InputStream in_ = getStream(); | 103 InputStream in_ = getStream(); |
99 ImageData result = null; | 104 ImageData result = null; |
100 if (in_ !is null) { | 105 if (in_ !is null) { |
102 result = new ImageData(in_); | 107 result = new ImageData(in_); |
103 } catch (DWTException e) { | 108 } catch (DWTException e) { |
104 if (e.code !is DWT.ERROR_INVALID_IMAGE /+&& e.code !is DWT.ERROR_UNSUPPORTED_FORMAT+/) { | 109 if (e.code !is DWT.ERROR_INVALID_IMAGE /+&& e.code !is DWT.ERROR_UNSUPPORTED_FORMAT+/) { |
105 Trace.formatln( "FileImageDescriptor getImageData DWTException for name={}", name ); | 110 Trace.formatln( "FileImageDescriptor getImageData DWTException for name={}", name ); |
106 throw e; | 111 throw e; |
107 // fall through otherwise | 112 // fall through otherwise |
108 } | 113 } |
109 } finally { | 114 } finally { |
110 in_.close(); | 115 in_.close(); |
111 } | 116 } |
112 } | 117 } |
113 return result; | 118 return result; |
114 } | 119 } |
115 | 120 |
116 /** | 121 /** |
117 * Returns a stream on the image contents. Returns | 122 * Returns a stream on the image contents. Returns null if a stream could |
118 * null if a stream could not be opened. | 123 * not be opened. |
119 * | 124 * |
120 * @return the buffered stream on the file or <code>null</code> | 125 * @return the buffered stream on the file or <code>null</code> if the |
121 * if the file cannot be found | 126 * file cannot be found |
122 */ | 127 */ |
123 private InputStream getStream() { | 128 private InputStream getStream() { |
124 InputStream is_ = null; | 129 InputStream is_ = null; |
125 | 130 |
126 // if (location !is null) { | 131 // if (location !is null) { |
135 // return null; | 140 // return null; |
136 // } | 141 // } |
137 // } | 142 // } |
138 if (is_ is null) { | 143 if (is_ is null) { |
139 return null; | 144 return null; |
140 } else { | 145 } |
141 return new BufferedInputStream(is_); | 146 return new BufferedInputStream(is); |
142 } | 147 |
143 } | 148 } |
144 | 149 |
145 /* (non-Javadoc) | 150 /* |
146 * Method declared on Object. | 151 * (non-Javadoc) Method declared on Object. |
147 */ | 152 */ |
148 public override hash_t toHash() { | 153 public override hash_t toHash() { |
149 int code = dwt.dwthelper.utils.toHash(cast(char[])importdata/+name+/); | 154 int code = dwt.dwthelper.utils.toHash(cast(char[])importdata/+name+/); |
150 // if (location !is null) { | 155 // if (location !is null) { |
151 // code += location.toHash(); | 156 // code += location.toHash(); |
152 // } | 157 // } |
153 return code; | 158 return code; |
154 } | 159 } |
155 | 160 |
156 /* (non-Javadoc) | 161 /* |
157 * Method declared on Object. | 162 * (non-Javadoc) Method declared on Object. |
158 */ | 163 */ |
159 /** | 164 /** |
160 * The <code>FileImageDescriptor</code> implementation of this <code>Object</code> method | 165 * The <code>FileImageDescriptor</code> implementation of this |
161 * returns a string representation of this object which is suitable only for debugging. | 166 * <code>Object</code> method returns a string representation of this |
167 * object which is suitable only for debugging. | |
162 */ | 168 */ |
163 public override String toString() { | 169 public override String toString() { |
164 // return "FileImageDescriptor(location=" ~ location.toString() ~ ", name=" ~ name ~ ")";//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$ | 170 return Format("FileImageDescriptor(location={}, name={})", location, name );//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$ |
165 return "FileImageDescriptor()";//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$ | 171 } |
172 | |
173 /* | |
174 * (non-Javadoc) | |
175 * | |
176 * @see dwtx.jface.resource.ImageDescriptor#createImage(bool, | |
177 * dwt.graphics.Device) | |
178 */ | |
179 public Image createImage(bool returnMissingImageOnError, Device device) { | |
180 String path = getFilePath(); | |
181 if (path is null) | |
182 return createDefaultImage(returnMissingImageOnError, device); | |
183 try { | |
184 return new Image(device, path); | |
185 } catch (DWTException exception) { | |
186 //if we fail try the default way using a stream | |
187 } | |
188 return super.createImage(returnMissingImageOnError, device); | |
189 } | |
190 | |
191 /** | |
192 * Return default image if returnMissingImageOnError is true. | |
193 * | |
194 * @param device | |
195 * @return Image or <code>null</code> | |
196 */ | |
197 private Image createDefaultImage(bool returnMissingImageOnError, | |
198 Device device) { | |
199 try { | |
200 if (returnMissingImageOnError) | |
201 return new Image(device, DEFAULT_IMAGE_DATA); | |
202 } catch (DWTException nextException) { | |
203 return null; | |
204 } | |
205 return null; | |
206 } | |
207 | |
208 /** | |
209 * Returns the filename for the ImageData. | |
210 * | |
211 * @return {@link String} or <code>null</code> if the file cannot be found | |
212 */ | |
213 private String getFilePath() { | |
214 | |
215 if (location is null) | |
216 return new Path(name).toOSString(); | |
217 | |
218 URL resource = location.getResource(name); | |
219 | |
220 if (resource is null) | |
221 return null; | |
222 // try { | |
223 // if (JFaceActivator.getBundleContext() is null) {// Stand-alone case | |
224 // | |
225 // return new Path(resource.getFile()).toOSString(); | |
226 // } | |
227 // return new Path(FileLocator.toFileURL(resource).getPath()).toOSString(); | |
228 return null; | |
229 // } catch (IOException e) { | |
230 // Policy.logException(e); | |
231 // return null; | |
232 // } | |
166 } | 233 } |
167 } | 234 } |