Coverage Report - org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable
 
Classes in this File Line Coverage Branch Coverage Complexity
UIThreadRunnable
90%
38/42
75%
6/8
1.263
UIThreadRunnable$1
100%
5/5
N/A
1.263
UIThreadRunnable$2
100%
4/4
N/A
1.263
UIThreadRunnable$3
100%
5/5
N/A
1.263
UIThreadRunnable$4
100%
4/4
N/A
1.263
UIThreadRunnable$5
100%
4/4
N/A
1.263
 
 1  7372
 /*******************************************************************************
 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  63451
         private UIThreadRunnable(Display display) {
 49  63451
                 this(display, false);
 50  63451
         }
 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  78195
         private UIThreadRunnable(Display display, boolean async) {
 61  70823
                 this.display = display;
 62  70823
                 this.async = async;
 63  70823
         }
 64  
 
 65  
         /**
 66  
          * This method is intelligent to execute in the UI thread.
 67  
          */
 68  
         public void run() {
 69  70823
                 if ((display == null) || display.isDisposed())
 70  0
                         return;
 71  
 
 72  70823
                 if (!isUIThread(display)) {
 73  27223
                         if (async)
 74  7244
                                 display.asyncExec(runnable());
 75  
                         else
 76  19979
                                 display.syncExec(runnable());
 77  
                 } else
 78  43600
                         doRun();
 79  70823
         }
 80  
 
 81  
         /**
 82  
          * A runnable instance that is used internally.
 83  
          * 
 84  
          * @return The runnable instance.
 85  
          */
 86  
         private Runnable runnable() {
 87  27223
                 final Runnable runnable = new Runnable() {
 88  
                         public void run() {
 89  27223
                                 doRun();
 90  27223
                                 dispatchAllEvents();
 91  27223
                         }
 92  
                 };
 93  27223
                 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  27223
         private void dispatchAllEvents() {
 103  27223
                 display.wake();
 104  
                 // while (true)
 105  
                 // if (!display.readAndDispatch())
 106  
                 // break;
 107  27223
         }
 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  117
                 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  50042
                 final ArrayList<T> arrayList = new ArrayList<T>();
 138  50042
                 new UIThreadRunnable(display) {
 139  
                         protected void doRun() {
 140  50042
                                 arrayList.add(toExecute.run());
 141  50042
                         }
 142  50042
                 }.run();
 143  50042
                 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  6697
                 final ArrayList<T[]> arrayList = new ArrayList<T[]>();
 167  6697
                 new UIThreadRunnable(display) {
 168  
                         protected void doRun() {
 169  6697
                                 T[] run = toExecute.run();
 170  6697
                                 arrayList.add(run);
 171  6697
                         }
 172  6697
                 }.run();
 173  6697
                 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  6585
                 syncExec(display(), toExecute);
 184  6585
         }
 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  6712
                 new UIThreadRunnable(display) {
 194  
                         @Override
 195  
                         protected void doRun() {
 196  6712
                                 toExecute.run();
 197  6712
                         }
 198  6712
                 }.run();
 199  6712
         }
 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  7372
                 new UIThreadRunnable(display, true) {
 219  
                         @Override
 220  
                         protected void doRun() {
 221  7372
                                 toExecute.run();
 222  7372
                         }
 223  7372
                 }.run();
 224  7372
         }
 225  
 
 226  
 }