Skip to content

Commit 2d711eb

Browse files
committed
Continue eclipse-platform#2027 (and eclipse-platform#2035): use Stream for ILaunchDelegateManager
* see eclipse-platform#2035 (comment)
1 parent cd13212 commit 2d711eb

File tree

6 files changed

+60
-81
lines changed

6 files changed

+60
-81
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: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import java.util.Map;
2222
import java.util.Optional;
23+
import java.util.stream.Stream;
2324

2425
import org.eclipse.core.expressions.EvaluationContext;
2526
import org.eclipse.core.expressions.EvaluationResult;
@@ -31,6 +32,7 @@
3132
import org.eclipse.core.runtime.IExtension;
3233
import org.eclipse.core.runtime.IExtensionPoint;
3334
import org.eclipse.core.runtime.IExtensionRegistry;
35+
import org.eclipse.core.runtime.ILog;
3436
import org.eclipse.core.runtime.IStatus;
3537
import org.eclipse.core.runtime.Platform;
3638
import org.eclipse.core.runtime.Status;
@@ -224,23 +226,23 @@ public int compare(IExtension o1, IExtension o2) {
224226
}
225227

226228
/**
227-
* Returns the list of all contributed terminal launcher delegates.
229+
* Returns the stream of all contributed terminal launcher delegates.
228230
*
229231
* @param unique If <code>true</code>, the method returns new instances for each
230232
* contributed terminal launcher delegate.
231233
*
232-
* @return The list of contributed terminal launcher delegates, or an empty list.
234+
* @return The stream of contributed terminal launcher delegates
233235
*/
234236
@Override
235-
public List<ILauncherDelegate> getLauncherDelegates(boolean unique) {
237+
public Stream<ILauncherDelegate> getLauncherDelegates(boolean unique) {
236238
List<ILauncherDelegate> contributions = new ArrayList<>();
237239
for (Proxy launcherDelegate : getExtensions().values()) {
238240
ILauncherDelegate instance = unique ? launcherDelegate.newInstance() : launcherDelegate.getInstance();
239241
if (instance != null && !contributions.contains(instance)) {
240242
contributions.add(instance);
241243
}
242244
}
243-
return contributions;
245+
return contributions.stream();
244246
}
245247

246248
/**
@@ -268,51 +270,37 @@ public Optional<ILauncherDelegate> findLauncherDelegate(String id, boolean uniqu
268270
* Returns the applicable terminal launcher delegates for the given selection.
269271
*
270272
* @param selection The selection or <code>null</code>.
271-
* @return The list of applicable terminal launcher delegates or an empty list.
273+
* @return The stream of applicable terminal launcher delegates.
272274
*/
273275
@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-
}
276+
public Stream<ILauncherDelegate> getApplicableLauncherDelegates(ISelection selection) {
277+
return getLauncherDelegates(false).filter(d -> isApplicable(selection, d));
278+
}
308279

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

318306
/**

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)