Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0177c7b2a00e10ad6679523a8a1b5c1a488887cf (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
/*******************************************************************************
 * Copyright (C) 2016, Thomas Wolf <thomas.wolf@paranor.ch>
 *
 * 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
 *******************************************************************************/
package org.eclipse.egit.ui.internal.commit;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Listener;

/**
 * Utility class to track the focus of a set of {@link Control}s to determine
 * the one that last had the focus.
 */
public class FocusTracker {

	private final Set<Control> trackedControls = new HashSet<>();

	private final Listener listener = event -> {
		switch (event.type) {
		case SWT.Dispose:
			trackedControls.remove(event.widget);
			break;
		case SWT.FocusIn:
		case SWT.FocusOut:
			if (event.widget instanceof Control) {
				lastFocusControl = (Control) event.widget;
			}
			break;
		default:
			break;
		}
	};

	private Control lastFocusControl;

	private void hookControl(@NonNull Control control) {
		control.addListener(SWT.Dispose, listener);
		control.addListener(SWT.FocusIn, listener);
		control.addListener(SWT.FocusOut, listener);
	}

	private void unhookControl(@NonNull Control control) {
		control.removeListener(SWT.FocusOut, listener);
		control.removeListener(SWT.FocusIn, listener);
		control.removeListener(SWT.Dispose, listener);
	}

	/**
	 * Registers the given {@link Control} for having focus changes being
	 * tracked. Has no effect if the control is already being tracked.
	 *
	 * @param control
	 *            to track
	 */
	public void addToFocusTracking(@NonNull Control control) {
		if (trackedControls.add(control)) {
			hookControl(control);
		}
	}

	/**
	 * Stops tracking focus changes for the given {@link Control}. Has no effect
	 * if the control is {@code null} or is not currently being tracked.
	 *
	 * @param control
	 *            to remove from focus tracking
	 */
	public void removeFromFocusTracking(Control control) {
		if (trackedControls.remove(control) && !control.isDisposed()) {
			unhookControl(control);
		}
	}

	/**
	 * Retrieves the last control to have had the focus.
	 *
	 * @return the control, or {@code null} if none determined yet.
	 */
	public Control getLastFocusControl() {
		if (lastFocusControl != null && lastFocusControl.isDisposed()) {
			trackedControls.remove(lastFocusControl);
			lastFocusControl = null;
		}
		return lastFocusControl;
	}

	/**
	 * Dispose of this {@link FocusTracker}.
	 */
	public void dispose() {
		for (Control tracked : trackedControls) {
			if (!tracked.isDisposed()) {
				unhookControl(tracked);
			}
		}
		trackedControls.clear();
		lastFocusControl = null;
	}
}

Back to the top