comparison org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 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 org.eclipse.jface.resource.URLImageDescriptor;
14
15 import org.eclipse.jface.resource.ImageDescriptor;
16
17 // import java.io.BufferedInputStream;
18 // import java.io.IOException;
19 // import java.io.InputStream;
20 import tango.net.Uri;
21
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.SWTException;
24 import org.eclipse.swt.graphics.Device;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.graphics.ImageData;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.jface.util.Policy;
30
31 import java.lang.all;
32 import java.io.BufferedInputStream;
33 import java.io.InputStream;
34
35 /**
36 * An ImageDescriptor that gets its information from a URL. This class is not
37 * public API. Use ImageDescriptor#createFromURL to create a descriptor that
38 * uses a URL.
39 */
40 class URLImageDescriptor : ImageDescriptor {
41 /**
42 * Constant for the file protocol for optimized loading
43 */
44 private static final String FILE_PROTOCOL = "file"; //$NON-NLS-1$
45 private Uri url;
46
47 /**
48 * Creates a new URLImageDescriptor.
49 *
50 * @param url
51 * The URL to load the image from. Must be non-null.
52 */
53 this(Uri url) {
54 this.url = url;
55 }
56
57 /*
58 * (non-Javadoc) Method declared on Object.
59 */
60 public override int opEquals(Object o) {
61 if (!(cast(URLImageDescriptor)o )) {
62 return false;
63 }
64 return (cast(URLImageDescriptor) o).url.opEquals(this.url) !is 0;
65 }
66
67 /*
68 * (non-Javadoc) Method declared on ImageDesciptor. Returns null if the
69 * image data cannot be read.
70 */
71 public override ImageData getImageData() {
72 ImageData result = null;
73 InputStream in_ = getStream();
74 if (in_ !is null) {
75 scope(exit)
76 in_.close();
77 try {
78 result = new ImageData(in_);
79 } catch (SWTException e) {
80 if (e.code !is SWT.ERROR_INVALID_IMAGE) {
81 throw e;
82 // fall through otherwise
83 }
84 }
85 }
86 return result;
87 }
88
89 /**
90 * Returns a stream on the image contents. Returns null if a stream could
91 * not be opened.
92 *
93 * @return the stream for loading the data
94 */
95 protected InputStream getStream() {
96 implMissing( __FILE__, __LINE__ );
97 return null;
98 //FIXME
99 /+
100 try {
101 return new BufferedInputStream(url.openStream());
102 } catch (IOException e) {
103 return null;
104 }
105 +/
106 }
107
108 /*
109 * (non-Javadoc) Method declared on Object.
110 */
111 public override hash_t toHash() {
112 return url.toHash();
113 }
114
115 /*
116 * (non-Javadoc) Method declared on Object.
117 */
118 /**
119 * The <code>URLImageDescriptor</code> implementation of this
120 * <code>Object</code> method returns a string representation of this
121 * object which is suitable only for debugging.
122 */
123 public override String toString() {
124 return "URLImageDescriptor(" ~ url.toString ~ ")"; //$NON-NLS-1$ //$NON-NLS-2$
125 }
126
127 /**
128 * Returns the filename for the ImageData.
129 *
130 * @return {@link String} or <code>null</code> if the file cannot be found
131 */
132 private String getFilePath() {
133 // try {
134 // if (JFaceActivator.getBundleContext() is null) {
135 // if (FILE_PROTOCOL.equalsIgnoreCase(url.getProtocol()))
136 // return new Path(url.getFile()).toOSString();
137 // return null;
138 // }
139 //
140 // URL locatedURL = FileLocator.toFileURL(url);
141 // if (FILE_PROTOCOL.equalsIgnoreCase(locatedURL.getProtocol()))
142 // return new Path(locatedURL.getPath()).toOSString();
143 // return null;
144 //
145 // } catch (IOException e) {
146 // Policy.logException(e);
147 // return null;
148 // }
149 return null;
150 }
151
152 /*
153 * (non-Javadoc)
154 *
155 * @see org.eclipse.jface.resource.ImageDescriptor#createImage(bool,
156 * org.eclipse.swt.graphics.Device)
157 */
158 public override Image createImage(bool returnMissingImageOnError, Device device) {
159
160 // Try to see if we can optimize using SWTs file based image support.
161 String path = getFilePath();
162 if (path is null)
163 return super.createImage(returnMissingImageOnError, device);
164
165 try {
166 return new Image(device, path);
167 } catch (SWTException exception) {
168 // If we fail fall back to the slower input stream method.
169 }
170 return super.createImage(returnMissingImageOnError, device);
171 }
172
173 }