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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.utils.ui.gfx;
13 import java.util.Arrays;
15 import org.eclipse.jface.resource.CompositeImageDescriptor;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.viewers.IDecoration;
18 import org.eclipse.swt.graphics.*;
21 * A <code>DecorationOverlayIcon</code> is an image descriptor that can be used
22 * to overlay decoration images on to the 4 corner quadrants of a base image.
23 * The four quadrants are {@link IDecoration#TOP_LEFT},
24 * {@link IDecoration#TOP_RIGHT}, {@link IDecoration#BOTTOM_LEFT} and
25 * {@link IDecoration#BOTTOM_RIGHT}. Additionally, the overlay can be used to
26 * provide an underlay corresponding to {@link IDecoration#UNDERLAY}.
28 * Modified for Simantics from
29 * {@link org.eclipse.jface.viewers.DecorationOverlayIcon} to take the original
30 * image as an {@link ImageDescriptor} instead of {@link Image} by Tuukka
36 public class DecorationOverlayIcon extends CompositeImageDescriptor {
39 private ImageDescriptor base;
42 private ImageDescriptor[] overlays;
48 * Create the decoration overlay for the base image using the array of
49 * provided overlays. The indices of the array correspond to the values
50 * of the 5 overlay constants defined on {@link IDecoration}
51 * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
52 * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT}
53 * and{@link IDecoration#UNDERLAY}).
55 * @param baseImage the base image
56 * @param overlaysArray the overlay images
57 * @param sizeValue the size of the resulting image
59 public DecorationOverlayIcon(ImageDescriptor baseImage,
60 ImageDescriptor[] overlaysArray, Point sizeValue) {
61 init(baseImage, overlaysArray, sizeValue);
65 * Create the decoration overlay for the base image using the array of
66 * provided overlays. The indices of the array correspond to the values
67 * of the 5 overlay constants defined on {@link IDecoration}
68 * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
69 * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT}
70 * and {@link IDecoration#UNDERLAY}).
72 * @param baseImage the base image
73 * @param overlaysArray the overlay images
75 public DecorationOverlayIcon(ImageDescriptor baseImage, ImageDescriptor[] overlaysArray) {
76 ImageData data = baseImage.getImageData();
77 Point size = new Point(data.width, data.height);
78 init(baseImage, overlaysArray, size);
82 * Create a decoration overlay icon that will place the given overlay icon in
83 * the given quadrant of the base image.
84 * @param baseImage the base image
85 * @param overlayImage the overlay image
86 * @param quadrant the quadrant (one of {@link IDecoration}
87 * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
88 * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT}
89 * or {@link IDecoration#UNDERLAY})
91 public DecorationOverlayIcon(ImageDescriptor baseImage, ImageDescriptor overlayImage, int quadrant) {
92 this(baseImage, createArrayFrom(overlayImage, quadrant));
95 private void init(ImageDescriptor baseImage, ImageDescriptor[] overlaysArray, Point sizeValue) {
96 this.base = baseImage;
97 this.overlays = overlaysArray;
98 this.size = sizeValue;
102 * Convert the given image and quadrant into the proper input array.
103 * @param overlayImage the overlay image
104 * @param quadrant the quadrant
105 * @return an array with the given image in the proper quadrant
107 private static ImageDescriptor[] createArrayFrom(
108 ImageDescriptor overlayImage, int quadrant) {
109 ImageDescriptor[] descs = new ImageDescriptor[] { null, null, null, null, null };
110 descs[quadrant] = overlayImage;
115 * Draw the overlays for the receiver.
116 * @param overlaysArray
118 private void drawOverlays(ImageDescriptor[] overlaysArray) {
120 for (int i = 0; i < overlays.length; i++) {
121 ImageDescriptor overlay = overlaysArray[i];
122 if (overlay == null) {
125 ImageData overlayData = overlay.getImageData();
126 //Use the missing descriptor if it is not there.
127 if (overlayData == null) {
128 overlayData = ImageDescriptor.getMissingImageDescriptor()
132 case IDecoration.TOP_LEFT:
133 drawImage(overlayData, 0, 0);
135 case IDecoration.TOP_RIGHT:
136 drawImage(overlayData, size.x - overlayData.width, 0);
138 case IDecoration.BOTTOM_LEFT:
139 drawImage(overlayData, 0, size.y - overlayData.height);
141 case IDecoration.BOTTOM_RIGHT:
142 drawImage(overlayData, size.x - overlayData.width, size.y
143 - overlayData.height);
150 * @see java.lang.Object#equals(java.lang.Object)
152 public boolean equals(Object o) {
153 if (!(o instanceof DecorationOverlayIcon)) {
156 DecorationOverlayIcon other = (DecorationOverlayIcon) o;
157 return base.equals(other.base)
158 && Arrays.equals(overlays, other.overlays);
162 * @see java.lang.Object#hashCode()
164 public int hashCode() {
165 int code = base.hashCode();
166 for (int i = 0; i < overlays.length; i++) {
167 if (overlays[i] != null) {
168 code ^= overlays[i].hashCode();
175 * @see org.eclipse.jface.resource.CompositeImageDescriptor#drawCompositeImage(int, int)
177 protected void drawCompositeImage(int width, int height) {
178 if (overlays.length > IDecoration.UNDERLAY) {
179 ImageDescriptor underlay = overlays[IDecoration.UNDERLAY];
180 if (underlay != null) {
181 drawImage(underlay.getImageData(), 0, 0);
184 if (overlays.length > IDecoration.REPLACE && overlays[IDecoration.REPLACE] != null) {
185 drawImage(overlays[IDecoration.REPLACE].getImageData(), 0, 0);
187 drawImage(base.getImageData(), 0, 0);
189 drawOverlays(overlays);
193 * @see org.eclipse.jface.resource.CompositeImageDescriptor#getSize()
195 protected Point getSize() {
200 * @see org.eclipse.jface.resource.CompositeImageDescriptor#getTransparentPixel()
202 protected int getTransparentPixel() {
203 return base.getImageData().transparentPixel;