1 11 package org.eclipse.help.ui.internal; 12 import java.util.Enumeration ; 13 import java.util.Hashtable ; 14 15 import org.eclipse.swt.*; 16 import org.eclipse.swt.events.*; 17 import org.eclipse.swt.graphics.*; 18 import org.eclipse.swt.widgets.*; 19 20 public class HyperlinkHandler 21 implements 22 MouseListener, 23 MouseTrackListener, 24 PaintListener, 25 Listener { 26 public static final int UNDERLINE_NEVER = 1; 27 public static final int UNDERLINE_ROLLOVER = 2; 28 public static final int UNDERLINE_ALWAYS = 3; 29 private Cursor hyperlinkCursor; 30 private Cursor busyCursor; 31 private boolean hyperlinkCursorUsed = true; 32 private int hyperlinkUnderlineMode = UNDERLINE_ALWAYS; 33 private Color background; 34 private Color foreground; 35 private Color activeBackground; 36 private Color activeForeground; 37 private Hashtable hyperlinkListeners; 38 private Control lastLink; 39 42 public HyperlinkHandler() { 43 hyperlinkListeners = new Hashtable (); 44 hyperlinkCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND); 45 busyCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_WAIT); 46 } 47 49 public void dispose() { 50 hyperlinkCursor.dispose(); 51 busyCursor.dispose(); 52 } 53 56 public Color getActiveBackground() { 57 return activeBackground; 58 } 59 62 public Color getActiveForeground() { 63 return activeForeground; 64 } 65 68 public Color getBackground() { 69 return background; 70 } 71 74 public Cursor getBusyCursor() { 75 return busyCursor; 76 } 77 80 public Color getForeground() { 81 return foreground; 82 } 83 86 public Cursor getHyperlinkCursor() { 87 return hyperlinkCursor; 88 } 89 92 public int getHyperlinkUnderlineMode() { 93 return hyperlinkUnderlineMode; 94 } 95 98 public Control getLastLink() { 99 return lastLink; 100 } 101 104 public boolean isHyperlinkCursorUsed() { 105 return hyperlinkCursorUsed; 106 } 107 public void mouseDoubleClick(MouseEvent e) { 108 } 109 public void mouseDown(MouseEvent e) { 110 if (e.button == 1) 111 return; 112 lastLink = (Control) e.widget; 113 } 114 public void mouseEnter(MouseEvent e) { 115 Control control = (Control) e.widget; 116 117 if (isHyperlinkCursorUsed()) 118 control.setCursor(hyperlinkCursor); 119 if (activeBackground != null) 120 control.setBackground(activeBackground); 121 if (activeForeground != null) 122 control.setForeground(activeForeground); 123 if (hyperlinkUnderlineMode == UNDERLINE_ROLLOVER) 124 underline(control, true); 125 IHyperlinkListener action = getLinkListener(control); 126 if (action != null) 127 action.linkEntered(control); 128 } 129 public void mouseExit(MouseEvent e) { 130 Control control = (Control) e.widget; 131 132 if (isHyperlinkCursorUsed()) 133 control.setCursor(null); 134 if (hyperlinkUnderlineMode == UNDERLINE_ROLLOVER) 135 underline(control, false); 136 if (background != null) 137 control.setBackground(background); 138 if (foreground != null) 139 control.setForeground(foreground); 140 IHyperlinkListener action = getLinkListener(control); 141 if (action != null) 142 action.linkExited(control); 143 } 144 public void mouseHover(MouseEvent e) { 145 } 146 public void mouseUp(MouseEvent e) { 147 if (e.button != 1) 148 return; 149 IHyperlinkListener action = getLinkListener((Control) e.widget); 150 151 if (action != null) { 152 Control c = (Control) e.widget; 153 c.setCursor(busyCursor); 154 action.linkActivated(c); 155 if (!c.isDisposed()) 156 c.setCursor(isHyperlinkCursorUsed() ? hyperlinkCursor : null); 157 } 158 } 159 public void paintControl(PaintEvent e) { 160 Control control = (Control) e.widget; 161 if (hyperlinkUnderlineMode == UNDERLINE_ALWAYS) 162 HyperlinkHandler.underline(control, true); 163 } 164 170 public void registerHyperlink(Control control, IHyperlinkListener listener) { 171 if (background != null) 172 control.setBackground(background); 173 if (foreground != null) 174 control.setForeground(foreground); 175 control.addMouseListener(this); 176 control.addMouseTrackListener(this); 177 control.addListener(SWT.DefaultSelection, this); 178 179 if (hyperlinkUnderlineMode == UNDERLINE_ALWAYS) 180 control.addPaintListener(this); 181 hyperlinkListeners.put(control, listener); 182 removeDisposedLinks(); 183 } 184 public IHyperlinkListener getLinkListener(Control c) { 185 if (c instanceof Label) 186 c = c.getParent(); 187 return (IHyperlinkListener) hyperlinkListeners.get(c); 188 } 189 190 private void removeDisposedLinks() { 191 for (Enumeration keys = hyperlinkListeners.keys(); keys 192 .hasMoreElements();) { 193 Control control = (Control) keys.nextElement(); 194 if (control.isDisposed()) { 195 hyperlinkListeners.remove(control); 196 } 197 } 198 } 199 201 public void reset() { 202 hyperlinkListeners.clear(); 203 } 204 208 public void setActiveBackground(Color newActiveBackground) { 209 activeBackground = newActiveBackground; 210 } 211 215 public void setActiveForeground(Color newActiveForeground) { 216 activeForeground = newActiveForeground; 217 } 218 222 public void setBackground(Color newBackground) { 223 background = newBackground; 224 } 225 229 public void setForeground(Color newForeground) { 230 foreground = newForeground; 231 } 232 236 public void setHyperlinkCursorUsed(boolean newHyperlinkCursorUsed) { 237 hyperlinkCursorUsed = newHyperlinkCursorUsed; 238 } 239 243 public void setHyperlinkUnderlineMode(int newHyperlinkUnderlineMode) { 244 hyperlinkUnderlineMode = newHyperlinkUnderlineMode; 245 } 246 252 public static void underline(Control control, boolean inside) { 253 254 if (control instanceof HyperlinkLabel) 255 control = ((HyperlinkLabel) control).getLabel(); 256 257 Composite parent = control.getParent(); 258 Rectangle bounds = control.getBounds(); 259 GC gc = new GC(parent); 260 Color color = inside ? control.getForeground() : control 261 .getBackground(); 262 gc.setForeground(color); 263 int y = bounds.y + bounds.height; 264 gc.drawLine(bounds.x, y, bounds.x + bounds.width, y); 265 gc.dispose(); 266 } 267 268 274 public void handleEvent(Event event) { 275 IHyperlinkListener listener = getLinkListener((Control) event.widget); 276 listener.linkActivated((Control) event.widget); 277 } 278 } 279 | Popular Tags |