Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/resource/ImageDataImageDescriptor.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) 2004, 2007 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.ImageDataImageDescriptor; | |
14 | |
15 import org.eclipse.swt.graphics.Device; | |
16 import org.eclipse.swt.graphics.Image; | |
17 import org.eclipse.swt.graphics.ImageData; | |
18 | |
19 import org.eclipse.jface.resource.ImageDescriptor; | |
20 | |
21 import java.lang.all; | |
22 | |
23 /** | |
24 * @since 3.1 | |
25 */ | |
26 class ImageDataImageDescriptor : ImageDescriptor { | |
27 | |
28 private ImageData data; | |
29 | |
30 /** | |
31 * Original image being described, or null if this image is described | |
32 * completely using its ImageData | |
33 */ | |
34 private Image originalImage = null; | |
35 | |
36 /** | |
37 * Creates an image descriptor, given an image and the device it was created on. | |
38 * | |
39 * @param originalImage | |
40 */ | |
41 this(Image originalImage) { | |
42 this(originalImage.getImageData()); | |
43 this.originalImage = originalImage; | |
44 } | |
45 | |
46 /** | |
47 * Creates an image descriptor, given some image data. | |
48 * | |
49 * @param data describing the image | |
50 */ | |
51 | |
52 this(ImageData data) { | |
53 this.data = data; | |
54 } | |
55 | |
56 /* (non-Javadoc) | |
57 * @see org.eclipse.jface.resource.DeviceResourceDescriptor#create(org.eclipse.swt.graphics.Device) | |
58 */ | |
59 public override Object createResource(Device device) { | |
60 | |
61 // If this descriptor is an existing font, then we can return the original font | |
62 // if this is the same device. | |
63 if (originalImage !is null) { | |
64 // If we're allocating on the same device as the original font, return the original. | |
65 if (originalImage.getDevice() is device) { | |
66 return originalImage; | |
67 } | |
68 } | |
69 | |
70 return super.createResource(device); | |
71 } | |
72 | |
73 /* (non-Javadoc) | |
74 * @see org.eclipse.jface.resource.DeviceResourceDescriptor#destroy(java.lang.Object) | |
75 */ | |
76 public override void destroyResource(Object previouslyCreatedObject) { | |
77 if (previouslyCreatedObject is originalImage) { | |
78 return; | |
79 } | |
80 | |
81 super.destroyResource(previouslyCreatedObject); | |
82 } | |
83 | |
84 /* (non-Javadoc) | |
85 * @see org.eclipse.jface.resource.ImageDescriptor#getImageData() | |
86 */ | |
87 public override ImageData getImageData() { | |
88 return data; | |
89 } | |
90 | |
91 /* (non-Javadoc) | |
92 * @see Object#hashCode | |
93 */ | |
94 public override hash_t toHash() { | |
95 if (originalImage !is null) { | |
96 return System.identityHashCode(originalImage); | |
97 } | |
98 return data.toHash(); | |
99 } | |
100 | |
101 /* (non-Javadoc) | |
102 * @see Object#equals | |
103 */ | |
104 public override int opEquals(Object obj) { | |
105 if (!(cast(ImageDataImageDescriptor)obj )) { | |
106 return false; | |
107 } | |
108 | |
109 ImageDataImageDescriptor imgWrap = cast(ImageDataImageDescriptor) obj; | |
110 | |
111 //Intentionally using is instead of equals() as Image.hashCode() changes | |
112 //when the image is disposed and so leaks may occur with equals() | |
113 | |
114 if (originalImage !is null) { | |
115 return imgWrap.originalImage is originalImage; | |
116 } | |
117 | |
118 return (imgWrap.originalImage is null && data.opEquals(imgWrap.data)); | |
119 } | |
120 | |
121 } |