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,10 @@ 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+ void run (TerminalViewId tvid , String title , ITerminalConnector connector , Object data ) throws CoreException ;
9688 }
9789
9890 @ Activate
@@ -194,18 +186,14 @@ protected final CompletableFuture<?> executeServiceOperation(final Map<String, O
194186 private CompletableFuture <?> executeServiceOperation (final TerminalServiceRunnable runnable , TerminalViewId tvid ,
195187 final String title , final ITerminalConnector connector , final Object data ) {
196188 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- }
189+ return CompletableFuture .runAsync (() -> {
190+ try {
191+ runnable .run (tvid , title , connector , data );
192+ } catch (CoreException e ) {
193+ throw new CompletionException (e );
194+ }
195+ }, Display .getDefault ()::asyncExec );
196+
209197 } catch (RuntimeException e ) {
210198 return CompletableFuture .failedFuture (e );
211199 }
@@ -265,9 +253,10 @@ public CompletableFuture<?> openConsole(final Map<String, Object> properties) {
265253 final boolean restoringView = fRestoringView ;
266254 return executeServiceOperation (properties , new TerminalServiceRunnable () {
267255 @ Override
268- public IStatus run (TerminalViewId tvid , String title , ITerminalConnector connector , Object data ) {
256+ public void run (TerminalViewId tvid , String title , ITerminalConnector connector , Object data )
257+ throws CoreException {
269258 if (restoringView ) {
270- return doRun (tvid , title , connector , data );
259+ doRun (tvid , title , connector , data );
271260 } else {
272261 // First, restore the view. This opens consoles from the memento
273262 fRestoringView = true ;
@@ -277,11 +266,12 @@ public IStatus run(TerminalViewId tvid, String title, ITerminalConnector connect
277266 ILog .get ().log (e .getStatus ());
278267 }
279268 fRestoringView = false ;
280- return doRun (tvid , title , connector , data );
269+ doRun (tvid , title , connector , data );
281270 }
282271 }
283272
284- private IStatus doRun (TerminalViewId tvid , String title , ITerminalConnector connector , Object data ) {
273+ private void doRun (TerminalViewId tvid , String title , ITerminalConnector connector , Object data )
274+ throws CoreException {
285275 // Determine the terminal encoding
286276 String encoding = (String ) properties .get (ITerminalsConnectorConstants .PROP_ENCODING );
287277 // Create the flags to pass on to openConsole
@@ -302,16 +292,11 @@ private IStatus doRun(TerminalViewId tvid, String title, ITerminalConnector conn
302292 flags .put (ITerminalsConnectorConstants .PROP_TITLE_DISABLE_ANSI_TITLE , false );
303293 }
304294 // 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 ();
295+ Widget console = consoleViewManager .openConsole (tvid , title , encoding , connector , data , flags );
296+ // Associate the original terminal properties with the tab item.
297+ // This makes it easier to persist the connection data within the memento handler
298+ if (!console .isDisposed ()) {
299+ console .setData ("properties" , properties ); //$NON-NLS-1$
315300 }
316301 }
317302 });
@@ -320,25 +305,12 @@ private IStatus doRun(TerminalViewId tvid, String title, ITerminalConnector conn
320305 @ Override
321306 public CompletableFuture <?> closeConsole (Map <String , Object > properties ) {
322307 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- });
308+ return executeServiceOperation (properties , consoleViewManager ::closeConsole );
330309 }
331310
332311 @ Override
333312 public CompletableFuture <?> terminateConsole (Map <String , Object > properties ) {
334313 Assert .isNotNull (properties );
335-
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- });
314+ return executeServiceOperation (properties , consoleViewManager ::terminateConsole );
343315 }
344316}
0 commit comments