21
21
import java .util .Optional ;
22
22
23
23
import org .eclipse .core .runtime .Assert ;
24
- import org .eclipse .core .runtime .IStatus ;
24
+ import org .eclipse .core .runtime .CoreException ;
25
25
import org .eclipse .core .runtime .Status ;
26
26
import org .eclipse .swt .custom .CTabFolder ;
27
27
import org .eclipse .swt .custom .CTabItem ;
@@ -183,21 +183,7 @@ private final IWorkbenchPage getActiveWorkbenchPage() {
183
183
@ Override
184
184
public Optional <ITerminalsView > findConsoleView (TerminalViewId tvid ) {
185
185
Assert .isNotNull (Display .findDisplay (Thread .currentThread ()));
186
-
187
- ITerminalsView view = null ;
188
-
189
- // Get the active workbench page
190
- IWorkbenchPage page = getActiveWorkbenchPage ();
191
- if (page != null ) {
192
- // Look for the view
193
- IViewPart part = getTerminalsViewWithSecondaryId (tvid , true );
194
- // Check the interface
195
- if (part instanceof ITerminalsView ) {
196
- view = (ITerminalsView ) part ;
197
- }
198
- }
199
-
200
- return Optional .ofNullable (view );
186
+ return findTerminalsViewWithSecondaryId (tvid , true );
201
187
}
202
188
203
189
/**
@@ -209,18 +195,23 @@ public Optional<ITerminalsView> findConsoleView(TerminalViewId tvid) {
209
195
*
210
196
* @return The terminals console view instance or <code>null</code> if not found.
211
197
*/
212
- private IViewPart getTerminalsViewWithSecondaryId (TerminalViewId tvid , boolean restore ) {
213
- for (IViewReference ref : getActiveWorkbenchPage ().getViewReferences ()) {
198
+ private Optional <ITerminalsView > findTerminalsViewWithSecondaryId (TerminalViewId tvid , boolean restore ) {
199
+ IWorkbenchPage page = getActiveWorkbenchPage ();
200
+ if (page == null ) {
201
+ return Optional .empty ();
202
+ }
203
+ for (IViewReference ref : page .getViewReferences ()) {
214
204
if (ref .getId ().equals (tvid .primary ())) {
215
205
String refSecondaryId = ref .getSecondaryId ();
216
206
String secondaryId = tvid .secondary ().orElse (null );
217
207
if (ITerminalsConnectorConstants .ANY_ACTIVE_SECONDARY_ID .equals (secondaryId )
218
208
|| Objects .equals (secondaryId , refSecondaryId )) {
219
- return ref .getView (restore );
209
+ return Optional .ofNullable (ref .getView (restore )).filter (ITerminalsView .class ::isInstance )
210
+ .map (ITerminalsView .class ::cast );
220
211
}
221
212
}
222
213
}
223
- return null ;
214
+ return Optional . empty () ;
224
215
}
225
216
226
217
/**
@@ -230,34 +221,33 @@ private IViewPart getTerminalsViewWithSecondaryId(TerminalViewId tvid, boolean r
230
221
* {@link ITerminalsConnectorConstants#LAST_ACTIVE_SECONDARY_ID} for its secondary part.
231
222
* @return The terminals console view instance or <code>null</code> if not found.
232
223
*/
233
- private IViewPart getActiveTerminalsView (TerminalViewId tvid ) {
234
- IViewPart part = null ;
224
+ private Optional < ITerminalsView > findActiveTerminalsView (TerminalViewId tvid ) {
225
+ Optional < ITerminalsView > part = Optional . empty () ;
235
226
String id = tvid .primary ();
236
-
237
227
String secondaryId = tvid .secondary ().orElse (null );
238
228
if (id .equals (lastActiveViewId )) {
239
229
if (ITerminalsConnectorConstants .LAST_ACTIVE_SECONDARY_ID .equals (secondaryId )
240
230
|| Objects .equals (secondaryId , lastActiveSecondaryViewId )) {
241
- part = getTerminalsViewWithSecondaryId (new TerminalViewId (lastActiveViewId , lastActiveSecondaryViewId ),
231
+ part = findTerminalsViewWithSecondaryId (new TerminalViewId (lastActiveViewId , lastActiveSecondaryViewId ),
242
232
false );
243
233
}
244
234
}
245
235
246
- if (part == null ) {
236
+ if (part . isEmpty () ) {
247
237
String finalSecondaryId ;
248
238
if (ITerminalsConnectorConstants .LAST_ACTIVE_SECONDARY_ID .equals (secondaryId )) {
249
239
// There is no last available, so get any available instead
250
240
finalSecondaryId = ITerminalsConnectorConstants .ANY_ACTIVE_SECONDARY_ID ;
251
241
} else {
252
242
finalSecondaryId = secondaryId ;
253
243
}
254
- part = getTerminalsViewWithSecondaryId (new TerminalViewId (id , finalSecondaryId ), true );
255
- if (part != null ) {
256
- lastActiveViewId = part .getViewSite ().getId ();
257
- lastActiveSecondaryViewId = part .getViewSite ().getSecondaryId ();
244
+ part = findTerminalsViewWithSecondaryId (new TerminalViewId (id , finalSecondaryId ), true );
245
+ if (part .isPresent ()) {
246
+ IViewSite site = part .get ().getViewSite ();
247
+ lastActiveViewId = site .getId ();
248
+ lastActiveSecondaryViewId = site .getSecondaryId ();
258
249
}
259
250
}
260
-
261
251
return part ;
262
252
}
263
253
@@ -267,69 +257,72 @@ private IViewPart getActiveTerminalsView(TerminalViewId tvid) {
267
257
* <b>Note:</b> The method must be called within the UI thread.
268
258
*
269
259
* @param tvid The terminals console view id.
260
+ * @throws CoreException if the requested console cannot be opened
261
+ * @return opened terminal console view part
270
262
*/
271
263
@ Override
272
- public IViewPart showConsoleView (TerminalViewId tvid ) {
264
+ public ITerminalsView showConsoleView (TerminalViewId tvid ) throws CoreException {
273
265
Assert .isNotNull (Display .findDisplay (Thread .currentThread ()));
274
-
275
- // Get the active workbench page
276
266
IWorkbenchPage page = getActiveWorkbenchPage ();
277
- if (page != null ) {
278
- try {
279
- // show the view
280
- IViewPart part = getActiveTerminalsView (tvid );
281
- if (part == null ) {
282
- String secondaryId = tvid .secondary ().orElse (null );
283
- String finalSecondaryId ;
284
- if (ITerminalsConnectorConstants .LAST_ACTIVE_SECONDARY_ID .equals (secondaryId )
285
- || ITerminalsConnectorConstants .ANY_ACTIVE_SECONDARY_ID .equals (secondaryId )) {
286
- // We have already checked all open views, so since none of the special flags work
287
- // we are opening the first view, which means no secondary id.
288
- finalSecondaryId = null ;
289
- } else {
290
- finalSecondaryId = secondaryId ;
291
- }
292
- part = page .showView (tvid .primary (), finalSecondaryId , IWorkbenchPage .VIEW_ACTIVATE );
293
- }
294
- // and force the view to the foreground
295
- page .bringToTop (part );
296
- return part ;
297
- } catch (PartInitException e ) {
298
- IStatus status = new Status (IStatus .ERROR , UIPlugin .getUniqueIdentifier (), e .getLocalizedMessage (), e );
299
- UIPlugin .getDefault ().getLog ().log (status );
267
+ if (page == null ) {
268
+ throw noActivePage ();
269
+ }
270
+ // show the view
271
+ Optional <ITerminalsView > found = findActiveTerminalsView (tvid );
272
+ if (found .isEmpty ()) {
273
+ String secondaryId = tvid .secondary ().orElse (null );
274
+ String finalSecondaryId ;
275
+ if (ITerminalsConnectorConstants .LAST_ACTIVE_SECONDARY_ID .equals (secondaryId )
276
+ || ITerminalsConnectorConstants .ANY_ACTIVE_SECONDARY_ID .equals (secondaryId )) {
277
+ // We have already checked all open views, so since none of the special flags work
278
+ // we are opening the first view, which means no secondary id.
279
+ finalSecondaryId = null ;
280
+ } else {
281
+ finalSecondaryId = secondaryId ;
300
282
}
283
+ found = Optional .of (page .showView (tvid .primary (), finalSecondaryId , IWorkbenchPage .VIEW_ACTIVATE ))
284
+ .filter (ITerminalsView .class ::isInstance ).map (ITerminalsView .class ::cast );
301
285
}
302
- return null ;
286
+ // and force the view to the foreground
287
+ found .ifPresent (page ::bringToTop );
288
+ return found .orElseThrow (this ::cannotCreateConsole );
289
+ }
290
+
291
+ private CoreException noActivePage () {
292
+ return new CoreException (Status .error (Messages .ConsoleManager_e_no_active_page ));
293
+ }
294
+
295
+ private CoreException cannotCreateConsole () {
296
+ return new CoreException (Status .error (Messages .ConsoleManager_e_cannot_create_console ));
303
297
}
304
298
305
299
/**
306
300
* Bring the terminals console view, specified by the given id, to the top of the view stack.
307
301
*
308
302
* @param tvid The terminals console view id.
309
303
* @param activate If <code>true</code> activate the console view.
304
+ * @throws CoreException if the requested console cannot be opened
305
+ * @return opened terminal console view part
310
306
*/
311
- private IViewPart bringToTop (TerminalViewId tvid , boolean activate ) {
307
+ private ITerminalsView bringToTop (TerminalViewId tvid , boolean activate ) throws CoreException {
312
308
// Get the active workbench page
313
309
IWorkbenchPage page = getActiveWorkbenchPage ();
314
- if (page != null ) {
315
- // get (last) active terminal view
316
- IViewPart activePart = getActiveTerminalsView (tvid );
317
- if (activePart == null ) {
318
- // Create a new one
319
- IViewPart newPart = showConsoleView (
320
- new TerminalViewId (tvid .primary (), new TerminalViewId ().next ().secondary ()));
321
- return newPart ;
322
- }
323
-
324
- if (activate ) {
325
- page .activate (activePart );
326
- } else {
327
- page .bringToTop (activePart );
328
- }
329
-
330
- return activePart ;
310
+ if (page == null ) {
311
+ throw noActivePage ();
331
312
}
332
- return null ;
313
+ // get (last) active terminal view
314
+ Optional <ITerminalsView > found = findActiveTerminalsView (tvid );
315
+ if (found .isEmpty ()) {
316
+ // Create a new one
317
+ return showConsoleView (new TerminalViewId (tvid .primary (), new TerminalViewId ().next ().secondary ()));
318
+ }
319
+ ITerminalsView tv = found .get ();
320
+ if (activate ) {
321
+ page .activate (tv );
322
+ } else {
323
+ page .bringToTop (tv );
324
+ }
325
+ return tv ;
333
326
}
334
327
335
328
/**
@@ -344,10 +337,12 @@ private IViewPart bringToTop(TerminalViewId tvid, boolean activate) {
344
337
* @param connector The terminal connector. Must not be <code>null</code>.
345
338
* @param data The custom terminal data node or <code>null</code>.
346
339
* @param flags The flags controlling how the console is opened or <code>null</code> to use defaults.
340
+ * @throws CoreException if the requested console cannot be opened
341
+ * @return opened terminal console widget
347
342
*/
348
343
@ Override
349
- public CTabItem openConsole (TerminalViewId tvid , String title , String encoding , ITerminalConnector connector ,
350
- Object data , Map <String , Boolean > flags ) {
344
+ public Widget openConsole (TerminalViewId tvid , String title , String encoding , ITerminalConnector connector ,
345
+ Object data , Map <String , Boolean > flags ) throws CoreException {
351
346
Assert .isNotNull (title );
352
347
Assert .isNotNull (connector );
353
348
Assert .isNotNull (Display .findDisplay (Thread .currentThread ()));
@@ -358,23 +353,18 @@ public CTabItem openConsole(TerminalViewId tvid, String title, String encoding,
358
353
boolean forceNew = flags != null && flags .containsKey (ITerminalsConnectorConstants .PROP_FORCE_NEW )
359
354
? flags .get (ITerminalsConnectorConstants .PROP_FORCE_NEW ).booleanValue ()
360
355
: false ;
361
-
362
356
// Make the consoles view visible
363
- IViewPart part = bringToTop (tvid , activate );
364
- if (!(part instanceof ITerminalsView view )) {
365
- return null ;
366
- }
357
+ ITerminalsView view = bringToTop (tvid , activate );
367
358
// Cast to the correct type
368
359
// Get the tab folder manager associated with the view
369
360
TabFolderManager manager = view .getAdapter (TabFolderManager .class );
370
361
if (manager == null ) {
371
- return null ;
362
+ throw cannotCreateConsole () ;
372
363
}
373
364
374
365
// 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 );
366
+ String secId = ((IViewSite ) view .getSite ()).getSecondaryId ();
367
+ Optional <Widget > item = findConsole (new TerminalViewId (tvid .primary (), secId ), title , connector , data );
378
368
379
369
// Switch to the tab folder page _before_ calling TabFolderManager#createItem(...).
380
370
// The createItem(...) method invokes the corresponding connect and this may take
@@ -383,7 +373,7 @@ public CTabItem openConsole(TerminalViewId tvid, String title, String encoding,
383
373
view .switchToTabFolderControl ();
384
374
385
375
// If no existing console exist or forced -> Create the tab item
386
- if (item == null || forceNew ) {
376
+ if (item . isEmpty () || forceNew ) {
387
377
// If configured, check all existing tab items if they are associated
388
378
// with terminated consoles
389
379
if (UIPlugin .getScopedPreferences ().getBoolean (IPreferenceKeys .PREF_REMOVE_TERMINATED_TERMINALS )) {
@@ -396,21 +386,19 @@ public CTabItem openConsole(TerminalViewId tvid, String title, String encoding,
396
386
}
397
387
398
388
// Create a new tab item
399
- item = manager .createTabItem (title , encoding , connector , data , flags );
400
- }
401
- // If still null, something went wrong
402
- if (item == null ) {
403
- return null ;
389
+ item = Optional .ofNullable (manager .createTabItem (title , encoding , connector , data , flags ));
404
390
}
405
-
391
+ CTabItem tab = toTabItem ( item );
406
392
// Make the item the active console
407
- manager .bringToTop (item );
408
-
393
+ manager .bringToTop (tab );
409
394
// Make sure the terminals view has the focus after opening a new terminal
410
395
view .setFocus ();
411
-
412
396
// Return the tab item of the opened console
413
- return item ;
397
+ return tab ;
398
+ }
399
+
400
+ private CTabItem toTabItem (Optional <Widget > item ) throws CoreException {
401
+ return item .filter (CTabItem .class ::isInstance ).map (CTabItem .class ::cast ).orElseThrow (this ::cannotCreateConsole );
414
402
}
415
403
416
404
/**
0 commit comments