1 11 package org.eclipse.ui.forms.widgets; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.accessibility.*; 15 import org.eclipse.swt.graphics.*; 16 import org.eclipse.swt.widgets.Composite; 17 import org.eclipse.ui.internal.forms.widgets.*; 18 19 30 public class Hyperlink extends AbstractHyperlink { 31 private String text; 32 private static final String ELLIPSIS = "..."; private boolean underlined; 34 private String appToolTipText; 40 41 49 public Hyperlink(Composite parent, int style) { 50 super(parent, style); 51 initAccessible(); 52 } 53 54 protected void initAccessible() { 55 Accessible accessible = getAccessible(); 56 accessible.addAccessibleListener(new AccessibleAdapter() { 57 public void getName(AccessibleEvent e) { 58 e.result = getText(); 59 if (e.result == null) 60 getHelp(e); 61 } 62 63 public void getHelp(AccessibleEvent e) { 64 e.result = getToolTipText(); 65 } 66 }); 67 accessible.addAccessibleControlListener(new AccessibleControlAdapter() { 68 public void getChildAtPoint(AccessibleControlEvent e) { 69 Point pt = toControl(new Point(e.x, e.y)); 70 e.childID = (getBounds().contains(pt)) ? ACC.CHILDID_SELF 71 : ACC.CHILDID_NONE; 72 } 73 74 public void getLocation(AccessibleControlEvent e) { 75 Rectangle location = getBounds(); 76 Point pt = toDisplay(new Point(location.x, location.y)); 77 e.x = pt.x; 78 e.y = pt.y; 79 e.width = location.width; 80 e.height = location.height; 81 } 82 83 public void getChildCount(AccessibleControlEvent e) { 84 e.detail = 0; 85 } 86 87 public void getRole(AccessibleControlEvent e) { 88 e.detail = ACC.ROLE_LINK; 89 } 90 91 public void getDefaultAction (AccessibleControlEvent e) { 92 e.result = SWT.getMessage ("SWT_Press"); } 94 95 public void getState(AccessibleControlEvent e) { 96 int state = ACC.STATE_NORMAL; 97 if (Hyperlink.this.getSelection()) 98 state = ACC.STATE_SELECTED | ACC.STATE_FOCUSED; 99 e.detail = state; 100 } 101 }); 102 } 103 104 112 public void setUnderlined(boolean underlined) { 113 this.underlined = underlined; 114 redraw(); 115 } 116 117 123 public boolean isUnderlined() { 124 return underlined; 125 } 126 127 130 public Point computeSize(int wHint, int hHint, boolean changed) { 131 checkWidget(); 132 int innerWidth = wHint; 133 if (innerWidth != SWT.DEFAULT) 134 innerWidth -= marginWidth * 2; 135 Point textSize = computeTextSize(innerWidth, hHint); 136 int textWidth = textSize.x + 2 * marginWidth; 137 int textHeight = textSize.y + 2 * marginHeight; 138 return new Point(textWidth, textHeight); 139 } 140 141 146 public String getText() { 147 return text; 148 } 149 150 153 public String getToolTipText () { 154 checkWidget(); 155 return appToolTipText; 156 } 157 158 161 public void setToolTipText (String string) { 162 super.setToolTipText (string); 163 appToolTipText = super.getToolTipText(); 164 } 165 166 172 public void setText(String text) { 173 if (text != null) 174 this.text = text; 175 else 176 this.text = ""; redraw(); 178 } 179 180 186 protected void paintHyperlink(GC gc) { 187 Rectangle carea = getClientArea(); 188 Rectangle bounds = new Rectangle(marginWidth, marginHeight, carea.width 189 - marginWidth - marginWidth, carea.height - marginHeight 190 - marginHeight); 191 paintText(gc, bounds); 192 } 193 194 202 protected void paintText(GC gc, Rectangle bounds) { 203 gc.setFont(getFont()); 204 gc.setForeground(getForeground()); 205 if ((getStyle() & SWT.WRAP) != 0) { 206 FormUtil.paintWrapText(gc, text, bounds, underlined); 207 } else { 208 Point totalSize = computeTextSize(SWT.DEFAULT, SWT.DEFAULT); 209 boolean shortenText =false; 210 if (bounds.width<totalSize.x) { 211 shortenText=true; 213 } 214 int textWidth = Math.min(bounds.width, totalSize.x); 215 int textHeight = totalSize.y; 216 String textToDraw = getText(); 217 if (shortenText) { 218 textToDraw = shortenText(gc, getText(), bounds.width); 219 if (appToolTipText == null) { 220 super.setToolTipText(getText()); 221 } 222 } 223 else { 224 super.setToolTipText(appToolTipText); 225 } 226 gc.drawText(textToDraw, bounds.x, bounds.y, true); 227 if (underlined) { 228 int descent = gc.getFontMetrics().getDescent(); 229 int lineY = bounds.y + textHeight - descent + 1; 230 gc.drawLine(bounds.x, lineY, bounds.x + textWidth, lineY); 231 } 232 } 233 } 234 235 protected String shortenText(GC gc, String t, int width) { 236 if (t == null) return null; 237 int w = gc.textExtent(ELLIPSIS).x; 238 if (width<=w) return t; 239 int l = t.length(); 240 int max = l/2; 241 int min = 0; 242 int mid = (max+min)/2 - 1; 243 if (mid <= 0) return t; 244 while (min < mid && mid < max) { 245 String s1 = t.substring(0, mid); 246 String s2 = t.substring(l-mid, l); 247 int l1 = gc.textExtent(s1).x; 248 int l2 = gc.textExtent(s2).x; 249 if (l1+w+l2 > width) { 250 max = mid; 251 mid = (max+min)/2; 252 } else if (l1+w+l2 < width) { 253 min = mid; 254 mid = (max+min)/2; 255 } else { 256 min = max; 257 } 258 } 259 if (mid == 0) return t; 260 return t.substring(0, mid)+ELLIPSIS+t.substring(l-mid, l); 261 } 262 263 protected Point computeTextSize(int wHint, int hHint) { 264 Point extent; 265 GC gc = new GC(this); 266 gc.setFont(getFont()); 267 if ((getStyle() & SWT.WRAP) != 0 && wHint != SWT.DEFAULT) { 268 extent = FormUtil.computeWrapSize(gc, getText(), wHint); 269 } else { 270 extent = gc.textExtent(getText()); 271 if ((getStyle() & SWT.WRAP)==0 && wHint!=SWT.DEFAULT) 272 extent.x = wHint; 273 } 274 gc.dispose(); 275 return extent; 276 } 277 } 278 | Popular Tags |