Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/viewers/DecorationOverlayIcon.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) 2006, 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.viewers.DecorationOverlayIcon; | |
14 | |
15 import org.eclipse.jface.viewers.IDecoration; | |
16 import org.eclipse.jface.util.Util; | |
17 | |
18 // import tango.util.Arrays; | |
19 | |
20 import org.eclipse.swt.graphics.Image; | |
21 import org.eclipse.swt.graphics.ImageData; | |
22 import org.eclipse.swt.graphics.Point; | |
23 import org.eclipse.jface.resource.CompositeImageDescriptor; | |
24 import org.eclipse.jface.resource.ImageDescriptor; | |
25 | |
26 import java.lang.all; | |
27 import java.util.Arrays; | |
28 | |
29 /** | |
30 * A <code>DecorationOverlayIcon</code> is an image descriptor that can be used | |
31 * to overlay decoration images on to the 4 corner quadrants of a base image. | |
32 * The four quadrants are {@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT}, | |
33 * {@link IDecoration#BOTTOM_LEFT} and {@link IDecoration#BOTTOM_RIGHT}. Additionally, | |
34 * the overlay can be used to provide an underlay corresponding to {@link IDecoration#UNDERLAY}. | |
35 * | |
36 * @since 3.3 | |
37 * @see IDecoration | |
38 */ | |
39 public class DecorationOverlayIcon : CompositeImageDescriptor { | |
40 | |
41 // the base image | |
42 private Image base; | |
43 | |
44 // the overlay images | |
45 private ImageDescriptor[] overlays; | |
46 | |
47 // the size | |
48 private Point size; | |
49 | |
50 /** | |
51 * Create the decoration overlay for the base image using the array of | |
52 * provided overlays. The indices of the array correspond to the values | |
53 * of the 5 overlay constants defined on {@link IDecoration} | |
54 * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT}, | |
55 * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT} | |
56 * and{@link IDecoration#UNDERLAY}). | |
57 * | |
58 * @param baseImage the base image | |
59 * @param overlaysArray the overlay images | |
60 * @param sizeValue the size of the resulting image | |
61 */ | |
62 public this(Image baseImage, | |
63 ImageDescriptor[] overlaysArray, Point sizeValue) { | |
64 this.base = baseImage; | |
65 this.overlays = overlaysArray; | |
66 this.size = sizeValue; | |
67 } | |
68 | |
69 /** | |
70 * Create the decoration overlay for the base image using the array of | |
71 * provided overlays. The indices of the array correspond to the values | |
72 * of the 5 overlay constants defined on {@link IDecoration} | |
73 * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT}, | |
74 * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT} | |
75 * and {@link IDecoration#UNDERLAY}). | |
76 * | |
77 * @param baseImage the base image | |
78 * @param overlaysArray the overlay images | |
79 */ | |
80 public this(Image baseImage, ImageDescriptor[] overlaysArray) { | |
81 this(baseImage, overlaysArray, new Point(baseImage.getBounds().width, baseImage.getBounds().height)); | |
82 } | |
83 | |
84 /** | |
85 * Create a decoration overlay icon that will place the given overlay icon in | |
86 * the given quadrant of the base image. | |
87 * @param baseImage the base image | |
88 * @param overlayImage the overlay image | |
89 * @param quadrant the quadrant (one of {@link IDecoration} | |
90 * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT}, | |
91 * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT} | |
92 * or {@link IDecoration#UNDERLAY}) | |
93 */ | |
94 public this(Image baseImage, ImageDescriptor overlayImage, int quadrant) { | |
95 this(baseImage, createArrayFrom(overlayImage, quadrant)); | |
96 } | |
97 | |
98 /** | |
99 * Convert the given image and quadrant into the proper input array. | |
100 * @param overlayImage the overlay image | |
101 * @param quadrant the quadrant | |
102 * @return an array with the given image in the proper quadrant | |
103 */ | |
104 private static ImageDescriptor[] createArrayFrom( | |
105 ImageDescriptor overlayImage, int quadrant) { | |
106 ImageDescriptor[] descs = [ cast(ImageDescriptor) null, null, null, null, null ]; | |
107 descs[quadrant] = overlayImage; | |
108 return descs; | |
109 } | |
110 | |
111 /** | |
112 * Draw the overlays for the receiver. | |
113 * @param overlaysArray | |
114 */ | |
115 private void drawOverlays(ImageDescriptor[] overlaysArray) { | |
116 | |
117 for (int i = 0; i < overlays.length; i++) { | |
118 ImageDescriptor overlay = overlaysArray[i]; | |
119 if (overlay is null) { | |
120 continue; | |
121 } | |
122 ImageData overlayData = overlay.getImageData(); | |
123 //Use the missing descriptor if it is not there. | |
124 if (overlayData is null) { | |
125 overlayData = ImageDescriptor.getMissingImageDescriptor() | |
126 .getImageData(); | |
127 } | |
128 switch (i) { | |
129 case IDecoration.TOP_LEFT: | |
130 drawImage(overlayData, 0, 0); | |
131 break; | |
132 case IDecoration.TOP_RIGHT: | |
133 drawImage(overlayData, size.x - overlayData.width, 0); | |
134 break; | |
135 case IDecoration.BOTTOM_LEFT: | |
136 drawImage(overlayData, 0, size.y - overlayData.height); | |
137 break; | |
138 case IDecoration.BOTTOM_RIGHT: | |
139 drawImage(overlayData, size.x - overlayData.width, size.y | |
140 - overlayData.height); | |
141 break; | |
142 default: | |
143 } | |
144 } | |
145 } | |
146 | |
147 /* (non-Javadoc) | |
148 * @see java.lang.Object#equals(java.lang.Object) | |
149 */ | |
150 public override int opEquals(Object o) { | |
151 if (!( cast(DecorationOverlayIcon)o )) { | |
152 return false; | |
153 } | |
154 DecorationOverlayIcon other = cast(DecorationOverlayIcon) o; | |
155 return base.opEquals(other.base) | |
156 && Util.opEquals(overlays, other.overlays); | |
157 } | |
158 | |
159 /* (non-Javadoc) | |
160 * @see java.lang.Object#hashCode() | |
161 */ | |
162 public override hash_t toHash() { | |
163 int code = System.identityHashCode(base); | |
164 for (int i = 0; i < overlays.length; i++) { | |
165 if (overlays[i] !is null) { | |
166 code ^= overlays[i].toHash(); | |
167 } | |
168 } | |
169 return code; | |
170 } | |
171 | |
172 /* (non-Javadoc) | |
173 * @see org.eclipse.jface.resource.CompositeImageDescriptor#drawCompositeImage(int, int) | |
174 */ | |
175 protected override void drawCompositeImage(int width, int height) { | |
176 if (overlays.length > IDecoration.UNDERLAY) { | |
177 ImageDescriptor underlay = overlays[IDecoration.UNDERLAY]; | |
178 if (underlay !is null) { | |
179 drawImage(underlay.getImageData(), 0, 0); | |
180 } | |
181 } | |
182 if (overlays.length > IDecoration.REPLACE && overlays[IDecoration.REPLACE] !is null) { | |
183 drawImage(overlays[IDecoration.REPLACE].getImageData(), 0, 0); | |
184 } else { | |
185 drawImage(base.getImageData(), 0, 0); | |
186 } | |
187 drawOverlays(overlays); | |
188 } | |
189 | |
190 /* (non-Javadoc) | |
191 * @see org.eclipse.jface.resource.CompositeImageDescriptor#getSize() | |
192 */ | |
193 protected override Point getSize() { | |
194 return size; | |
195 } | |
196 | |
197 /* (non-Javadoc) | |
198 * @see org.eclipse.jface.resource.CompositeImageDescriptor#getTransparentPixel() | |
199 */ | |
200 protected override int getTransparentPixel() { | |
201 return base.getImageData().transparentPixel; | |
202 } | |
203 | |
204 } |