Skip to main content
summaryrefslogtreecommitdiffstats
blob: f23f2328d6f9b19fe9e61aad3e980f31b1ab1a4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*******************************************************************************
 * Copyright (c) 2000, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.help.ui.internal;
import java.util.Enumeration;
import java.util.Hashtable;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

public class HyperlinkHandler
		implements
			MouseListener,
			MouseTrackListener,
			PaintListener,
			Listener {
	public static final int UNDERLINE_NEVER = 1;
	public static final int UNDERLINE_ROLLOVER = 2;
	public static final int UNDERLINE_ALWAYS = 3;
	private Cursor hyperlinkCursor;
	private Cursor busyCursor;
	private boolean hyperlinkCursorUsed = true;
	private int hyperlinkUnderlineMode = UNDERLINE_ALWAYS;
	private Color background;
	private Color foreground;
	private Color activeBackground;
	private Color activeForeground;
	private Hashtable<Control, IHyperlinkListener> hyperlinkListeners;
	private Control lastLink;
	/**
	 * HyperlinkHandler constructor comment.
	 */
	public HyperlinkHandler() {
		hyperlinkListeners = new Hashtable<>();
		hyperlinkCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND);
		busyCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_WAIT);
	}
	/**
	 */
	public void dispose() {
		hyperlinkCursor.dispose();
		busyCursor.dispose();
	}
	/**
	 * @return org.eclipse.swt.graphics.Color
	 */
	public Color getActiveBackground() {
		return activeBackground;
	}
	/**
	 * @return org.eclipse.swt.graphics.Color
	 */
	public Color getActiveForeground() {
		return activeForeground;
	}
	/**
	 * @return org.eclipse.swt.graphics.Color
	 */
	public Color getBackground() {
		return background;
	}
	/**
	 * @return org.eclipse.swt.graphics.Cursor
	 */
	public Cursor getBusyCursor() {
		return busyCursor;
	}
	/**
	 * @return org.eclipse.swt.graphics.Color
	 */
	public Color getForeground() {
		return foreground;
	}
	/**
	 * @return org.eclipse.swt.graphics.Cursor
	 */
	public Cursor getHyperlinkCursor() {
		return hyperlinkCursor;
	}
	/**
	 * @return int
	 */
	public int getHyperlinkUnderlineMode() {
		return hyperlinkUnderlineMode;
	}
	/**
	 * @return org.eclipse.swt.widgets.Control
	 */
	public Control getLastLink() {
		return lastLink;
	}
	/**
	 * @return boolean
	 */
	public boolean isHyperlinkCursorUsed() {
		return hyperlinkCursorUsed;
	}

	@Override
	public void mouseDoubleClick(MouseEvent e) {
	}

	@Override
	public void mouseDown(MouseEvent e) {
		if (e.button == 1)
			return;
		lastLink = (Control) e.widget;
	}

	@Override
	public void mouseEnter(MouseEvent e) {
		Control control = (Control) e.widget;

		if (isHyperlinkCursorUsed())
			control.setCursor(hyperlinkCursor);
		if (activeBackground != null)
			control.setBackground(activeBackground);
		if (activeForeground != null)
			control.setForeground(activeForeground);
		if (hyperlinkUnderlineMode == UNDERLINE_ROLLOVER)
			underline(control, true);
		IHyperlinkListener action = getLinkListener(control);
		if (action != null)
			action.linkEntered(control);
	}

	@Override
	public void mouseExit(MouseEvent e) {
		Control control = (Control) e.widget;

		if (isHyperlinkCursorUsed())
			control.setCursor(null);
		if (hyperlinkUnderlineMode == UNDERLINE_ROLLOVER)
			underline(control, false);
		if (background != null)
			control.setBackground(background);
		if (foreground != null)
			control.setForeground(foreground);
		IHyperlinkListener action = getLinkListener(control);
		if (action != null)
			action.linkExited(control);
	}

	@Override
	public void mouseHover(MouseEvent e) {
	}

	@Override
	public void mouseUp(MouseEvent e) {
		if (e.button != 1)
			return;
		IHyperlinkListener action = getLinkListener((Control) e.widget);

		if (action != null) {
			Control c = (Control) e.widget;
			c.setCursor(busyCursor);
			action.linkActivated(c);
			if (!c.isDisposed())
				c.setCursor(isHyperlinkCursorUsed() ? hyperlinkCursor : null);
		}
	}

	@Override
	public void paintControl(PaintEvent e) {
		Control control = (Control) e.widget;
		if (hyperlinkUnderlineMode == UNDERLINE_ALWAYS)
			HyperlinkHandler.underline(control, true);
	}
	/**
	 * @param control
	 *            org.eclipse.swt.widgets.Control
	 * @param listener
	 *            org.eclipse.help.ui.internal.IHyperlinkListener
	 */
	public void registerHyperlink(Control control, IHyperlinkListener listener) {
		if (background != null)
			control.setBackground(background);
		if (foreground != null)
			control.setForeground(foreground);
		control.addMouseListener(this);
		control.addMouseTrackListener(this);
		control.addListener(SWT.DefaultSelection, this);

		if (hyperlinkUnderlineMode == UNDERLINE_ALWAYS)
			control.addPaintListener(this);
		hyperlinkListeners.put(control, listener);
		removeDisposedLinks();
	}
	public IHyperlinkListener getLinkListener(Control c) {
		if (c instanceof Label)
			c = c.getParent();
		return hyperlinkListeners.get(c);
	}

	private void removeDisposedLinks() {
		for (Enumeration<Control> keys = hyperlinkListeners.keys(); keys
				.hasMoreElements();) {
			Control control = keys.nextElement();
			if (control.isDisposed()) {
				hyperlinkListeners.remove(control);
			}
		}
	}
	/**
	 */
	public void reset() {
		hyperlinkListeners.clear();
	}
	/**
	 * @param newActiveBackground
	 *            org.eclipse.swt.graphics.Color
	 */
	public void setActiveBackground(Color newActiveBackground) {
		activeBackground = newActiveBackground;
	}
	/**
	 * @param newActiveForeground
	 *            org.eclipse.swt.graphics.Color
	 */
	public void setActiveForeground(Color newActiveForeground) {
		activeForeground = newActiveForeground;
	}
	/**
	 * @param newBackground
	 *            org.eclipse.swt.graphics.Color
	 */
	public void setBackground(Color newBackground) {
		background = newBackground;
	}
	/**
	 * @param newForeground
	 *            org.eclipse.swt.graphics.Color
	 */
	public void setForeground(Color newForeground) {
		foreground = newForeground;
	}
	/**
	 * @param newHyperlinkCursorUsed
	 *            boolean
	 */
	public void setHyperlinkCursorUsed(boolean newHyperlinkCursorUsed) {
		hyperlinkCursorUsed = newHyperlinkCursorUsed;
	}
	/**
	 * @param newHyperlinkUnderlineMode
	 *            int
	 */
	public void setHyperlinkUnderlineMode(int newHyperlinkUnderlineMode) {
		hyperlinkUnderlineMode = newHyperlinkUnderlineMode;
	}
	/**
	 * @param control
	 *            org.eclipse.swt.widgets.Control
	 * @param inside
	 *            boolean
	 */
	public static void underline(Control control, boolean inside) {

		if (control instanceof HyperlinkLabel)
			control = ((HyperlinkLabel) control).getLabel();

		Composite parent = control.getParent();
		Rectangle bounds = control.getBounds();
		GC gc = new GC(parent);
		Color color = inside ? control.getForeground() : control
				.getBackground();
		gc.setForeground(color);
		int y = bounds.y + bounds.height;
		gc.drawLine(bounds.x, y, bounds.x + bounds.width, y);
		gc.dispose();
	}

	/**
	 * Sent when an event that the receiver has registered for occurs.
	 *
	 * @param event
	 *            the event which occurred
	 */
	@Override
	public void handleEvent(Event event) {
		IHyperlinkListener listener = getLinkListener((Control) event.widget);
		listener.linkActivated((Control) event.widget);
	}
}

Back to the top