-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
com.archimatetool.editor/src/com/archimatetool/editor/ui/components/IRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.archimatetool.editor.ui.components; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
|
||
import org.eclipse.core.runtime.IProgressMonitor; | ||
import org.eclipse.jface.operation.IRunnableContext; | ||
|
||
/** | ||
* An IRunnableContext and IRunnableWithProgress wrapper that catches and re-throws any Exceptions. Example: | ||
* | ||
* <pre> | ||
* ProgressMonitorDialog dialog = ...; | ||
* try { | ||
* IRunnable.run(dialog, monitor -> { | ||
* // do stuff... | ||
* monitor.setTaskName("New task"); | ||
* }, true); | ||
* } | ||
* catch(Exception ex) { | ||
* ex.printStackTrace(); | ||
* } | ||
* </pre> | ||
* | ||
* @author Phillip Beauvoir | ||
*/ | ||
public interface IRunnable { | ||
|
||
void run(IProgressMonitor monitor) throws Exception; | ||
|
||
/** | ||
* Run the Runnable, catching any Exceptions and re-throwing them | ||
* @param fork true if the runnable should be run in a separate thread and false to run in the same thread | ||
*/ | ||
static void run(IRunnableContext context, IRunnable runnable, boolean fork) throws Exception { | ||
Exception[] exception = new Exception[1]; | ||
|
||
try { | ||
context.run(fork, true, monitor -> { | ||
try { | ||
runnable.run(monitor); | ||
} | ||
catch(Exception ex) { | ||
exception[0] = ex; | ||
} | ||
}); | ||
} | ||
catch(InvocationTargetException ex) { | ||
exception[0] = new Exception(ex.getTargetException()); // we want the target exception | ||
} | ||
catch(InterruptedException ex) { | ||
exception[0] = ex; | ||
} | ||
|
||
if(exception[0] != null) { | ||
throw exception[0]; | ||
} | ||
} | ||
} |