KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > OverlayIcon


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.team.internal.ui;
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  * An OverlayIcon consists of a main icon and several adornments.
21  */

22 public class OverlayIcon extends CompositeImageDescriptor {
23     // the base image
24
private Image base;
25     // the base as a descriptor
26
private ImageDescriptor descriptorBase;
27     // the overlay images
28
private ImageDescriptor[] overlays;
29     // the size
30
private Point size;
31     // the locations
32
private int[] locations;
33     
34     public static final int TOP_LEFT = 0;
35     public static final int TOP_RIGHT = 1;
36     public static final int BOTTOM_LEFT = 2;
37     public static final int BOTTOM_RIGHT = 3;
38     
39     public static final int DEFAULT_WIDTH= 22;
40     public static final int DEFAULT_HEIGHT= 16;
41     
42     /**
43      * OverlayIcon constructor.
44      *
45      * @param base the base image
46      * @param overlays the overlay images
47      * @param locations the location of each image
48      * @param size the size
49      */

50     public OverlayIcon(Image base, ImageDescriptor[] overlays, int[] locations, Point size) {
51         this.base = base;
52         this.descriptorBase = null;
53         this.overlays = overlays;
54         this.locations = locations;
55         this.size = size;
56     }
57     
58     protected void drawOverlays(ImageDescriptor[] overlays, int[] locations) {
59             Point size = getSize();
60             for (int i = 0; i < overlays.length; i++) {
61                 ImageDescriptor overlay = overlays[i];
62                 ImageData overlayData = overlay.getImageData();
63                 switch (locations[i]) {
64                     case TOP_LEFT:
65                         drawImage(overlayData, 0, 0);
66                         break;
67                     case TOP_RIGHT:
68                         drawImage(overlayData, size.x - overlayData.width, 0);
69                         break;
70                     case BOTTOM_LEFT:
71                         drawImage(overlayData, 0, size.y - overlayData.height);
72                         break;
73                     case BOTTOM_RIGHT:
74                         drawImage(overlayData, size.x - overlayData.width, size.y - overlayData.height);
75                         break;
76                 }
77             }
78         }
79
80     public boolean equals(Object JavaDoc o) {
81         if (! (o instanceof OverlayIcon)) return false;
82         OverlayIcon other = (OverlayIcon) o;
83         return base.equals(other.base) && Arrays.equals(overlays, other.overlays);
84     }
85
86     public int hashCode() {
87         int code = base.hashCode();
88         for (int i = 0; i < overlays.length; i++) {
89             code ^= overlays[i].hashCode();
90         }
91         return code;
92     }
93
94
95     protected void drawCompositeImage(int width, int height) {
96         if(descriptorBase != null) {
97             ImageData bg;
98             if (descriptorBase == null || (bg= descriptorBase.getImageData()) == null)
99                 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 }
111
Popular Tags