summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/utils/execution/ExecutionUtils.java
blob: d80ff9463eedfe2ead6cf6621faf6c5e454a1175 (plain)
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
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * 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:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.internal.core.utils.execution;

import org.eclipse.swt.widgets.Display;
import org.eclipse.wb.internal.core.DesignerPlugin;
import org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils;

import java.beans.Beans;

/**
 * Utilities for executing actions, such as {@link RunnableEx}.
 *
 * @author scheglov_ke
 * @coverage core.util
 */
public class ExecutionUtils {
  ////////////////////////////////////////////////////////////////////////////
  //
  // Constructor
  //
  ////////////////////////////////////////////////////////////////////////////
  private ExecutionUtils() {
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Sleep
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Sleeps given number of milliseconds, ignoring exceptions.
   */
  public static void sleep(final int millis) {
    runIgnore(new RunnableEx() {
      @Override
    public void run() throws Exception {
        Thread.sleep(millis);
      }
    });
  }

  /**
   * Waits given number of milliseconds and runs events loop every 1 millisecond. At least one
   * events loop will be executed. If current thread is not UI thread, then this method works just
   * as {@link #sleep(int)}.
   */
  public static void waitEventLoop(int millis) {
    Display display = Display.getCurrent();
    if (display != null) {
      long nanos = millis * 1000000L;
      long start = System.nanoTime();
      do {
        sleep(0);
        while (display.readAndDispatch()) {
          // do nothing
        }
      } while (System.nanoTime() - start < nanos);
    } else {
      sleep(millis);
    }
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // void
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Runs given {@link RunnableEx} and ignores exceptions.
   *
   * @return <code>true</code> if execution was finished without exception.
   */
  public static boolean runIgnore(RunnableEx runnable) {
    try {
      runnable.run();
      return true;
    } catch (Throwable e) {
      return false;
    }
  }

  /**
   * Runs given {@link RunnableEx} and logs exceptions using {@link DesignerPlugin#log(Throwable)}.
   *
   * @return <code>true</code> if execution was finished without exception.
   */
  public static boolean runLog(RunnableEx runnable) {
    try {
      runnable.run();
      return true;
    } catch (Throwable e) {
      DesignerPlugin.log(e);
      return false;
    }
  }

  /**
   * Runs given {@link RunnableEx} and re-throws exceptions using {@link RuntimeException}.
   */
  public static void runRethrow(RunnableEx runnable) {
    try {
      runnable.run();
    } catch (Throwable e) {
      throw ReflectionUtils.propagate(e);
    }
  }

  /**
   * Runs given {@link RunnableEx} and re-throws exceptions using {@link RuntimeException}.
   */
  public static void runRethrow(RunnableEx runnable, String format, Object... args) {
    try {
      runnable.run();
    } catch (Throwable e) {
      String message = String.format(format, args);
      throw new RuntimeException(message, e);
    }
  }

  /**
   * Ensures that {@link Beans#isDesignTime()} returns <code>true</code> and runs given
   * {@link RunnableEx}.
   */
  public static void runDesignTime(RunnableEx runnable) throws Exception {
    boolean old_designTime = Beans.isDesignTime();
    try {
      Beans.setDesignTime(true);
      runnable.run();
    } finally {
      Beans.setDesignTime(old_designTime);
    }
  }

  /**
   * Ensures that {@link Beans#isDesignTime()} returns <code>true</code> and runs given
   * {@link RunnableEx}.
   */
  public static <T> T runDesignTime(RunnableObjectEx<T> runnable) throws Exception {
    boolean old_designTime = Beans.isDesignTime();
    try {
      Beans.setDesignTime(true);
      return runnable.runObject();
    } finally {
      Beans.setDesignTime(old_designTime);
    }
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // UI
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Runs given {@link RunnableEx} inside of UI thread, using {@link Display#syncExec(Runnable)}.
   *
   * @return <code>true</code> if {@link RunnableEx} was executed without any {@link Exception}.
   */
  public static boolean runLogUI(final RunnableEx runnable) {
    final boolean[] success = new boolean[1];
    Display.getDefault().syncExec(new Runnable() {
      @Override
    public void run() {
        success[0] = ExecutionUtils.runLog(runnable);
      }
    });
    return success[0];
  }

  /**
   * Runs given {@link RunnableEx} inside of UI thread, using {@link Display#syncExec(Runnable)}.
   */
  public static void runRethrowUI(final RunnableEx runnable) {
    Display.getDefault().syncExec(new Runnable() {
      @Override
    public void run() {
        ExecutionUtils.runRethrow(runnable);
      }
    });
  }

  /**
   * Runs given {@link RunnableEx} within UI thread using {@link Display#asyncExec(Runnable)}. Logs
   * a {@link Throwable} which may occur.
   */
  public static void runAsync(final RunnableEx runnable) {
    Display.getDefault().asyncExec(new Runnable() {
      @Override
    public void run() {
        ExecutionUtils.runLog(runnable);
      }
    });
  }

  /**
   * Runs given {@link RunnableEx} inside of UI thread, using {@link Display#syncExec(Runnable)}.
   */
  @SuppressWarnings("unchecked")
  public static <T> T runObjectUI(final RunnableObjectEx<T> runnable) {
    final Object[] result = new Object[1];
    runRethrowUI(new RunnableEx() {
      @Override
    public void run() throws Exception {
        result[0] = runObject(runnable);
      }
    });
    return (T) result[0];
  }

  /**
   * Runs given {@link RunnableEx} as {@link #runLog(RunnableEx)}, but using
   * {@link Display#asyncExec(Runnable)}.
   */
  public static void runLogLater(final RunnableEx runnable) {
    Display.getDefault().asyncExec(new Runnable() {
      @Override
    public void run() {
        ExecutionUtils.runLog(runnable);
      }
    });
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Object
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Runs given {@link RunnableEx} and re-throws exceptions using {@link RuntimeException}.
   *
   * @return the {@link Object} returned by {@link RunnableEx#run()}.
   */
  public static <T> T runObject(RunnableObjectEx<T> runnable) {
    try {
      return runnable.runObject();
    } catch (Throwable e) {
      throw ReflectionUtils.propagate(e);
    }
  }

  /**
   * Runs given {@link RunnableEx} and re-throws exceptions using {@link RuntimeException}.
   *
   * @return the {@link Object} returned by {@link RunnableEx#run()}.
   */
  public static <T> T runObject(RunnableObjectEx<T> runnable, String format, Object... args) {
    try {
      return runnable.runObject();
    } catch (Throwable e) {
      String message = String.format(format, args);
      throw new Error(message, e);
    }
  }

  /**
   * Runs given {@link RunnableEx} and ignores exceptions.
   *
   * @return the {@link Object} returned by {@link RunnableEx#run()} or <code>defaultValue</code> if
   *         exception happened.
   */
  public static <T> T runObjectIgnore(RunnableObjectEx<T> runnable, T defaultValue) {
    try {
      return runnable.runObject();
    } catch (Throwable e) {
      return defaultValue;
    }
  }

  /**
   * Runs given {@link RunnableEx} and logs exceptions using {@link DesignerPlugin#log(Throwable)}.
   *
   * @return the {@link Object} returned by {@link RunnableEx#run()} or <code>defaultValue</code> if
   *         exception was logged.
   */
  public static <T> T runObjectLog(RunnableObjectEx<T> runnable, T defaultValue) {
    try {
      return runnable.runObject();
    } catch (Throwable e) {
      DesignerPlugin.log(e);
      return defaultValue;
    }
  }

}