@@ -269,7 +269,7 @@ private IViewPart getActiveTerminalsView(TerminalViewId tvid) {
269269 * @param tvid The terminals console view id.
270270 */
271271 @ Override
272- public IViewPart showConsoleView (TerminalViewId tvid ) {
272+ public Optional < IViewPart > showConsoleView (TerminalViewId tvid ) {
273273 Assert .isNotNull (Display .findDisplay (Thread .currentThread ()));
274274
275275 // Get the active workbench page
@@ -293,13 +293,13 @@ public IViewPart showConsoleView(TerminalViewId tvid) {
293293 }
294294 // and force the view to the foreground
295295 page .bringToTop (part );
296- return part ;
296+ return Optional . ofNullable ( part ) ;
297297 } catch (PartInitException e ) {
298298 IStatus status = new Status (IStatus .ERROR , UIPlugin .getUniqueIdentifier (), e .getLocalizedMessage (), e );
299299 UIPlugin .getDefault ().getLog ().log (status );
300300 }
301301 }
302- return null ;
302+ return Optional . empty () ;
303303 }
304304
305305 /**
@@ -308,17 +308,15 @@ public IViewPart showConsoleView(TerminalViewId tvid) {
308308 * @param tvid The terminals console view id.
309309 * @param activate If <code>true</code> activate the console view.
310310 */
311- private IViewPart bringToTop (TerminalViewId tvid , boolean activate ) {
311+ private Optional < IViewPart > bringToTop (TerminalViewId tvid , boolean activate ) {
312312 // Get the active workbench page
313313 IWorkbenchPage page = getActiveWorkbenchPage ();
314314 if (page != null ) {
315315 // get (last) active terminal view
316316 IViewPart activePart = getActiveTerminalsView (tvid );
317317 if (activePart == null ) {
318318 // Create a new one
319- IViewPart newPart = showConsoleView (
320- new TerminalViewId (tvid .primary (), new TerminalViewId ().next ().secondary ()));
321- return newPart ;
319+ return showConsoleView (new TerminalViewId (tvid .primary (), new TerminalViewId ().next ().secondary ()));
322320 }
323321
324322 if (activate ) {
@@ -327,7 +325,7 @@ private IViewPart bringToTop(TerminalViewId tvid, boolean activate) {
327325 page .bringToTop (activePart );
328326 }
329327
330- return activePart ;
328+ return Optional . of ( activePart ) ;
331329 }
332330 return null ;
333331 }
@@ -346,8 +344,8 @@ private IViewPart bringToTop(TerminalViewId tvid, boolean activate) {
346344 * @param flags The flags controlling how the console is opened or <code>null</code> to use defaults.
347345 */
348346 @ Override
349- public CTabItem openConsole (TerminalViewId tvid , String title , String encoding , ITerminalConnector connector ,
350- Object data , Map <String , Boolean > flags ) {
347+ public Optional < Widget > openConsole (TerminalViewId tvid , String title , String encoding ,
348+ ITerminalConnector connector , Object data , Map <String , Boolean > flags ) {
351349 Assert .isNotNull (title );
352350 Assert .isNotNull (connector );
353351 Assert .isNotNull (Display .findDisplay (Thread .currentThread ()));
@@ -360,21 +358,22 @@ public CTabItem openConsole(TerminalViewId tvid, String title, String encoding,
360358 : false ;
361359
362360 // Make the consoles view visible
363- IViewPart part = bringToTop (tvid , activate );
364- if (!(part instanceof ITerminalsView view )) {
365- return null ;
361+ Optional <ITerminalsView > part = bringToTop (tvid , activate ).filter (ITerminalsView .class ::isInstance )
362+ .map (ITerminalsView .class ::cast );
363+ if (part .isEmpty ()) {
364+ return Optional .empty ();
366365 }
366+ ITerminalsView view = part .get ();
367367 // Cast to the correct type
368368 // Get the tab folder manager associated with the view
369369 TabFolderManager manager = view .getAdapter (TabFolderManager .class );
370370 if (manager == null ) {
371- return null ;
371+ return Optional . empty () ;
372372 }
373373
374374 // Lookup an existing console first
375- String secId = ((IViewSite ) part .getSite ()).getSecondaryId ();
376- CTabItem item = (CTabItem ) findConsole (new TerminalViewId (tvid .primary (), secId ), title , connector , data )
377- .orElse (null );
375+ String secId = ((IViewSite ) view .getSite ()).getSecondaryId ();
376+ Optional <Widget > item = findConsole (new TerminalViewId (tvid .primary (), secId ), title , connector , data );
378377
379378 // Switch to the tab folder page _before_ calling TabFolderManager#createItem(...).
380379 // The createItem(...) method invokes the corresponding connect and this may take
@@ -383,7 +382,7 @@ public CTabItem openConsole(TerminalViewId tvid, String title, String encoding,
383382 view .switchToTabFolderControl ();
384383
385384 // If no existing console exist or forced -> Create the tab item
386- if (item == null || forceNew ) {
385+ if (item . isEmpty () || forceNew ) {
387386 // If configured, check all existing tab items if they are associated
388387 // with terminated consoles
389388 if (UIPlugin .getScopedPreferences ().getBoolean (IPreferenceKeys .PREF_REMOVE_TERMINATED_TERMINALS )) {
@@ -396,15 +395,15 @@ public CTabItem openConsole(TerminalViewId tvid, String title, String encoding,
396395 }
397396
398397 // Create a new tab item
399- item = manager .createTabItem (title , encoding , connector , data , flags );
398+ item = Optional . ofNullable ( manager .createTabItem (title , encoding , connector , data , flags ) );
400399 }
401400 // If still null, something went wrong
402- if (item == null ) {
403- return null ;
401+ if (item . isEmpty () ) {
402+ return Optional . empty () ;
404403 }
405404
406405 // Make the item the active console
407- manager . bringToTop ( item );
406+ item . filter ( CTabItem . class :: isInstance ). map ( CTabItem . class :: cast ). ifPresent ( manager :: bringToTop );
408407
409408 // Make sure the terminals view has the focus after opening a new terminal
410409 view .setFocus ();
0 commit comments