improved PHP parser
[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.1 2004-09-02 18:26:28 jsurfer 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 the descriptor of the base image
45          * @param overlays descriptors of the overlay images
46          */
47         public OverlayImageDescriptor(ImageDescriptor base,
48                         ImageDescriptor[][] overlays) {
49                 this(base, overlays, null);
50         }
51
52         /**
53          * Creates the image descriptor.
54          * 
55          * @param base the descriptor of the base image
56          * @param overlays descriptors of the overlay images
57          * @param size the size of the composite image, or <tt>null</tt> to use the
58          *        size of the base image
59          */
60         public OverlayImageDescriptor(ImageDescriptor base,
61                         ImageDescriptor[][] overlays, Point size) {
62                 this.base = base;
63                 this.overlays = overlays;
64                 if (size == null) {
65                         ImageData data = base.getImageData();
66                         size = new Point(data.width, data.height); 
67                 }
68                 this.size = size;
69         }
70
71         // CompositeImageDescriptor Implementation ---------------------------------
72
73         /* 
74          * @see CompositeImageDescriptor#drawCompositeImage(int, int)
75          */
76         protected void drawCompositeImage(int width, int height) {
77                 ImageData bg;
78                 if ((base == null) || ((bg = base.getImageData()) == null)) {
79                         bg = DEFAULT_IMAGE_DATA;
80                 }
81                 drawImage(bg, 0, 0);
82                 if (overlays != null) {
83                         if (overlays.length > 0) {
84                                 drawTopRight(overlays[0]);
85                         }
86                         if (overlays.length > 1) {
87                                 drawBottomRight(overlays[1]);
88                         }
89                         if (overlays.length > 2) {
90                                 drawBottomLeft(overlays[2]);
91                         }
92                         if (overlays.length > 3) {
93                                 drawTopLeft(overlays[3]);
94                         }
95                 }
96         }
97
98         /* 
99          * @see CompositeImageDescriptor#getSize()
100          */
101         protected Point getSize() {
102                 return size;
103         }
104
105         // Private Methods ---------------------------------------------------------
106
107         private void drawBottomLeft(ImageDescriptor[] descriptors) {
108                 if (descriptors == null) {
109                         return;
110                 }
111                 int length = descriptors.length;
112                 int x = 0;
113                 for (int i = 0; i < 3; i++) {
114                         if ((i < length) && (descriptors[i] != null)) {
115                                 ImageData id = descriptors[i].getImageData();
116                                 drawImage(id, x, getSize().y - id.height);
117                                 x += id.width;
118                         }
119                 }
120         }
121
122         private void drawBottomRight(ImageDescriptor[] descriptors) {
123                 if (descriptors == null) {
124                         return;
125                 }
126                 int length = descriptors.length;
127                 int x = getSize().x;
128                 for (int i = 2; i >= 0; i--) {
129                         if (i < length && descriptors[i] != null) {
130                                 ImageData id = descriptors[i].getImageData();
131                                 x -= id.width;
132                                 drawImage(id, x, getSize().y - id.height);
133                         }
134                 }
135         }
136
137         private void drawTopLeft(ImageDescriptor[] descriptors) {
138                 if (descriptors == null) {
139                         return;
140                 }
141                 int length = descriptors.length;
142                 int x = 0;
143                 for (int i = 0; i < 3; i++) {
144                         if (i < length && descriptors[i] != null) {
145                                 ImageData id = descriptors[i].getImageData();
146                                 drawImage(id, x, 0);
147                                 x += id.width;
148                         }
149                 }
150         }
151
152         private void drawTopRight(ImageDescriptor[] descriptors) {
153                 if (descriptors == null) {
154                         return;
155                 }
156                 int length = descriptors.length;
157                 int x = getSize().x;
158                 for (int i = 2; i >= 0; i--) {
159                         if (i < length && descriptors[i] != null) {
160                                 ImageData id = descriptors[i].getImageData();
161                                 x -= id.width;
162                                 drawImage(id, x, 0);
163                         }
164                 }
165         }
166
167 }