1 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 34 public class ImageHyperlink extends Hyperlink { 35 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 64 public ImageHyperlink(Composite parent, int style) { 65 super(parent, removeAlignment(style)); 66 extractAlignment(style); 67 } 68 69 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 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 188 public Image getActiveImage() { 189 return activeImage; 190 } 191 192 198 public void setActiveImage(Image activeImage) { 199 this.activeImage = activeImage; 200 } 201 202 207 public Image getHoverImage() { 208 return hoverImage; 209 } 210 211 216 public void setHoverImage(Image hoverImage) { 217 this.hoverImage = hoverImage; 218 } 219 220 225 public Image getImage() { 226 return image; 227 } 228 229 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 |