KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > forms > widgets > ImageHyperlink


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Chriss Gross (schtoo@schtoo.com) - fix for 61670
11  *******************************************************************************/

12 package org.eclipse.ui.forms.widgets;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.graphics.*;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Event;
18
19 /**
20  * This class extends hyperlink widget by adding the capability to render an
21  * image relative to the text. If no text has been set, only image will be
22  * shown. Images for hover and active states can be set in addition to the
23  * normal state image.
24  * <p>
25  * When image is taller than the text, additional style can be provided to
26  * control vertical alignment (supported values are SWT.TOP, SWT.BOTTOM and
27  * SWT.CENTER).
28  * <p>
29  * The class does not need to be sublassed but it is allowed to do so if some
30  * aspect of the image hyperlink needs to be modified.
31  *
32  * @since 3.0
33  */

34 public class ImageHyperlink extends Hyperlink {
35     /**
36      * Amount of pixels between the image and the text (default is 5).
37      */

38     public int textSpacing = 5;
39
40     private Image image;
41
42     private Image hoverImage;
43
44     private Image activeImage;
45
46     private int state;
47
48     private static final int HOVER = 1 << 1;
49
50     private static final int ACTIVE = 1 << 2;
51
52     private int verticalAlignment = SWT.CENTER;
53
54     private int horizontalAlignment = SWT.LEFT;
55
56     /**
57      * Creates the image hyperlink instance.
58      *
59      * @param parent
60      * the control parent
61      * @param style
62      * the control style (SWT.WRAP, BOTTOM, TOP, MIDDLE, LEFT, RIGHT)
63      */

64     public ImageHyperlink(Composite parent, int style) {
65         super(parent, removeAlignment(style));
66         extractAlignment(style);
67     }
68
69     /*
70      * (non-Javadoc)
71      *
72      * @see org.eclipse.ui.forms.widgets.AbstractHyperlink#paintHyperlink(org.eclipse.swt.events.PaintEvent)
73      */

74     protected void paintHyperlink(GC gc) {
75         paintHyperlink(gc, getClientArea());
76     }
77     
78     protected void paintHyperlink(GC gc, Rectangle bounds) {
79         Image image = null;
80         if ((state & ACTIVE) != 0)
81             image = activeImage;
82         else if ((state & HOVER) != 0)
83             image = hoverImage;
84         if (image == null)
85             image = this.image;
86         Rectangle ibounds = image != null ? image.getBounds() : new Rectangle(0, 0, 0, 0);
87         Point maxsize = computeMaxImageSize();
88         int spacing = image!=null?textSpacing:0;
89         int textWidth = bounds.width - maxsize.x - spacing
90                 - marginWidth - marginWidth;
91         int y = bounds.y+marginHeight + maxsize.y / 2 - ibounds.height / 2;
92
93         if (horizontalAlignment == SWT.LEFT) {
94             int x = bounds.x+marginWidth + maxsize.x / 2 - ibounds.width / 2;
95             int textX = bounds.x + marginWidth + maxsize.x + spacing;
96             if (image != null)
97                 gc.drawImage(image, x, y);
98             if (getText() != null)
99                 drawText(gc, bounds, textX, textWidth);
100         } else if (horizontalAlignment == SWT.RIGHT) {
101             int x = bounds.x+marginWidth;
102             if (getText() != null) {
103                 x += drawText(gc, bounds, x, textWidth);
104             }
105             x += maxsize.x / 2 - ibounds.width / 2 + spacing;
106             if (image != null)
107                 gc.drawImage(image, x, y);
108         }
109     }
110
111     private int drawText(GC gc, Rectangle clientArea, int textX, int textWidth) {
112         Point textSize = computeTextSize(textWidth, SWT.DEFAULT);
113         int slotHeight = clientArea.height - marginHeight - marginHeight;
114         int textY;
115         textWidth = textSize.x;
116         int textHeight = textSize.y;
117         if (verticalAlignment == SWT.BOTTOM) {
118             textY = marginHeight + slotHeight - textHeight;
119         } else if (verticalAlignment == SWT.CENTER) {
120             textY = marginHeight + slotHeight / 2 - textHeight / 2;
121         } else {
122             textY = marginHeight;
123         }
124         paintText(gc, new Rectangle(textX, textY, textWidth, textHeight));
125         return textWidth;
126     }
127
128     /**
129      * Computes the control size by reserving space for images in addition to
130      * text.
131      *
132      * @param wHint
133      * width hint
134      * @param hHint
135      * height hint
136      * @param changed
137      * if <code>true</code>, any cached layout data should be
138      * computed anew
139      */

140     public Point computeSize(int wHint, int hHint, boolean changed) {
141         checkWidget();
142         Point isize = computeMaxImageSize();
143         int spacing = isize.x>0?textSpacing:0;
144         Point textSize = null;
145         if (getText() != null) {
146             int innerWHint = wHint;
147             if (wHint != SWT.DEFAULT) {
148                 innerWHint = wHint - 2 * marginWidth - isize.x - spacing;
149             }
150             textSize = super.computeSize(innerWHint, hHint, changed);
151         }
152         int width = isize.x;
153         int height = isize.y;
154         if (textSize != null) {
155             width += spacing;
156             width += textSize.x;
157             height = Math.max(height, textSize.y);
158         }
159         width += 2 * marginWidth;
160         height += 2 * marginHeight;
161         return new Point(width, height);
162     }
163
164     protected void handleEnter(Event e) {
165         state = HOVER;
166         super.handleEnter(e);
167     }
168
169     protected void handleExit(Event e) {
170         state = 0;
171         super.handleExit(e);
172     }
173
174     protected void handleActivate(Event e) {
175         state &= ACTIVE;
176         redraw();
177         super.handleActivate(e);
178         state &= ~ACTIVE;
179         if (!isDisposed())
180             redraw();
181     }
182
183     /**
184      * Returns active image.
185      *
186      * @return active image or <code>null</code> if not set.
187      */

188     public Image getActiveImage() {
189         return activeImage;
190     }
191
192     /**
193      * Sets the image to show when link is activated.
194      *
195      * @param activeImage
196      *
197      */

198     public void setActiveImage(Image activeImage) {
199         this.activeImage = activeImage;
200     }
201
202     /**
203      * Returns the hover image.
204      *
205      * @return hover image or <code>null</code> if not set.
206      */

207     public Image getHoverImage() {
208         return hoverImage;
209     }
210
211     /**
212      * Sets the image to show when link is hover state (on mouse over).
213      *
214      * @param hoverImage
215      */

216     public void setHoverImage(Image hoverImage) {
217         this.hoverImage = hoverImage;
218     }
219
220     /**
221      * Returns the image to show in the normal state.
222      *
223      * @return normal image or <code>null</code> if not set.
224      */

225     public Image getImage() {
226         return image;
227     }
228
229     /**
230      * Sets the image to show when link is in the normal state.
231      *
232      * @param image
233      */

234     public void setImage(Image image) {
235         this.image = image;
236     }
237
238     private Point computeMaxImageSize() {
239         int x = 0;
240         int y = 0;
241         if (image != null) {
242             x = Math.max(image.getBounds().width, x);
243             y = Math.max(image.getBounds().height, y);
244         }
245         if (hoverImage != null) {
246             x = Math.max(hoverImage.getBounds().width, x);
247             y = Math.max(hoverImage.getBounds().height, y);
248         }
249         if (activeImage != null) {
250             x = Math.max(activeImage.getBounds().width, x);
251             y = Math.max(activeImage.getBounds().height, y);
252         }
253         return new Point(x, y);
254     }
255
256     private static int removeAlignment(int style) {
257         int resultStyle = style;
258         if ((style & SWT.CENTER) != 0) {
259             resultStyle &= (~SWT.CENTER);
260         }
261         if ((style & SWT.TOP) != 0) {
262             resultStyle &= (~SWT.TOP);
263         }
264         if ((style & SWT.BOTTOM) != 0) {
265             resultStyle &= (~SWT.BOTTOM);
266         }
267         if ((style & SWT.LEFT) != 0) {
268             resultStyle &= (~SWT.LEFT);
269         }
270         if ((style & SWT.RIGHT) != 0) {
271             resultStyle &= (~SWT.RIGHT);
272         }
273         return resultStyle;
274     }
275
276     private void extractAlignment(int style) {
277         if ((style & SWT.CENTER) != 0) {
278             verticalAlignment = SWT.CENTER;
279         } else if ((style & SWT.TOP) != 0) {
280             verticalAlignment = SWT.TOP;
281         } else if ((style & SWT.BOTTOM) != 0) {
282             verticalAlignment = SWT.BOTTOM;
283         }
284         if ((style & SWT.LEFT) != 0) {
285             horizontalAlignment = SWT.LEFT;
286         } else if ((style & SWT.RIGHT) != 0) {
287             horizontalAlignment = SWT.RIGHT;
288         }
289     }
290 }
291
Popular Tags