001: /*******************************************************************************
002: * Copyright (c) 2004, 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 org.eclipse.swt.graphics.Device;
013: import org.eclipse.swt.graphics.Image;
014: import org.eclipse.swt.graphics.ImageData;
015:
016: /**
017: * @since 3.1
018: */
019: class ImageDataImageDescriptor extends ImageDescriptor {
020:
021: private ImageData data;
022:
023: /**
024: * Original image being described, or null if this image is described
025: * completely using its ImageData
026: */
027: private Image originalImage = null;
028:
029: /**
030: * Creates an image descriptor, given an image and the device it was created on.
031: *
032: * @param originalImage
033: */
034: ImageDataImageDescriptor(Image originalImage) {
035: this (originalImage.getImageData());
036: this .originalImage = originalImage;
037: }
038:
039: /**
040: * Creates an image descriptor, given some image data.
041: *
042: * @param data describing the image
043: */
044:
045: ImageDataImageDescriptor(ImageData data) {
046: this .data = data;
047: }
048:
049: /* (non-Javadoc)
050: * @see org.eclipse.jface.resource.DeviceResourceDescriptor#create(org.eclipse.swt.graphics.Device)
051: */
052: public Object createResource(Device device)
053: throws DeviceResourceException {
054:
055: // If this descriptor is an existing font, then we can return the original font
056: // if this is the same device.
057: if (originalImage != null) {
058: // If we're allocating on the same device as the original font, return the original.
059: if (originalImage.getDevice() == device) {
060: return originalImage;
061: }
062: }
063:
064: return super .createResource(device);
065: }
066:
067: /* (non-Javadoc)
068: * @see org.eclipse.jface.resource.DeviceResourceDescriptor#destroy(java.lang.Object)
069: */
070: public void destroyResource(Object previouslyCreatedObject) {
071: if (previouslyCreatedObject == originalImage) {
072: return;
073: }
074:
075: super .destroyResource(previouslyCreatedObject);
076: }
077:
078: /* (non-Javadoc)
079: * @see org.eclipse.jface.resource.ImageDescriptor#getImageData()
080: */
081: public ImageData getImageData() {
082: return data;
083: }
084:
085: /* (non-Javadoc)
086: * @see Object#hashCode
087: */
088: public int hashCode() {
089: return data.hashCode();
090: }
091:
092: /* (non-Javadoc)
093: * @see Object#equals
094: */
095: public boolean equals(Object obj) {
096: if (!(obj instanceof ImageDataImageDescriptor)) {
097: return false;
098: }
099:
100: ImageDataImageDescriptor imgWrap = (ImageDataImageDescriptor) obj;
101:
102: if (originalImage != null) {
103: return imgWrap.originalImage == originalImage;
104: }
105:
106: return (imgWrap.originalImage == null && data
107: .equals(imgWrap.data));
108: }
109:
110: }
|