Skip to content

Commit 5a019ec

Browse files
committed
Continue #2027 (and #2035): use Stream for ILaunchDelegateManager
* see #2035 (comment)
1 parent cd13212 commit 5a019ec

File tree

6 files changed

+62
-88
lines changed

6 files changed

+62
-88
lines changed

terminal/bundles/org.eclipse.terminal.connector.local/src/org/eclipse/terminal/connector/local/launcher/LocalLauncherHandler.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
package org.eclipse.terminal.connector.local.launcher;
1414

1515
import java.util.HashMap;
16-
import java.util.List;
1716
import java.util.Map;
1817

1918
import org.eclipse.core.commands.AbstractHandler;
@@ -37,9 +36,15 @@ public class LocalLauncherHandler extends AbstractHandler {
3736

3837
@Override
3938
public Object execute(ExecutionEvent event) throws ExecutionException {
40-
// Get the current selection
41-
ISelection selection = HandlerUtil.getCurrentSelection(event);
39+
ISelection selection = selection(event);
40+
UIPlugin.getLaunchDelegateManager().getApplicableLauncherDelegates(selection)
41+
.filter(d -> "org.eclipse.terminal.connector.local.launcher.local".equals(d.getId())).findFirst() //$NON-NLS-1$
42+
.ifPresent(d -> executeDelegate(selection, d));
43+
return null;
44+
}
4245

46+
private ISelection selection(ExecutionEvent event) {
47+
ISelection selection = HandlerUtil.getCurrentSelection(event);
4348
// If the selection is not a structured selection, check if there is an active
4449
// editor and get the path from the editor input
4550
if (!(selection instanceof IStructuredSelection)) {
@@ -56,29 +61,14 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
5661
}
5762
}
5863
}
64+
return selection;
65+
}
5966

60-
// Get all applicable launcher delegates for the current selection
61-
List<ILauncherDelegate> delegates = UIPlugin.getLaunchDelegateManager()
62-
.getApplicableLauncherDelegates(selection);
63-
// Find the local terminal launcher delegate
64-
ILauncherDelegate delegate = null;
65-
for (ILauncherDelegate candidate : delegates) {
66-
if ("org.eclipse.terminal.connector.local.launcher.local".equals(candidate.getId())) { //$NON-NLS-1$
67-
delegate = candidate;
68-
break;
69-
}
70-
}
71-
72-
// Launch the local terminal
73-
if (delegate != null) {
74-
Map<String, Object> properties = new HashMap<>();
75-
properties.put(ITerminalsConnectorConstants.PROP_DELEGATE_ID, delegate.getId());
76-
properties.put(ITerminalsConnectorConstants.PROP_SELECTION, selection);
77-
78-
delegate.execute(properties, null);
79-
}
80-
81-
return null;
67+
private void executeDelegate(ISelection selection, ILauncherDelegate delegate) {
68+
Map<String, Object> properties = new HashMap<>();
69+
properties.put(ITerminalsConnectorConstants.PROP_DELEGATE_ID, delegate.getId());
70+
properties.put(ITerminalsConnectorConstants.PROP_SELECTION, selection);
71+
delegate.execute(properties, null);
8272
}
8373

8474
}

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

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import java.util.HashMap;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.Objects;
2223
import java.util.Optional;
24+
import java.util.stream.Stream;
2325

2426
import org.eclipse.core.expressions.EvaluationContext;
2527
import org.eclipse.core.expressions.EvaluationResult;
@@ -31,6 +33,7 @@
3133
import org.eclipse.core.runtime.IExtension;
3234
import org.eclipse.core.runtime.IExtensionPoint;
3335
import org.eclipse.core.runtime.IExtensionRegistry;
36+
import org.eclipse.core.runtime.ILog;
3437
import org.eclipse.core.runtime.IStatus;
3538
import org.eclipse.core.runtime.Platform;
3639
import org.eclipse.core.runtime.Status;
@@ -224,23 +227,17 @@ public int compare(IExtension o1, IExtension o2) {
224227
}
225228

