KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > DecorationOverlayIcon


1 /*******************************************************************************
2  * Copyright (c) 2006 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  *******************************************************************************/

11 package org.eclipse.jface.viewers;
12
13 import java.util.Arrays JavaDoc;
14
15 import org.eclipse.jface.resource.CompositeImageDescriptor;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.swt.graphics.*;
18
19 /**
20  * A <code>DecorationOverlayIcon</code> is an image descriptor that can be used
21  * to overlay decoration images on to the 4 corner quadrants of a base image.
22  * The four quadrants are {@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
23  * {@link IDecoration#BOTTOM_LEFT} and {@link IDecoration#BOTTOM_RIGHT}. Additionally,
24  * the overlay can be used to provide an underlay corresponding to {@link IDecoration#UNDERLAY}.
25  *
26  * @since 3.3
27  * @see IDecoration
28  */

29 public class DecorationOverlayIcon extends CompositeImageDescriptor {
30     
31     // the base image
32
private Image base;
33
34     // the overlay images
35
private ImageDescriptor[] overlays;
36
37     // the size
38
private Point size;
39
40     /**
41      * Create the decoration overlay for the base image using the array of
42      * provided overlays. The indices of the array correspond to the values
43      * of the 5 overlay constants defined on {@link IDecoration}
44      * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
45      * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT}
46      * and{@link IDecoration#UNDERLAY}).
47      *
48      * @param baseImage the base image
49      * @param overlaysArray the overlay images
50      * @param sizeValue the size of the resulting image
51      */

52     public DecorationOverlayIcon(Image baseImage,
53             ImageDescriptor[] overlaysArray, Point sizeValue) {
54         this.base = baseImage;
55         this.overlays = overlaysArray;
56         this.size = sizeValue;
57     }
58     
59     /**
60      * Create the decoration overlay for the base image using the array of
61      * provided overlays. The indices of the array correspond to the values
62      * of the 5 overlay constants defined on {@link IDecoration}
63      * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
64      * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT}
65      * and {@link IDecoration#UNDERLAY}).
66      *
67      * @param baseImage the base image
68      * @param overlaysArray the overlay images
69      */

70     public DecorationOverlayIcon(Image baseImage, ImageDescriptor[] overlaysArray) {
71         this(baseImage, overlaysArray, new Point(baseImage.getBounds().width, baseImage.getBounds().height));
72     }
73
74     /**
75      * Create a decoration overlay icon that will place the given overlay icon in
76      * the given quadrant of the base image.
77      * @param baseImage the base image
78      * @param overlayImage the overlay image
79      * @param quadrant the quadrant (one of {@link IDecoration}
80      * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
81      * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT}
82      * or {@link IDecoration#UNDERLAY})
83      */

84     public DecorationOverlayIcon(Image baseImage, ImageDescriptor overlayImage, int quadrant) {
85         this(baseImage, createArrayFrom(overlayImage, quadrant));
86     }
87
88     /**
89      * Convert the given image and quadrant into the proper input array.
90      * @param overlayImage the overlay image
91      * @param quadrant the quadrant
92      * @return an array with the given image in the proper quadrant
93      */

94     private static ImageDescriptor[] createArrayFrom(
95             ImageDescriptor overlayImage, int quadrant) {
96         ImageDescriptor[] descs = new ImageDescriptor[] { null, null, null, null, null };
97         descs[quadrant] = overlayImage;
98         return descs;
99     }
100
101     /**
102      * Draw the overlays for the receiver.
103      * @param overlaysArray
104      */

105     private void drawOverlays(ImageDescriptor[] overlaysArray) {
106
107         for (int i = 0; i < overlays.length; i++) {
108             ImageDescriptor overlay = overlaysArray[i];
109             if (overlay == null) {
110                 continue;
111             }
112             ImageData overlayData = overlay.getImageData();
113             //Use the missing descriptor if it is not there.
114
if (overlayData == null) {
115                 overlayData = ImageDescriptor.getMissingImageDescriptor()
116                         .getImageData();
117             }
118             switch (i) {
119             case IDecoration.TOP_LEFT:
120                 drawImage(overlayData, 0, 0);
121                 break;
122             case IDecoration.TOP_RIGHT:
123                 drawImage(overlayData, size.x - overlayData.width, 0);
124                 break;
125             case IDecoration.BOTTOM_LEFT:
126                 drawImage(overlayData, 0, size.y - overlayData.height);
127                 break;
128             case IDecoration.BOTTOM_RIGHT:
129                 drawImage(overlayData, size.x - overlayData.width, size.y
130                         - overlayData.height);
131                 break;
132             }
133         }
134     }
135
136     /* (non-Javadoc)
137      * @see java.lang.Object#equals(java.lang.Object)
138      */

139     public boolean equals(Object JavaDoc o) {
140         if (!(o instanceof DecorationOverlayIcon)) {
141             return false;
142         }
143         DecorationOverlayIcon other = (DecorationOverlayIcon) o;
144         return base.equals(other.base)
145                 && Arrays.equals(overlays, other.overlays);
146     }
147
148     /* (non-Javadoc)
149      * @see java.lang.Object#hashCode()
150      */

151     public int hashCode() {
152         int code = base.hashCode();
153         for (int i = 0; i < overlays.length; i++) {
154             if (overlays[i] != null) {
155                 code ^= overlays[i].hashCode();
156             }
157         }
158         return code;
159     }
160
161     /* (non-Javadoc)
162      * @see org.eclipse.jface.resource.CompositeImageDescriptor#drawCompositeImage(int, int)
163      */

164     protected void drawCompositeImage(int width, int height) {
165         if (overlays.length > IDecoration.UNDERLAY) {
166             ImageDescriptor underlay = overlays[IDecoration.UNDERLAY];
167             if (underlay != null) {
168                 drawImage(underlay.getImageData(), 0, 0);
169             }
170         }
171         drawImage(base.getImageData(), 0, 0);
172         drawOverlays(overlays);
173     }
174
175     /* (non-Javadoc)
176      * @see org.eclipse.jface.resource.CompositeImageDescriptor#getSize()
177      */

178     protected Point getSize() {
179         return size;
180     }
181     
182     /* (non-Javadoc)
183      * @see org.eclipse.jface.resource.CompositeImageDescriptor#getTransparentPixel()
184      */

185     protected int getTransparentPixel() {
186         return base.getImageData().transparentPixel;
187     }
188
189 }
190
Popular Tags