Skip to main content
summaryrefslogtreecommitdiffstats
blob: 28becce163c76e09b56af41a64a86cb4180035e0 (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
/*******************************************************************************
 * Copyright (c) 2013, 2017 Obeo 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:
 *     Obeo - initial API and implementation
 *     Philip Langer - check if control is disposed before redraw
 *******************************************************************************/
package org.eclipse.emf.compare.rcp.ui.internal.util;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

/**
 * Utility class for running operation in the UI-Thread if not already within it.
 * 
 * @author <a href="mailto:mikael.barbero@obeo.fr">Mikael Barbero</a>
 */
public final class SWTUtil {

	private SWTUtil() {
		// prevent instantiation.
	}

	/**
	 * Causes the run() method of the runnable to be invoked by the user-interface thread at the next
	 * reasonable opportunity. The caller of this method continues to run in parallel, and is not notified
	 * when the runnable has completed.
	 * 
	 * @param runnable
	 *            runnable code to run on the user-interface thread.
	 */
	public static void safeAsyncExec(final Runnable runnable) {
		if (Display.getCurrent() != null) {
			Display.getCurrent().asyncExec(runnable);
		} else {
			Display.getDefault().asyncExec(runnable);
		}
	}

	/**
	 * Causes the run() method of the runnable to be invoked by the user-interface thread at the next
	 * reasonable opportunity. The thread which calls this method is suspended until the runnable completes.
	 * 
	 * @param runnable
	 *            runnable code to run on the user-interface thread.
	 */
	public static void safeSyncExec(final Runnable runnable) {
		if (Display.getCurrent() != null) {
			runnable.run();
		} else {
			Display.getDefault().syncExec(runnable);
		}
	}

	/**
	 * Run {@link Viewer#refresh()} on the given viewer.
	 * 
	 * @param viewer
	 *            the viewer to refresh.
	 * @param async
	 *            whether the thread which calls this method is not suspended until the runnable completes.
	 * @param checkDisposed
	 *            whether the viewer's control dispose state has to be checked before refresh.
	 */
	public static void safeRefresh(final Viewer viewer, boolean async, final boolean checkDisposed) {
		Runnable runnable = new Runnable() {
			public void run() {
				if (checkDisposed) {
					Control control = viewer.getControl();
					if (control != null && !control.isDisposed()) {
						viewer.refresh();
					}
				} else {
					viewer.refresh();
				}
			}
		};
		if (async) {
			safeAsyncExec(runnable);
		} else {
			safeSyncExec(runnable);
		}
	}

	/**
	 * Run {@link Control#redraw()} on the given viewer.
	 * 
	 * @param redraw
	 *            the control to redraw.
	 * @param async
	 *            whether the thread which calls this method is not suspended until the runnable completes.
	 */
	public static void safeRedraw(final Control control, boolean async) {
		Runnable runnable = new Runnable() {
			public void run() {
				if (!control.isDisposed()) {
					control.redraw();
				}
			}
		};
		if (async) {
			safeAsyncExec(runnable);
		} else {
			safeSyncExec(runnable);
		}
	}
}

Back to the top