]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/DecorationOverlayIcon.java
Implement ImageDescriptor.getImageData(int zoom)
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / DecorationOverlayIcon.java
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  *******************************************************************************/
11 package org.simantics.utils.ui.gfx;
12
13 import java.util.Arrays;
14
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.*;
19
20 /**
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}.
27  * 
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
31  * Lehtonen
32  * 
33  * @since 3.3
34  * @see IDecoration
35  */
36 public class DecorationOverlayIcon extends CompositeImageDescriptor {
37
38     // the base image
39     private ImageDescriptor base;
40
41     // the overlay images
42     private ImageDescriptor[] overlays;
43
44     // the size
45     private Point size;
46
47     /**
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}).
54      * 
55      * @param baseImage the base image
56      * @param overlaysArray the overlay images
57      * @param sizeValue the size of the resulting image
58      */
59     public DecorationOverlayIcon(ImageDescriptor baseImage,
60             ImageDescriptor[] overlaysArray, Point sizeValue) {
61         init(baseImage, overlaysArray, sizeValue);
62     }
63
64     /**
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}).
71      * 
72      * @param baseImage the base image
73      * @param overlaysArray the overlay images
74      */
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);
79     }
80
81     /**
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})
90      */
91     public DecorationOverlayIcon(ImageDescriptor baseImage, ImageDescriptor overlayImage, int quadrant) {
92         this(baseImage, createArrayFrom(overlayImage, quadrant));
93     }
94
95     private void init(ImageDescriptor baseImage, ImageDescriptor[] overlaysArray, Point sizeValue) {
96         this.base = baseImage;
97         this.overlays = overlaysArray;
98         this.size = sizeValue;
99     }
100
101     /**
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
106      */
107     private static ImageDescriptor[] createArrayFrom(
108             ImageDescriptor overlayImage, int quadrant) {
109         ImageDescriptor[] descs = new ImageDescriptor[] { null, null, null, null, null };
110         descs[quadrant] = overlayImage;
111         return descs;
112     }
113
114     /**
115      * Draw the overlays for the receiver.
116      * @param overlaysArray 
117      */
118     private void drawOverlays(ImageDescriptor[] overlaysArray) {
119
120         for (int i = 0; i < overlays.length; i++) {
121             ImageDescriptor overlay = overlaysArray[i];
122             if (overlay == null) {
123                 continue;
124             }
125             ImageData overlayData = overlay.getImageData();
126             //Use the missing descriptor if it is not there.
127             if (overlayData == null) {
128                 overlayData = ImageDescriptor.getMissingImageDescriptor()
129                 .getImageData();
130             }
131             switch (i) {
132                 case IDecoration.TOP_LEFT:
133                     drawImage(overlayData, 0, 0);
134                     break;
135                 case IDecoration.TOP_RIGHT:
136                     drawImage(overlayData, size.x - overlayData.width, 0);
137                     break;
138                 case IDecoration.BOTTOM_LEFT:
139                     drawImage(overlayData, 0, size.y - overlayData.height);
140                     break;
141                 case IDecoration.BOTTOM_RIGHT:
142                     drawImage(overlayData, size.x - overlayData.width, size.y
143                             - overlayData.height);
144                     break;
145             }
146         }
147     }
148
149     /* (non-Javadoc)
150      * @see java.lang.Object#equals(java.lang.Object)
151      */
152     public boolean equals(Object o) {
153         if (!(o instanceof DecorationOverlayIcon)) {
154             return false;
155         }
156         DecorationOverlayIcon other = (DecorationOverlayIcon) o;
157         return base.equals(other.base)
158         && Arrays.equals(overlays, other.overlays);
159     }
160
161     /* (non-Javadoc)
162      * @see java.lang.Object#hashCode()
163      */
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();
169             }
170         }
171         return code;
172     }
173
174     /* (non-Javadoc)
175      * @see org.eclipse.jface.resource.CompositeImageDescriptor#drawCompositeImage(int, int)
176      */
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);
182             }
183         }
184         if (overlays.length > IDecoration.REPLACE && overlays[IDecoration.REPLACE] != null) {
185             drawImage(overlays[IDecoration.REPLACE].getImageData(), 0, 0);
186         } else {
187             drawImage(base.getImageData(), 0, 0);
188         }
189         drawOverlays(overlays);
190     }
191
192     /* (non-Javadoc)
193      * @see org.eclipse.jface.resource.CompositeImageDescriptor#getSize()
194      */
195     protected Point getSize() {
196         return size;
197     }
198
199     /* (non-Javadoc)
200      * @see org.eclipse.jface.resource.CompositeImageDescriptor#getTransparentPixel()
201      */
202     protected int getTransparentPixel() {
203         return base.getImageData().transparentPixel;
204     }
205
206 }