1 11 package org.eclipse.ui.forms.widgets; 12 13 import org.eclipse.core.runtime.ListenerList; 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.accessibility.ACC; 16 import org.eclipse.swt.events.PaintEvent; 17 import org.eclipse.swt.events.PaintListener; 18 import org.eclipse.swt.graphics.GC; 19 import org.eclipse.swt.graphics.Point; 20 import org.eclipse.swt.graphics.Rectangle; 21 import org.eclipse.swt.widgets.Canvas; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.swt.widgets.Event; 24 import org.eclipse.swt.widgets.Listener; 25 import org.eclipse.ui.forms.events.HyperlinkEvent; 26 import org.eclipse.ui.forms.events.IHyperlinkListener; 27 import org.eclipse.ui.internal.forms.widgets.FormsResources; 28 29 37 public abstract class AbstractHyperlink extends Canvas { 38 private boolean hasFocus; 39 boolean paintFocus=true; 40 41 45 private boolean armed; 46 47 private ListenerList listeners; 48 49 52 protected int marginWidth = 1; 53 54 57 protected int marginHeight = 1; 58 59 67 public AbstractHyperlink(Composite parent, int style) { 68 super(parent, style); 69 addListener(SWT.KeyDown, new Listener() { 70 public void handleEvent(Event e) { 71 if (e.character == '\r') { 72 handleActivate(e); 73 } 74 } 75 }); 76 addPaintListener(new PaintListener() { 77 public void paintControl(PaintEvent e) { 78 paint(e); 79 } 80 }); 81 addListener(SWT.Traverse, new Listener() { 82 public void handleEvent(Event e) { 83 switch (e.detail) { 84 case SWT.TRAVERSE_PAGE_NEXT: 85 case SWT.TRAVERSE_PAGE_PREVIOUS: 86 case SWT.TRAVERSE_ARROW_NEXT: 87 case SWT.TRAVERSE_ARROW_PREVIOUS: 88 case SWT.TRAVERSE_RETURN: 89 e.doit = false; 90 return; 91 } 92 e.doit = true; 93 } 94 }); 95 Listener listener = new Listener() { 96 public void handleEvent(Event e) { 97 switch (e.type) { 98 case SWT.FocusIn: 99 hasFocus = true; 100 handleEnter(e); 101 break; 102 case SWT.FocusOut: 103 hasFocus = false; 104 handleExit(e); 105 break; 106 case SWT.DefaultSelection: 107 handleActivate(e); 108 break; 109 case SWT.MouseEnter: 110 handleEnter(e); 111 break; 112 case SWT.MouseExit: 113 handleExit(e); 114 break; 115 case SWT.MouseDown: 116 handleMouseDown(e); 117 break; 118 case SWT.MouseUp: 119 handleMouseUp(e); 120 break; 121 case SWT.MouseMove: 122 handleMouseMove(e); 123 break; 124 } 125 } 126 }; 127 addListener(SWT.MouseEnter, listener); 128 addListener(SWT.MouseExit, listener); 129 addListener(SWT.MouseDown, listener); 130 addListener(SWT.MouseUp, listener); 131 addListener(SWT.MouseMove, listener); 132 addListener(SWT.FocusIn, listener); 133 addListener(SWT.FocusOut, listener); 134 setCursor(FormsResources.getHandCursor()); 135 } 136 137 143 public void addHyperlinkListener(IHyperlinkListener listener) { 144 if (listeners == null) 145 listeners = new ListenerList(); 146 listeners.add(listener); 147 } 148 149 155 public void removeHyperlinkListener(IHyperlinkListener listener) { 156 if (listeners == null) 157 return; 158 listeners.remove(listener); 159 } 160 161 169 public boolean getSelection() { 170 return hasFocus; 171 } 172 173 177 protected void handleEnter(Event e) { 178 redraw(); 179 if (listeners == null) 180 return; 181 int size = listeners.size(); 182 HyperlinkEvent he = new HyperlinkEvent(this, getHref(), getText(), 183 e.stateMask); 184 Object [] listenerList = listeners.getListeners(); 185 for (int i = 0; i < size; i++) { 186 IHyperlinkListener listener = (IHyperlinkListener) listenerList[i]; 187 listener.linkEntered(he); 188 } 189 } 190 191 195 protected void handleExit(Event e) { 196 armed = false; 198 redraw(); 199 if (listeners == null) 200 return; 201 int size = listeners.size(); 202 HyperlinkEvent he = new HyperlinkEvent(this, getHref(), getText(), 203 e.stateMask); 204 Object [] listenerList = listeners.getListeners(); 205 for (int i = 0; i < size; i++) { 206 IHyperlinkListener listener = (IHyperlinkListener) listenerList[i]; 207 listener.linkExited(he); 208 } 209 } 210 211 215 protected void handleActivate(Event e) { 216 armed = false; 218 getAccessible().setFocus(ACC.CHILDID_SELF); 219 if (listeners == null) 220 return; 221 int size = listeners.size(); 222 setCursor(FormsResources.getBusyCursor()); 223 HyperlinkEvent he = new HyperlinkEvent(this, getHref(), getText(), 224 e.stateMask); 225 Object [] listenerList = listeners.getListeners(); 226 for (int i = 0; i < size; i++) { 227 IHyperlinkListener listener = (IHyperlinkListener) listenerList[i]; 228 listener.linkActivated(he); 229 } 230 if (!isDisposed()) 231 setCursor(FormsResources.getHandCursor()); 232 } 233 234 242 public void setHref(Object href) { 243 setData("href", href); } 245 246 252 public Object getHref() { 253 return getData("href"); } 255 256 262 public String getText() { 263 return getToolTipText(); 264 } 265 266 272 protected abstract void paintHyperlink(GC gc); 273 274 280 protected void paint(PaintEvent e) { 281 GC gc = e.gc; 282 Rectangle clientArea = getClientArea(); 283 if (clientArea.width == 0 || clientArea.height == 0) 284 return; 285 paintHyperlink(gc); 286 if (paintFocus && hasFocus) { 287 Rectangle carea = getClientArea(); 288 gc.setForeground(getForeground()); 289 gc.drawFocus(0, 0, carea.width, carea.height); 290 } 291 } 292 293 private void handleMouseDown(Event e) { 294 if (e.button != 1) 295 return; 296 armed = true; 298 } 299 300 private void handleMouseUp(Event e) { 301 if (!armed || e.button != 1) 302 return; 303 Point size = getSize(); 304 if (e.x < 0) 309 return; 310 if (e.y < 0) 311 return; 312 if (e.x >= size.x) 313 return; 314 if (e.y >= size.y) 315 return; 316 handleActivate(e); 317 } 318 319 private void handleMouseMove(Event e) { 320 if (armed) { 322 Point size = getSize(); 323 armed = (e.x >= 0 && e.y >= 0 && e.x < size.x && e.y < size.y); 324 } 325 } 326 327 331 332 public void setEnabled (boolean enabled) { 333 super.setEnabled(enabled); 334 redraw(); 335 } 336 } 337 | Popular Tags |