226229
/**
227-
* Returns the list of all contributed terminal launcher delegates.
230+
* Returns the stream of all contributed terminal launcher delegates.
228231
*
229232
* @param unique If <code>true</code>, the method returns new instances for each
230233
* contributed terminal launcher delegate.
231234
*
232-
* @return The list of contributed terminal launcher delegates, or an empty list.
235+
* @return The stream of contributed terminal launcher delegates
233236
*/
234237
@Override
235-
public List<ILauncherDelegate> getLauncherDelegates(boolean unique) {
236-
List<ILauncherDelegate> contributions = new ArrayList<>();
237-
for (Proxy launcherDelegate : getExtensions().values()) {
238-
ILauncherDelegate instance = unique ? launcherDelegate.newInstance() : launcherDelegate.getInstance();
239-
if (instance != null && !contributions.contains(instance)) {
240-
contributions.add(instance);
241-
}
242-
}
243-
return contributions;
238+
public Stream<ILauncherDelegate> getLauncherDelegates(boolean unique) {
239+
return getExtensions().values().stream().map(proxy -> unique ? proxy.newInstance() : proxy.getInstance())
240+
.filter(Objects::nonNull).distinct();
244241
}
245242

246243
/**
@@ -268,51 +265,37 @@ public Optional<ILauncherDelegate> findLauncherDelegate(String id, boolean uniqu
268265
* Returns the applicable terminal launcher delegates for the given selection.
269266
*
270267
* @param selection The selection or <code>null</code>.
271-
* @return The list of applicable terminal launcher delegates or an empty list.
268+
* @return The stream of applicable terminal launcher delegates.
272269
*/
273270
@Override
274-
public List<ILauncherDelegate> getApplicableLauncherDelegates(ISelection selection) {
275-
List<ILauncherDelegate> applicable = new ArrayList<>();
276-
277-
for (ILauncherDelegate delegate : getLauncherDelegates(false)) {
278-
Expression enablement = delegate.getEnablement();
279-
280-
// The launcher delegate is applicable by default if
281-
// no expression is specified.
282-
boolean isApplicable = enablement == null;
283-
284-
if (enablement != null) {
285-
if (selection != null) {
286-
// Set the default variable to selection.
287-
IEvaluationContext currentState = PlatformUI.getWorkbench().getService(IHandlerService.class)
288-
.getCurrentState();
289-
EvaluationContext context = new EvaluationContext(currentState, selection);
290-
// Set the "selection" variable to the selection.
291-
context.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
292-
// Allow plug-in activation
293-
context.setAllowPluginActivation(true);
294-
// Evaluate the expression
295-
try {
296-
isApplicable = enablement.evaluate(context).equals(EvaluationResult.TRUE);
297-
} catch (CoreException e) {
298-
IStatus status = new Status(IStatus.ERROR, UIPlugin.getUniqueIdentifier(),
299-
e.getLocalizedMessage(), e);
300-
UIPlugin.getDefault().getLog().log(status);
301-
}
302-
} else {
303-
// The enablement is false by definition if
304-
// there is no selection.
305-
isApplicable = false;
306-
}
307-
}
271+
public Stream<ILauncherDelegate> getApplicableLauncherDelegates(ISelection selection) {
272+
return getLauncherDelegates(false).filter(d -> isApplicable(selection, d));
273+
}
308274

309-
// Add the page if applicable
310-
if (isApplicable) {
311-
applicable.add(delegate);
312-
}
275+
private boolean isApplicable(ISelection selection, ILauncherDelegate delegate) {
276+
Expression enablement = delegate.getEnablement();
277+
if (enablement == null) {
278+
// The launcher delegate is applicable by default if no expression is specified.
279+
return true;
280+
}
281+
if (selection == null) {
282+
// The enablement is false by definition if there is no selection.
283+
return false;
284+
}
285+
// Set the default variable to selection.
286+
IEvaluationContext currentState = PlatformUI.getWorkbench().getService(IHandlerService.class).getCurrentState();
287+
EvaluationContext context = new EvaluationContext(currentState, selection);
288+
// Set the "selection" variable to the selection.
289+
context.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
290+
// Allow plug-in activation
291+
context.setAllowPluginActivation(true);
292+
// Evaluate the expression
293+
try {
294+
return enablement.evaluate(context).equals(EvaluationResult.TRUE);
295+
} catch (CoreException e) {
296+
ILog.get().log(e.getStatus());
297+
return false;
313298
}
314-
315-
return applicable;
316299
}
317300

318301
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public boolean test(Object receiver, String property, Object[] args, Object expe
3030

3131
if ("hasApplicableLauncherDelegates".equals(property)) { //$NON-NLS-1$
3232
ISelection selection = receiver instanceof ISelection i ? i : new StructuredSelection(receiver);
33-
return expectedValue.equals(Boolean
34-
.valueOf(!UIPlugin.getLaunchDelegateManager().getApplicableLauncherDelegates(selection).isEmpty()));
33+
return expectedValue.equals(Boolean.valueOf(UIPlugin.getLaunchDelegateManager()
34+
.getApplicableLauncherDelegates(selection).findAny().isPresent()));
3535
}
3636

3737
if ("canDisconnect".equals(property) && receiver instanceof ITerminalsView) { //$NON-NLS-1$

terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/dialogs/LaunchTerminalSettingsDialog.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ protected List<String> getTerminals() {
421421
ITraceIds.TRACE_LAUNCH_TERMINAL_COMMAND_HANDLER, LaunchTerminalSettingsDialog.this);
422422
}
423423

424-
List<ILauncherDelegate> delegates = UIPlugin.getLaunchDelegateManager().getLauncherDelegates(false);
424+
List<ILauncherDelegate> delegates = UIPlugin.getLaunchDelegateManager().getLauncherDelegates(false)
425+
.toList();
425426

426427
if (UIPlugin.getTraceHandler().isSlotEnabled(0, ITraceIds.TRACE_LAUNCH_TERMINAL_COMMAND_HANDLER)) {
427428
UIPlugin.getTraceHandler().trace(
@@ -452,7 +453,7 @@ protected List<String> getTerminals() {
452453
}
453454

454455
List<ILauncherDelegate> delegates = UIPlugin.getLaunchDelegateManager()
455-
.getApplicableLauncherDelegates(selection);
456+
.getApplicableLauncherDelegates(selection).toList();
456457

457458
if (UIPlugin.getTraceHandler().isSlotEnabled(0, ITraceIds.TRACE_LAUNCH_TERMINAL_COMMAND_HANDLER)) {
458459
UIPlugin.getTraceHandler().trace(

terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/handler/LaunchTerminalCommandHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
8989

9090
// Check if the dialog needs to be shown at all
9191
List<ILauncherDelegate> delegates = UIPlugin.getLaunchDelegateManager()
92-
.getApplicableLauncherDelegates(selection);
92+
.getApplicableLauncherDelegates(selection).toList();
9393

9494
if (UIPlugin.getTraceHandler().isSlotEnabled(0, ITraceIds.TRACE_LAUNCH_TERMINAL_COMMAND_HANDLER)) {
9595
UIPlugin.getTraceHandler().trace(

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@
1313
*******************************************************************************/
1414
package org.eclipse.terminal.view.ui.launcher;
1515

16-
import java.util.List;
1716
import java.util.Optional;
17+
import java.util.stream.Stream;
1818

1919
import org.eclipse.jface.viewers.ISelection;
2020

2121
public interface ILaunchDelegateManager {
2222

2323
/**
24-
* Returns the list of all contributed terminal launcher delegates.
24+
* Returns all contributed terminal launcher delegates.
2525
*
2626
* @param unique If <code>true</code>, the method returns new instances for each
2727
* contributed terminal launcher delegate.
2828
*
29-
* @return The list of contributed terminal launcher delegates, or an empty list.
29+
* @return The stream of contributed terminal launcher delegates
3030
*/
31-
List<ILauncherDelegate> getLauncherDelegates(boolean unique);
31+
Stream<ILauncherDelegate> getLauncherDelegates(boolean unique);
3232

3333
/**
3434
* Lookup a terminal launcher delegate identified by its unique id.
@@ -44,8 +44,8 @@ public interface ILaunchDelegateManager {
4444
* Returns the applicable terminal launcher delegates for the given selection.
4545
*
4646
* @param selection The selection or <code>null</code>.
47-
* @return The list of applicable terminal launcher delegates or an empty list.
47+
* @return The stream of applicable terminal launcher delegates.
4848
*/
49-
List<ILauncherDelegate> getApplicableLauncherDelegates(ISelection selection);
49+
Stream<ILauncherDelegate> getApplicableLauncherDelegates(ISelection selection);
5050

5151
}

0 commit comments

Comments
 (0)