| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| UIThreadRunnable |
|
| 1.263157894736842;1.263 | ||||
| UIThreadRunnable$1 |
|
| 1.263157894736842;1.263 | ||||
| UIThreadRunnable$2 |
|
| 1.263157894736842;1.263 | ||||
| UIThreadRunnable$3 |
|
| 1.263157894736842;1.263 | ||||
| UIThreadRunnable$4 |
|
| 1.263157894736842;1.263 | ||||
| UIThreadRunnable$5 |
|
| 1.263157894736842;1.263 |
| 1 | 8617 | /******************************************************************************* |
| 2 | * Copyright (c) 2008 Ketan Padegaonkar and others. | |
| 3 | * All rights reserved. This program and the accompanying materials | |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 | |
| 5 | * which accompanies this distribution, and is available at | |
| 6 | * http://www.eclipse.org/legal/epl-v10.html | |
| 7 | * | |
| 8 | * Contributors: | |
| 9 | * Ketan Padegaonkar - initial API and implementation | |
| 10 | *******************************************************************************/ | |
| 11 | package org.eclipse.swtbot.swt.finder.finders; | |
| 12 | ||
| 13 | import static org.eclipse.swtbot.swt.finder.utils.SWTUtils.display; | |
| 14 | import static org.eclipse.swtbot.swt.finder.utils.SWTUtils.isUIThread; | |
| 15 | ||
| 16 | import java.util.ArrayList; | |
| 17 | ||
| 18 | import org.apache.log4j.Logger; | |
| 19 | import org.eclipse.swt.widgets.Display; | |
| 20 | import org.eclipse.swtbot.swt.finder.results.ArrayResult; | |
| 21 | import org.eclipse.swtbot.swt.finder.results.Result; | |
| 22 | import org.eclipse.swtbot.swt.finder.results.VoidResult; | |
| 23 | ||
| 24 | /** | |
| 25 | * Performs operations in the UI thread. If the {@link #run()} method of this class is called from an non-UI thread, the | |
| 26 | * instance ensures that it runs in the UI thread by invoking {@link Display#syncExec(Runnable)}, else it executes in | |
| 27 | * the UI thread. All operations are blocking operations. | |
| 28 | * | |
| 29 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
| 30 | * @version $Id$ | |
| 31 | */ | |
| 32 | 1 | public abstract class UIThreadRunnable implements Runnable { |
| 33 | ||
| 34 | 1 | private static final Logger log = Logger.getLogger(UIThreadRunnable.class); |
| 35 | ||
| 36 | /** the display on which runnables must be executed. */ | |
| 37 | protected final Display display; | |
| 38 | /** | |
| 39 | * A flag to denote if the runnable should execute asynchronously. | |
| 40 | */ | |
| 41 | private final boolean async; | |
| 42 | ||
| 43 | /** | |
| 44 | * Runs synchronously in the UI thread. | |
| 45 | * | |
| 46 | * @param display The display to be used. | |
| 47 | */ | |
| 48 | 86454 | private UIThreadRunnable(Display display) { |
| 49 | 86454 | this(display, false); |
| 50 | 86454 | } |
| 51 | ||
| 52 | /** | |
| 53 | * A private contructor use to create this object. | |
| 54 | * | |
| 55 | * @param display The display to use. | |
| 56 | * @param async if the thread should run asynchronously or not. | |
| 57 | * @see Display#syncExec(Runnable) | |
| 58 | * @see Display#asyncExec(Runnable) | |
| 59 | */ | |
| 60 | 103688 | private UIThreadRunnable(Display display, boolean async) { |
| 61 | 95071 | this.display = display; |
| 62 | 95071 | this.async = async; |
| 63 | 95071 | } |
| 64 | ||
| 65 | /** | |
| 66 | * This method is intelligent to execute in the UI thread. | |
| 67 | */ | |
| 68 | public void run() { | |
| 69 | 95071 | if ((display == null) || display.isDisposed()) |
| 70 | 0 | return; |
| 71 | ||
| 72 | 95071 | if (!isUIThread(display)) { |
| 73 | 35907 | if (async) |
| 74 | 8481 | display.asyncExec(runnable()); |
| 75 | else | |
| 76 | 27426 | display.syncExec(runnable()); |
| 77 | } else | |
| 78 | 59164 | doRun(); |
| 79 | 95071 | } |
| 80 | ||
| 81 | /** | |
| 82 | * A runnable instance that is used internally. | |
| 83 | * | |
| 84 | * @return The runnable instance. | |
| 85 | */ | |
| 86 | private Runnable runnable() { | |
| 87 | 35907 | final Runnable runnable = new Runnable() { |
| 88 | public void run() { | |
| 89 | 35907 | doRun(); |
| 90 | 35907 | dispatchAllEvents(); |
| 91 | 35907 | } |
| 92 | }; | |
| 93 | 35907 | return runnable; |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * This dispatched events in the UI thread. | |
| 98 | * <p> | |
| 99 | * This must be called in the UI thread only. This method does not execute in a syncexec/asyncexec block | |
| 100 | * </p> | |
| 101 | */ | |
| 102 | 35907 | private void dispatchAllEvents() { |
| 103 | 35907 | display.wake(); |
| 104 | // while (true) | |
| 105 | // if (!display.readAndDispatch()) | |
| 106 | // break; | |
| 107 | 35907 | } |
| 108 | ||
| 109 | /** | |
| 110 | * Performs the run in the UI Thread. | |
| 111 | * <p> | |
| 112 | * This MUST be invoked in the UI thread. | |
| 113 | * </p> | |
| 114 | */ | |
| 115 | protected abstract void doRun(); | |
| 116 | ||
| 117 | /** | |
| 118 | * Executes the {@code toExecute} on the UI thread, and blocks the calling thread. | |
| 119 | * | |
| 120 | * @param <T> the type of the result. | |
| 121 | * @param toExecute the runnable to execute. | |
| 122 | * @return the result of executing result on the UI thread. | |
| 123 | */ | |
| 124 | public static <T> T syncExec(final Result<T> toExecute) { | |
| 125 | 128 | return syncExec(display(), toExecute); |
| 126 | } | |
| 127 | ||
| 128 | /** | |
| 129 | * Executes the {@code toExecute} on the display thread, and blocks the calling thread. | |
| 130 | * | |
| 131 | * @param <T> the type of the result. | |
| 132 | * @param display the display on which toExecute must be executed. | |
| 133 | * @param toExecute the runnable to execute. | |
| 134 | * @return the object result of execution on the UI thread. | |
| 135 | */ | |
| 136 | public static <T> T syncExec(Display display, final Result<T> toExecute) { | |
| 137 | 70004 | final ArrayList<T> arrayList = new ArrayList<T>(); |
| 138 | 70004 | new UIThreadRunnable(display) { |
| 139 | protected void doRun() { | |
| 140 | 70004 | arrayList.add(toExecute.run()); |
| 141 | 70004 | } |
| 142 | 70004 | }.run(); |
| 143 | 70004 | return arrayList.get(0); |
| 144 | } | |
| 145 | ||
| 146 | /** | |
| 147 | * Executes the {@code toExecute} on the display thread, and blocks the calling thread. | |
| 148 | * | |
| 149 | * @param <T> the type of the result. | |
| 150 | * @param toExecute the runnable to execute. | |
| 151 | * @return the object result of execution on the UI thread. | |
| 152 | */ | |
| 153 | public static <T> T[] syncExec(final ArrayResult<T> toExecute) { | |
| 154 | 0 | return syncExec(display(), toExecute); |
| 155 | } | |
| 156 | ||
| 157 | /** | |
| 158 | * Executes the {@code toExecute} on the display thread, and blocks the calling thread. | |
| 159 | * | |
| 160 | * @param <T> the type of the result. | |
| 161 | * @param display the display on which toExecute must be executed. | |
| 162 | * @param toExecute the runnable to execute. | |
| 163 | * @return the object result of execution on the UI thread. | |
| 164 | */ | |
| 165 | public static <T> T[] syncExec(Display display, final ArrayResult<T> toExecute) { | |
| 166 | 7805 | final ArrayList<T[]> arrayList = new ArrayList<T[]>(); |
| 167 | 7805 | new UIThreadRunnable(display) { |
| 168 | protected void doRun() { | |
| 169 | 7805 | T[] run = toExecute.run(); |
| 170 | 7805 | arrayList.add(run); |
| 171 | 7805 | } |
| 172 | 7805 | }.run(); |
| 173 | 7805 | return arrayList.get(0); |
| 174 | } | |
| 175 | ||
| 176 | /** | |
| 177 | * Executes the {@code toExecute} on the UI thread, and blocks the calling thread. | |
| 178 | * | |
| 179 | * @param toExecute the runnable to execute. | |
| 180 | * @since 1.0 | |
| 181 | */ | |
| 182 | public static void syncExec(final VoidResult toExecute) { | |
| 183 | 7703 | syncExec(display(), toExecute); |
| 184 | 7703 | } |
| 185 | ||
| 186 | /** | |
| 187 | * Executes the {@code toExecute} on the display thread, and blocks the calling thread. | |
| 188 | * | |
| 189 | * @param display the display on which toExecute must be executed. | |
| 190 | * @param toExecute the runnable to execute. | |
| 191 | */ | |
| 192 | public static void syncExec(Display display, final VoidResult toExecute) { | |
| 193 | 8645 | new UIThreadRunnable(display) { |
| 194 | @Override | |
| 195 | protected void doRun() { | |
| 196 | 8645 | toExecute.run(); |
| 197 | 8645 | } |
| 198 | 8645 | }.run(); |
| 199 | 8645 | } |
| 200 | ||
| 201 | /** | |
| 202 | * Executes the {@code toExecute} on the UI thread asynchronously, and does not block the calling thread. | |
| 203 | * | |
| 204 | * @param toExecute the runnable to execute. | |
| 205 | * @since 1.0 | |
| 206 | */ | |
| 207 | public static void asyncExec(final VoidResult toExecute) { | |
| 208 | 0 | asyncExec(display(), toExecute); |
| 209 | 0 | } |
| 210 | ||
| 211 | /** | |
| 212 | * Executes the {@code toExecute} on the UI thread asynchronously, and does not block the calling thread. | |
| 213 | * | |
| 214 | * @param display the display on which toExecute must be executed. | |
| 215 | * @param toExecute the runnable to execute. | |
| 216 | */ | |
| 217 | public static void asyncExec(Display display, final VoidResult toExecute) { | |
| 218 | 8617 | new UIThreadRunnable(display, true) { |
| 219 | @Override | |
| 220 | protected void doRun() { | |
| 221 | 8617 | toExecute.run(); |
| 222 | 8617 | } |
| 223 | 8617 | }.run(); |
| 224 | 8617 | } |
| 225 | ||
| 226 | } |