001    /*******************************************************************************
002     * Copyright (c) 2000, 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     *******************************************************************************/
011    package org.eclipse.team.internal.ui;
012    
013    import java.util.Arrays;
014    
015    import org.eclipse.jface.resource.CompositeImageDescriptor;
016    import org.eclipse.jface.resource.ImageDescriptor;
017    import org.eclipse.swt.graphics.*;
018    
019    /**
020     * An OverlayIcon consists of a main icon and several adornments.
021     */
022    public class OverlayIcon extends CompositeImageDescriptor {
023            // the base image
024            private Image base;
025            // the base as a descriptor
026            private ImageDescriptor descriptorBase;
027            // the overlay images
028            private ImageDescriptor[] overlays;
029            // the size
030            private Point size;
031            // the locations
032            private int[] locations;
033            
034            public static final int TOP_LEFT = 0;
035            public static final int TOP_RIGHT = 1;
036            public static final int BOTTOM_LEFT = 2;
037            public static final int BOTTOM_RIGHT = 3;
038            
039            public static final int DEFAULT_WIDTH= 22;
040            public static final int DEFAULT_HEIGHT= 16;
041            
042            /**
043             * OverlayIcon constructor.
044             * 
045             * @param base the base image
046             * @param overlays the overlay images
047             * @param locations the location of each image
048             * @param size the size
049             */
050            public OverlayIcon(Image base, ImageDescriptor[] overlays, int[] locations, Point size) {
051                    this.base = base;
052                    this.descriptorBase = null;
053                    this.overlays = overlays;
054                    this.locations = locations;
055                    this.size = size;
056            }
057            
058            protected void drawOverlays(ImageDescriptor[] overlays, int[] locations) {
059                            Point size = getSize();
060                            for (int i = 0; i < overlays.length; i++) {
061                                    ImageDescriptor overlay = overlays[i];
062                                    ImageData overlayData = overlay.getImageData();
063                                    switch (locations[i]) {
064                                            case TOP_LEFT:
065                                                    drawImage(overlayData, 0, 0);                   
066                                                    break;
067                                            case TOP_RIGHT:
068                                                    drawImage(overlayData, size.x - overlayData.width, 0);                  
069                                                    break;
070                                            case BOTTOM_LEFT:
071                                                    drawImage(overlayData, 0, size.y - overlayData.height);                 
072                                                    break;
073                                            case BOTTOM_RIGHT:
074                                                    drawImage(overlayData, size.x - overlayData.width, size.y - overlayData.height);                        
075                                                    break;
076                                    }
077                            }
078                    }
079    
080            public boolean equals(Object o) {
081                    if (! (o instanceof OverlayIcon)) return false;
082                    OverlayIcon other = (OverlayIcon) o;
083                    return base.equals(other.base) && Arrays.equals(overlays, other.overlays);
084            }
085    
086            public int hashCode() {
087                    int code = base.hashCode();
088                    for (int i = 0; i < overlays.length; i++) {
089                            code ^= overlays[i].hashCode();
090                    }
091                    return code;
092            }
093    
094    
095            protected void drawCompositeImage(int width, int height) {
096                    if(descriptorBase != null) {
097                            ImageData bg;
098                            if (descriptorBase == null || (bg= descriptorBase.getImageData()) == null)
099                                    bg= DEFAULT_IMAGE_DATA;
100                            drawImage(bg, 0, 0);
101                    } else {
102                            drawImage(base.getImageData(), 0, 0);
103                    }
104                    drawOverlays(overlays, locations);
105            }
106    
107            protected Point getSize() {
108                    return size;
109            }
110    }