|
19 | 19 | import java.util.HashMap;
|
20 | 20 | import java.util.List;
|
21 | 21 | import java.util.Map;
|
| 22 | +import java.util.Objects; |
22 | 23 | import java.util.Optional;
|
| 24 | +import java.util.stream.Stream; |
23 | 25 |
|
24 | 26 | import org.eclipse.core.expressions.EvaluationContext;
|
25 | 27 | import org.eclipse.core.expressions.EvaluationResult;
|
|
31 | 33 | import org.eclipse.core.runtime.IExtension;
|
32 | 34 | import org.eclipse.core.runtime.IExtensionPoint;
|
33 | 35 | import org.eclipse.core.runtime.IExtensionRegistry;
|
| 36 | +import org.eclipse.core.runtime.ILog; |
34 | 37 | import org.eclipse.core.runtime.IStatus;
|
35 | 38 | import org.eclipse.core.runtime.Platform;
|
36 | 39 | import org.eclipse.core.runtime.Status;
|
@@ -224,23 +227,17 @@ public int compare(IExtension o1, IExtension o2) {
|
224 | 227 | }
|
225 | 228 |
|
226 | 229 | /**
|
227 |
| - * Returns the list of all contributed terminal launcher delegates. |
| 230 | + * Returns the stream of all contributed terminal launcher delegates. |
228 | 231 | *
|
229 | 232 | * @param unique If <code>true</code>, the method returns new instances for each
|
230 | 233 | * contributed terminal launcher delegate.
|
231 | 234 | *
|
232 |
| - * @return The list of contributed terminal launcher delegates, or an empty list. |
| 235 | + * @return The stream of contributed terminal launcher delegates |
233 | 236 | */
|
234 | 237 | @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(); |
244 | 241 | }
|
245 | 242 |
|
246 | 243 | /**
|
@@ -268,51 +265,37 @@ public Optional<ILauncherDelegate> findLauncherDelegate(String id, boolean uniqu
|
268 | 265 | * Returns the applicable terminal launcher delegates for the given selection.
|
269 | 266 | *
|
270 | 267 | * @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. |
272 | 269 | */
|
273 | 270 | @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 | + } |
308 | 274 |
|
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; |
313 | 298 | }
|
314 |
| - |
315 |
| - return applicable; |
316 | 299 | }
|
317 | 300 |
|
318 | 301 | /**
|
|
0 commit comments