A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / views / util / OverlayImageDescriptor.java
1 /*
2  * Copyright (c) 2004 Christopher Lenz and others
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial version based on the internal Eclipse class
10  *                        org.eclipse.ui.internal.ide.misc.OverlayIcon
11  * 
12  * $Id: OverlayImageDescriptor.java,v 1.2 2006-10-21 23:13:53 pombredanne Exp $
13  */
14
15 package net.sourceforge.phpeclipse.ui.views.util;
16
17 import org.eclipse.jface.resource.CompositeImageDescriptor;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.swt.graphics.ImageData;
20 import org.eclipse.swt.graphics.Point;
21
22 /**
23  * An overlay image descriptor can add several overlay icons to a base image.
24  */
25 public class OverlayImageDescriptor extends CompositeImageDescriptor {
26
27         // Instance Variables ------------------------------------------------------
28
29         /** Size of the resulting composite image. */
30         private Point size = null;
31
32         /** The base image. */
33         private ImageDescriptor base;
34
35         /** The overlay images. */
36         private ImageDescriptor overlays[][];
37
38         // Constructors ------------------------------------------------------------
39
40         /**
41          * Creates the image descriptor. The size of the resulting image will be the
42          * same as the size of the base image.
43          * 
44          * @param base
45          *            the descriptor of the base image
46          * @param overlays
47          *            descriptors of the overlay images
48          */
49         public OverlayImageDescriptor(ImageDescriptor base,
50                         ImageDescriptor[][] overlays) {
51                 this(base, overlays, null);
52         }
53
54         /**
55          * Creates the image descriptor.
56          * 
57          * @param base
58          *            the descriptor of the base image
59          * @param overlays
60          *            descriptors of the overlay images
61          * @param size
62          *            the size of the composite image, or <tt>null</tt> to use the
63          *            size of the base image
64          */
65         public OverlayImageDescriptor(ImageDescriptor base,
66                         ImageDescriptor[][] overlays, Point size) {
67                 this.base = base;
68                 this.overlays = overlays;
69                 if (size == null) {
70                         ImageData data = base.getImageData();
71                         size = new Point(data.width, data.height);
72                 }
73                 this.size = size;
74         }
75
76         // CompositeImageDescriptor Implementation ---------------------------------
77
78         /*
79          * @see CompositeImageDescriptor#drawCompositeImage(int, int)
80          */
81         protected void drawCompositeImage(int width, int height) {
82                 ImageData bg;
83                 if ((base == null) || ((bg = base.getImageData()) == null)) {
84                         bg = DEFAULT_IMAGE_DATA;
85                 }
86                 drawImage(bg, 0, 0);
87                 if (overlays != null) {
88                         if (overlays.length > 0) {
89                                 drawTopRight(overlays[0]);
90                         }
91                         if (overlays.length > 1) {
92                                 drawBottomRight(overlays[1]);
93                         }
94                         if (overlays.length > 2) {
95                                 drawBottomLeft(overlays[2]);
96                         }
97                         if (overlays.length > 3) {
98                                 drawTopLeft(overlays[3]);
99                         }
100                 }
101         }
102
103         /*
104          * @see CompositeImageDescriptor#getSize()
105          */
106         protected Point getSize() {
107                 return size;
108         }
109
110         // Private Methods ---------------------------------------------------------
111
112         private void drawBottomLeft(ImageDescriptor[] descriptors) {
113                 if (descriptors == null) {
114                         return;
115                 }
116                 int length = descriptors.length;
117                 int x = 0;
118                 for (int i = 0; i < 3; i++) {
119                         if ((i < length) && (descriptors[i] != null)) {
120                                 ImageData id = descriptors[i].getImageData();
121                                 drawImage(id, x, getSize().y - id.height);
122                                 x += id.width;
123                         }
124                 }
125         }
126
127         private void drawBottomRight(ImageDescriptor[] descriptors) {
128                 if (descriptors == null) {
129                         return;
130                 }
131                 int length = descriptors.length;
132                 int x = getSize().x;
133                 for (int i = 2; i >= 0; i--) {
134                         if (i < length && descriptors[i] != null) {
135                                 ImageData id = descriptors[i].getImageData();
136                                 x -= id.width;
137                                 drawImage(id, x, getSize().y - id.height);
138                         }
139                 }
140         }
141
142         private void drawTopLeft(ImageDescriptor[] descriptors) {
143                 if (descriptors == null) {
144                         return;
145                 }
146                 int length = descriptors.length;
147                 int x = 0;
148                 for (int i = 0; i < 3; i++) {
149                         if (i < length && descriptors[i] != null) {
150                                 ImageData id = descriptors[i].getImageData();
151                                 drawImage(id, x, 0);
152                                 x += id.width;
153                         }
154                 }
155         }
156
157         private void drawTopRight(ImageDescriptor[] descriptors) {
158                 if (descriptors == null) {
159                         return;
160                 }
161                 int length = descriptors.length;
162                 int x = getSize().x;
163                 for (int i = 2; i >= 0; i--) {
164                         if (i < length && descriptors[i] != null) {
165                                 ImageData id = descriptors[i].getImageData();
166                                 x -= id.width;
167                                 drawImage(id, x, 0);
168                         }
169                 }
170         }
171
172 }