KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > misc > 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.ui.internal.ide.misc;
12
13 import org.eclipse.jface.resource.CompositeImageDescriptor;
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.eclipse.swt.graphics.ImageData;
16 import org.eclipse.swt.graphics.Point;
17
18 /**
19  * An OverlayIcon consists of a main icon and several adornments.
20  */

21 public class OverlayIcon extends CompositeImageDescriptor {
22
23     static final int DEFAULT_WIDTH = 22;
24
25     static final int DEFAULT_HEIGHT = 16;
26
27     private Point fSize = null;
28
29     private ImageDescriptor fBase;
30
31     private ImageDescriptor fOverlays[][];
32
33     public OverlayIcon(ImageDescriptor base, ImageDescriptor[][] overlays,
34             Point size) {
35         fBase = base;
36         fOverlays = overlays;
37         fSize = size;
38     }
39
40     protected void drawBottomLeft(ImageDescriptor[] overlays) {
41         if (overlays == null) {
42             return;
43         }
44         int length = overlays.length;
45         int x = 0;
46         for (int i = 0; i < 3; i++) {
47             if (i < length && overlays[i] != null) {
48                 ImageData id = overlays[i].getImageData();
49                 drawImage(id, x, getSize().y - id.height);
50                 x += id.width;
51             }
52         }
53     }
54
55     protected void drawBottomRight(ImageDescriptor[] overlays) {
56         if (overlays == null) {
57             return;
58         }
59         int length = overlays.length;
60         int x = getSize().x;
61         for (int i = 2; i >= 0; i--) {
62             if (i < length && overlays[i] != null) {
63                 ImageData id = overlays[i].getImageData();
64                 x -= id.width;
65                 drawImage(id, x, getSize().y - id.height);
66             }
67         }
68     }
69
70     /**
71      * @see CompositeImageDescriptor#drawCompositeImage(int, int)
72      */

73     protected void drawCompositeImage(int width, int height) {
74         ImageData bg;
75         if (fBase == null || (bg = fBase.getImageData()) == null) {
76             bg = DEFAULT_IMAGE_DATA;
77         }
78         drawImage(bg, 0, 0);
79
80         if (fOverlays != null) {
81             if (fOverlays.length > 0) {
82                 drawTopRight(fOverlays[0]);
83             }
84
85             if (fOverlays.length > 1) {
86                 drawBottomRight(fOverlays[1]);
87             }
88
89             if (fOverlays.length > 2) {
90                 drawBottomLeft(fOverlays[2]);
91             }
92
93             if (fOverlays.length > 3) {
94                 drawTopLeft(fOverlays[3]);
95             }
96         }
97     }
98
99     protected void drawTopLeft(ImageDescriptor[] overlays) {
100         if (overlays == null) {
101             return;
102         }
103         int length = overlays.length;
104         int x = 0;
105         for (int i = 0; i < 3; i++) {
106             if (i < length && overlays[i] != null) {
107                 ImageData id = overlays[i].getImageData();
108                 drawImage(id, x, 0);
109                 x += id.width;
110             }
111         }
112     }
113
114     protected void drawTopRight(ImageDescriptor[] overlays) {
115         if (overlays == null) {
116             return;
117         }
118         int length = overlays.length;
119         int x = getSize().x;
120         for (int i = 2; i >= 0; i--) {
121             if (i < length && overlays[i] != null) {
122                 ImageData id = overlays[i].getImageData();
123                 x -= id.width;
124                 drawImage(id, x, 0);
125             }
126         }
127     }
128
129     /**
130      * @see CompositeImageDescriptor#getSize()
131      */

132     protected Point getSize() {
133         return fSize;
134     }
135 }
136
Popular Tags