001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jface.resource;
011:
012: import java.io.BufferedInputStream;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.net.URL;
016:
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.SWTException;
019: import org.eclipse.swt.graphics.ImageData;
020:
021: /**
022: * An ImageDescriptor that gets its information from a URL.
023: * This class is not public API. Use ImageDescriptor#createFromURL
024: * to create a descriptor that uses a URL.
025: */
026: class URLImageDescriptor extends ImageDescriptor {
027: private URL url;
028:
029: /**
030: * Creates a new URLImageDescriptor.
031: * @param url The URL to load the image from. Must be non-null.
032: */
033: URLImageDescriptor(URL url) {
034: this .url = url;
035: }
036:
037: /* (non-Javadoc)
038: * Method declared on Object.
039: */
040: public boolean equals(Object o) {
041: if (!(o instanceof URLImageDescriptor)) {
042: return false;
043: }
044: return ((URLImageDescriptor) o).url.equals(this .url);
045: }
046:
047: /* (non-Javadoc)
048: * Method declared on ImageDesciptor.
049: * Returns null if the image data cannot be read.
050: */
051: public ImageData getImageData() {
052: ImageData result = null;
053: InputStream in = getStream();
054: if (in != null) {
055: try {
056: result = new ImageData(in);
057: } catch (SWTException e) {
058: if (e.code != SWT.ERROR_INVALID_IMAGE) {
059: throw e;
060: // fall through otherwise
061: }
062: } finally {
063: try {
064: in.close();
065: } catch (IOException e) {
066: //System.err.println(getClass().getName()+".getImageData(): "+
067: // "Exception while closing InputStream : "+e);
068: }
069: }
070: }
071: return result;
072: }
073:
074: /**
075: * Returns a stream on the image contents. Returns
076: * null if a stream could not be opened.
077: * @return the stream for loading the data
078: */
079: protected InputStream getStream() {
080: try {
081: return new BufferedInputStream(url.openStream());
082: } catch (IOException e) {
083: return null;
084: }
085: }
086:
087: /* (non-Javadoc)
088: * Method declared on Object.
089: */
090: public int hashCode() {
091: return url.hashCode();
092: }
093:
094: /* (non-Javadoc)
095: * Method declared on Object.
096: */
097: /**
098: * The <code>URLImageDescriptor</code> implementation of this <code>Object</code> method
099: * returns a string representation of this object which is suitable only for debugging.
100: */
101: public String toString() {
102: return "URLImageDescriptor(" + url + ")"; //$NON-NLS-1$ //$NON-NLS-2$
103: }
104: }
|