KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > misc > OverlayComposite


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.misc;
12
13 import org.eclipse.jface.resource.*;
14 import org.eclipse.swt.graphics.*;
15 import java.util.*;
16 /**
17  * Instances represent images that are composed by overlaying one or more
18  * stand-alone images directly on top of each other.
19  */

20 public class OverlayComposite extends CompositeImageDescriptor {
21     private ImageData backgroundImage;
22     private ImageData leftImage;
23     private ImageData rightImage;
24     private ImageData topImage;
25     private ImageData bottomImage;
26
27     private List foregroundImages = new ArrayList();
28 /**
29  * Create an instance of this class.
30  */

31 public OverlayComposite(ImageData background) {
32     backgroundImage = background;
33 }
34 /**
35  * Add the passed image to this descriptor's collection of images to
36  * be composed together to create an image.
37  */

38 public void addForegroundImage(ImageData image) {
39     foregroundImages.add(image);
40 }
41 /**
42  *Superimpose self's images within the given bounds by means of #drawImage
43  *
44  *@see CompositeImage#drawImage(ImageData src,int ox,int oy)
45  */

46 protected void drawCompositeImage(int width, int height) {
47     //draw background
48
drawImage(backgroundImage, getLeftBound(), getTopBound());
49
50     //draw foreground images
51
Iterator e = foregroundImages.iterator();
52     while (e.hasNext())
53         drawImage(((ImageData) e.next()), getLeftBound(), getTopBound());
54
55     //draw extensions
56
if (topImage != null)
57         drawImage(topImage, getLeftBound(), 0);
58     if (bottomImage != null)
59         drawImage(bottomImage, getLeftBound(), height - bottomImage.height);
60     if (leftImage != null)
61         drawImage(leftImage, 0, getTopBound());
62     if (rightImage != null)
63         drawImage(rightImage, width - rightImage.width, getTopBound());
64
65 }
66 /**
67  * @see Object#equals
68  */

69 public boolean equals(Object JavaDoc o) {
70     if (!(o instanceof OverlayComposite)) {
71         return false;
72     }
73     OverlayComposite other = (OverlayComposite) o;
74
75     return equals(backgroundImage, other.backgroundImage)
76         && equals(leftImage, other.leftImage)
77         && equals(rightImage, other.rightImage)
78         && equals(topImage, other.topImage)
79         && equals(bottomImage, other.bottomImage)
80         && equals(foregroundImages, other.foregroundImages);
81 }
82 /**
83  * Private utility for comparing two possibly-null objects.
84  */

85 private boolean equals(Object JavaDoc o1, Object JavaDoc o2) {
86     return o1 == null ? o2 == null : o1.equals(o2);
87 }
88 /**
89  * Answer the left-most coordinate that the main image can draw to
90  *
91  * @return int
92  */

93 protected int getLeftBound() {
94     if (leftImage == null)
95         return 0;
96         
97     return leftImage.width;
98 }
99 /**
100  * Answer self's size, as determined by the size of the initially-
101  * provided base-level image
102  */

103 protected Point getSize() {
104     //start with basic size
105
Point size = new Point(backgroundImage.width,backgroundImage.height);
106
107     //overlays may increase size.
108
if (topImage != null)
109         size.y += topImage.height;
110
111     if (bottomImage != null)
112         size.y += bottomImage.height;
113
114     if (leftImage != null)
115         size.x += leftImage.width;
116
117     if (rightImage != null)
118         size.x += rightImage.width;
119
120     return size;
121 }
122 /**
123  * Answer the top-most coordinate that the main image can draw to
124  *
125  * @return int
126  */

127 protected int getTopBound() {
128     if (topImage == null)
129         return 0;
130         
131     return topImage.height;
132 }
133 /**
134  * @see Object#hashCode
135  */

136 public int hashCode() {
137     return hashCode(backgroundImage)
138         + hashCode(leftImage)
139         + hashCode(rightImage)
140         + hashCode(topImage)
141         + hashCode(bottomImage)
142         + hashCode(foregroundImages);
143 }
144 /**
145  * Private utility for getting the hashCode for an
146  * object that may be null.
147  */

148 private int hashCode(Object JavaDoc o) {
149     return o == null ? 0 : o.hashCode();
150 }
151 /**
152  * Set the image to be drawn below the primary overlay region.
153  */

154 public void setBottomExtension(ImageData value) {
155     bottomImage = value;
156 }
157 /**
158  * Set the image to be drawn to the left of the primary overlay region.
159  */

160 public void setLeftExtension(ImageData value) {
161     leftImage = value;
162 }
163 /**
164  * Set the image to be drawn to the right of the primary overlay region.
165  */

166 public void setRightExtension(ImageData value) {
167     rightImage = value;
168 }
169 /**
170  * Set the image to be drawn above the primary overlay region.
171  */

172 public void setTopExtension(ImageData value) {
173     topImage = value;
174 }
175 }
176
Popular Tags