Skip to content

Commit a83024c

Browse files
committed
Continue #2027: return Optional instead of null
* see #2027 (comment) * see #2027 (comment)
1 parent d4985f2 commit a83024c

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/ConsoleManager.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/TerminalService.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.HashMap;
1616
import java.util.Map;
1717
import java.util.Optional;
18+
import java.util.function.Predicate;
1819

1920
import org.eclipse.core.runtime.Assert;
2021
import org.eclipse.core.runtime.ISafeRunnable;
@@ -299,13 +300,10 @@ public void doRun(TerminalViewId tvid, String title, ITerminalConnector connecto
299300
flags.put(ITerminalsConnectorConstants.PROP_TITLE_DISABLE_ANSI_TITLE, false);
300301
}
301302
// Open the new console
302-
Widget item = consoleViewManager.openConsole(tvid, title, encoding, connector, data, flags);
303303
// Associate the original terminal properties with the tab item.
304304
// This makes it easier to persist the connection data within the memento handler
305-
if (item != null && !item.isDisposed()) {
306-
item.setData("properties", properties); //$NON-NLS-1$
307-
}
308-
305+
consoleViewManager.openConsole(tvid, title, encoding, connector, data, flags)
306+
.filter(Predicate.not(Widget::isDisposed)).ifPresent(w -> w.setData("properties", properties)); //$NON-NLS-1$
309307
// Invoke the callback
310308
if (done != null) {
311309
done.done(Status.OK_STATUS);

terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/launcher/ITerminalConsoleViewManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public interface ITerminalConsoleViewManager {
4646
*
4747
* @param tvid The terminals console view id.
4848
*/
49-
IViewPart showConsoleView(TerminalViewId tvid);
49+
Optional<IViewPart> showConsoleView(TerminalViewId tvid);
5050

5151
/**
5252
* Opens the console with the given title and connector.
@@ -61,8 +61,8 @@ public interface ITerminalConsoleViewManager {
6161
* @param data The custom terminal data node or <code>null</code>.
6262
* @param flags The flags controlling how the console is opened or <code>null</code> to use defaults.
6363
*/
64-
Widget openConsole(TerminalViewId tvid, String title, String encoding, ITerminalConnector connector, Object data,
65-
Map<String, Boolean> flags);
64+
Optional<Widget> openConsole(TerminalViewId tvid, String title, String encoding, ITerminalConnector connector,
65+
Object data, Map<String, Boolean> flags);
6666

6767
/**
6868
* Lookup a console with the given title and the given terminal connector.

0 commit comments

Comments
 (0)