1616import java .util .Map ;
1717import java .util .Optional ;
1818import java .util .concurrent .CompletableFuture ;
19+ import java .util .concurrent .CompletionException ;
1920
2021import org .eclipse .core .runtime .Assert ;
2122import org .eclipse .core .runtime .CoreException ;
2223import org .eclipse .core .runtime .ILog ;
2324import org .eclipse .core .runtime .ISafeRunnable ;
24- import org .eclipse .core .runtime .IStatus ;
2525import org .eclipse .core .runtime .ListenerList ;
2626import org .eclipse .core .runtime .SafeRunner ;
2727import org .eclipse .core .runtime .Status ;
@@ -68,8 +68,11 @@ public class TerminalService implements ITerminalService {
6868
6969 /**
7070 * Common terminal service runnable implementation.
71+ *
72+ * These runnables are run asynchronously on the UI thread.
7173 */
72- protected static abstract class TerminalServiceRunnable {
74+ @ FunctionalInterface
75+ private static interface TerminalServiceRunnable {
7376
7477 /**
7578 * Invoked to execute the terminal service runnable.
@@ -78,21 +81,11 @@ protected static abstract class TerminalServiceRunnable {
7881 * @param title The terminal tab title. Must not be <code>null</code>.
7982 * @param connector The terminal connector. Must not be <code>null</code>.
8083 * @param data The custom terminal data node or <code>null</code>.
81- * @return the result {@link IStatus}
82- */
83- public abstract IStatus run (TerminalViewId tvid , String title , ITerminalConnector connector , Object data );
84-
85- /**
86- * Returns if or if not to execute the runnable asynchronously.
87- * <p>
88- * The method returns per default <code>true</code>. Overwrite to
89- * modify the behavior.
90- *
91- * @return <code>True</code> to execute the runnable asynchronously, <code>false</code> otherwise.
84+ * @throws CoreException if operation does not complete successfully
85+ * @throws NullPointerException if any required arguments are null.
9286 */
93- public boolean isExecuteAsync () {
94- return true ;
95- }
87+ public abstract void run (TerminalViewId tvid , String title , ITerminalConnector connector , Object data )
88+ throws CoreException ;
9689 }
9790
9891 @ Activate
@@ -194,18 +187,14 @@ protected final CompletableFuture<?> executeServiceOperation(final Map<String, O
194187 private CompletableFuture <?> executeServiceOperation (final TerminalServiceRunnable runnable , TerminalViewId tvid ,
195188 final String title , final ITerminalConnector connector , final Object data ) {
196189 try { // Execute the operation
197- if (runnable .isExecuteAsync ()) {
198- return CompletableFuture .supplyAsync (() -> runnable .run (tvid , title , connector , data ),
199- /*
200- * A special executor is used here because the default Display executor will
201- * do a syncExec here if called from the UI thread, but isExecuteAsync is true so we
202- * must run async on the UI thread.
203- */
204- Display .getDefault ()::asyncExec );
205-
206- } else {
207- return CompletableFuture .completedFuture (runnable .run (tvid , title , connector , data ));
208- }
190+ return CompletableFuture .runAsync (() -> {
191+ try {
192+ runnable .run (tvid , title , connector , data );
193+ } catch (CoreException e ) {
194+ throw new CompletionException (e );
195+ }
196+ }, Display .getDefault ()::asyncExec );
197+
209198 } catch (RuntimeException e ) {
210199 return CompletableFuture .failedFuture (e );
211200 }
@@ -265,9 +254,10 @@ public CompletableFuture<?> openConsole(final Map<String, Object> properties) {
265254 final boolean restoringView = fRestoringView ;
266255 return executeServiceOperation (properties , new TerminalServiceRunnable () {
267256 @ Override
268- public IStatus run (TerminalViewId tvid , String title , ITerminalConnector connector , Object data ) {
257+ public void run (TerminalViewId tvid , String title , ITerminalConnector connector , Object data )
258+ throws CoreException {
269259 if (restoringView ) {
270- return doRun (tvid , title , connector , data );
260+ doRun (tvid , title , connector , data );
271261 } else {
272262 // First, restore the view. This opens consoles from the memento
273263 fRestoringView = true ;
@@ -277,11 +267,12 @@ public IStatus run(TerminalViewId tvid, String title, ITerminalConnector connect
277267 ILog .get ().log (e .getStatus ());
278268 }
279269 fRestoringView = false ;
280- return doRun (tvid , title , connector , data );
270+ doRun (tvid , title , connector , data );
281271 }
282272 }
283273
284- private IStatus doRun (TerminalViewId tvid , String title , ITerminalConnector connector , Object data ) {
274+ private void doRun (TerminalViewId tvid , String title , ITerminalConnector connector , Object data )
275+ throws CoreException {
285276 // Determine the terminal encoding
286277 String encoding = (String ) properties .get (ITerminalsConnectorConstants .PROP_ENCODING );
287278 // Create the flags to pass on to openConsole
@@ -302,16 +293,11 @@ private IStatus doRun(TerminalViewId tvid, String title, ITerminalConnector conn
302293 flags .put (ITerminalsConnectorConstants .PROP_TITLE_DISABLE_ANSI_TITLE , false );
303294 }
304295 // Open the new console
305- try {
306- Widget console = consoleViewManager .openConsole (tvid , title , encoding , connector , data , flags );
307- // Associate the original terminal properties with the tab item.
308- // This makes it easier to persist the connection data within the memento handler
309- if (!console .isDisposed ()) {
310- console .setData ("properties" , properties ); //$NON-NLS-1$
311- }
312- return Status .OK_STATUS ;
313- } catch (CoreException e ) {
314- return e .getStatus ();
296+ Widget console = consoleViewManager .openConsole (tvid , title , encoding , connector , data , flags );
297+ // Associate the original terminal properties with the tab item.
298+ // This makes it easier to persist the connection data within the memento handler
299+ if (!console .isDisposed ()) {
300+ console .setData ("properties" , properties ); //$NON-NLS-1$
315301 }
316302 }
317303 });
@@ -320,25 +306,15 @@ private IStatus doRun(TerminalViewId tvid, String title, ITerminalConnector conn
320306 @ Override
321307 public CompletableFuture <?> closeConsole (Map <String , Object > properties ) {
322308 Assert .isNotNull (properties );
323- return executeServiceOperation (properties , new TerminalServiceRunnable () {
324- @ Override
325- public IStatus run (TerminalViewId tvid , String title , ITerminalConnector connector , Object data ) {
326- consoleViewManager .closeConsole (tvid , title , connector , data );
327- return Status .OK_STATUS ;
328- }
329- });
309+ return executeServiceOperation (properties ,
310+ (tvid , title , connector , data ) -> consoleViewManager .closeConsole (tvid , title , connector , data ));
330311 }
331312
332313 @ Override
333314 public CompletableFuture <?> terminateConsole (Map <String , Object > properties ) {
334315 Assert .isNotNull (properties );
335316
336- return executeServiceOperation (properties , new TerminalServiceRunnable () {
337- @ Override
338- public IStatus run (TerminalViewId tvid , String title , ITerminalConnector connector , Object data ) {
339- consoleViewManager .terminateConsole (tvid , title , connector , data );
340- return Status .OK_STATUS ;
341- }
342- });
317+ return executeServiceOperation (properties ,
318+ (tvid , title , connector , data ) -> consoleViewManager .terminateConsole (tvid , title , connector , data ));
343319 }
344320}
0 commit comments