001: /*******************************************************************************
002: * Copyright (c) 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.viewers;
011:
012: import java.util.Arrays;
013:
014: import org.eclipse.jface.resource.CompositeImageDescriptor;
015: import org.eclipse.jface.resource.ImageDescriptor;
016: import org.eclipse.swt.graphics.*;
017:
018: /**
019: * A <code>DecorationOverlayIcon</code> is an image descriptor that can be used
020: * to overlay decoration images on to the 4 corner quadrants of a base image.
021: * The four quadrants are {@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
022: * {@link IDecoration#BOTTOM_LEFT} and {@link IDecoration#BOTTOM_RIGHT}. Additionally,
023: * the overlay can be used to provide an underlay corresponding to {@link IDecoration#UNDERLAY}.
024: *
025: * @since 3.3
026: * @see IDecoration
027: */
028: public class DecorationOverlayIcon extends CompositeImageDescriptor {
029:
030: // the base image
031: private Image base;
032:
033: // the overlay images
034: private ImageDescriptor[] overlays;
035:
036: // the size
037: private Point size;
038:
039: /**
040: * Create the decoration overlay for the base image using the array of
041: * provided overlays. The indices of the array correspond to the values
042: * of the 5 overlay constants defined on {@link IDecoration}
043: * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
044: * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT}
045: * and{@link IDecoration#UNDERLAY}).
046: *
047: * @param baseImage the base image
048: * @param overlaysArray the overlay images
049: * @param sizeValue the size of the resulting image
050: */
051: public DecorationOverlayIcon(Image baseImage,
052: ImageDescriptor[] overlaysArray, Point sizeValue) {
053: this .base = baseImage;
054: this .overlays = overlaysArray;
055: this .size = sizeValue;
056: }
057:
058: /**
059: * Create the decoration overlay for the base image using the array of
060: * provided overlays. The indices of the array correspond to the values
061: * of the 5 overlay constants defined on {@link IDecoration}
062: * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
063: * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT}
064: * and {@link IDecoration#UNDERLAY}).
065: *
066: * @param baseImage the base image
067: * @param overlaysArray the overlay images
068: */
069: public DecorationOverlayIcon(Image baseImage,
070: ImageDescriptor[] overlaysArray) {
071: this (baseImage, overlaysArray, new Point(
072: baseImage.getBounds().width,
073: baseImage.getBounds().height));
074: }
075:
076: /**
077: * Create a decoration overlay icon that will place the given overlay icon in
078: * the given quadrant of the base image.
079: * @param baseImage the base image
080: * @param overlayImage the overlay image
081: * @param quadrant the quadrant (one of {@link IDecoration}
082: * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
083: * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT}
084: * or {@link IDecoration#UNDERLAY})
085: */
086: public DecorationOverlayIcon(Image baseImage,
087: ImageDescriptor overlayImage, int quadrant) {
088: this (baseImage, createArrayFrom(overlayImage, quadrant));
089: }
090:
091: /**
092: * Convert the given image and quadrant into the proper input array.
093: * @param overlayImage the overlay image
094: * @param quadrant the quadrant
095: * @return an array with the given image in the proper quadrant
096: */
097: private static ImageDescriptor[] createArrayFrom(
098: ImageDescriptor overlayImage, int quadrant) {
099: ImageDescriptor[] descs = new ImageDescriptor[] { null, null,
100: null, null, null };
101: descs[quadrant] = overlayImage;
102: return descs;
103: }
104:
105: /**
106: * Draw the overlays for the receiver.
107: * @param overlaysArray
108: */
109: private void drawOverlays(ImageDescriptor[] overlaysArray) {
110:
111: for (int i = 0; i < overlays.length; i++) {
112: ImageDescriptor overlay = overlaysArray[i];
113: if (overlay == null) {
114: continue;
115: }
116: ImageData overlayData = overlay.getImageData();
117: //Use the missing descriptor if it is not there.
118: if (overlayData == null) {
119: overlayData = ImageDescriptor
120: .getMissingImageDescriptor().getImageData();
121: }
122: switch (i) {
123: case IDecoration.TOP_LEFT:
124: drawImage(overlayData, 0, 0);
125: break;
126: case IDecoration.TOP_RIGHT:
127: drawImage(overlayData, size.x - overlayData.width, 0);
128: break;
129: case IDecoration.BOTTOM_LEFT:
130: drawImage(overlayData, 0, size.y - overlayData.height);
131: break;
132: case IDecoration.BOTTOM_RIGHT:
133: drawImage(overlayData, size.x - overlayData.width,
134: size.y - overlayData.height);
135: break;
136: }
137: }
138: }
139:
140: /* (non-Javadoc)
141: * @see java.lang.Object#equals(java.lang.Object)
142: */
143: public boolean equals(Object o) {
144: if (!(o instanceof DecorationOverlayIcon)) {
145: return false;
146: }
147: DecorationOverlayIcon other = (DecorationOverlayIcon) o;
148: return base.equals(other.base)
149: && Arrays.equals(overlays, other.overlays);
150: }
151:
152: /* (non-Javadoc)
153: * @see java.lang.Object#hashCode()
154: */
155: public int hashCode() {
156: int code = System.identityHashCode(base);
157: for (int i = 0; i < overlays.length; i++) {
158: if (overlays[i] != null) {
159: code ^= overlays[i].hashCode();
160: }
161: }
162: return code;
163: }
164:
165: /* (non-Javadoc)
166: * @see org.eclipse.jface.resource.CompositeImageDescriptor#drawCompositeImage(int, int)
167: */
168: protected void drawCompositeImage(int width, int height) {
169: if (overlays.length > IDecoration.UNDERLAY) {
170: ImageDescriptor underlay = overlays[IDecoration.UNDERLAY];
171: if (underlay != null) {
172: drawImage(underlay.getImageData(), 0, 0);
173: }
174: }
175: drawImage(base.getImageData(), 0, 0);
176: drawOverlays(overlays);
177: }
178:
179: /* (non-Javadoc)
180: * @see org.eclipse.jface.resource.CompositeImageDescriptor#getSize()
181: */
182: protected Point getSize() {
183: return size;
184: }
185:
186: /* (non-Javadoc)
187: * @see org.eclipse.jface.resource.CompositeImageDescriptor#getTransparentPixel()
188: */
189: protected int getTransparentPixel() {
190: return base.getImageData().transparentPixel;
191: }
192:
193: }
|