|
33 | 33 | import java.util.Map; |
34 | 34 | import java.util.Objects; |
35 | 35 | import java.util.Set; |
36 | | -import java.util.concurrent.ConcurrentLinkedQueue; |
37 | 36 | import java.util.concurrent.atomic.AtomicReference; |
38 | 37 | import java.util.stream.Collectors; |
39 | 38 | import java.util.stream.IntStream; |
40 | 39 | import java.util.stream.Stream; |
41 | 40 |
|
42 | 41 | import org.eclipse.core.resources.IFile; |
43 | 42 | import org.eclipse.core.resources.IResource; |
44 | | -import org.eclipse.core.resources.IResourceProxy; |
45 | | -import org.eclipse.core.resources.IResourceProxyVisitor; |
46 | 43 | import org.eclipse.core.resources.ResourcesPlugin; |
47 | | -import org.eclipse.core.runtime.Assert; |
48 | 44 | import org.eclipse.core.runtime.CoreException; |
49 | 45 | import org.eclipse.core.runtime.IConfigurationElement; |
50 | 46 | import org.eclipse.core.runtime.IPath; |
@@ -100,44 +96,14 @@ public class TargetPlatformService implements ITargetPlatformService { |
100 | 96 | * The target definition currently being used as the target platform for |
101 | 97 | * the workspace. |
102 | 98 | */ |
103 | | - private final AtomicReference<ITargetDefinition> fWorkspaceTarget; |
| 99 | + private final AtomicReference<ITargetDefinition> fWorkspaceTarget = new AtomicReference<>(); |
104 | 100 |
|
105 | 101 | /** |
106 | 102 | * vm arguments for default target |
107 | 103 | */ |
108 | 104 | private StringBuilder fVMArguments; |
109 | 105 |
|
110 | | - private final EventDispatcher eventSendingJob; |
111 | | - |
112 | | - /** |
113 | | - * Collects target files in the workspace |
114 | | - */ |
115 | | - static class ResourceProxyVisitor implements IResourceProxyVisitor { |
116 | | - |
117 | | - private final List<IResource> fList; |
118 | | - |
119 | | - protected ResourceProxyVisitor(List<IResource> list) { |
120 | | - fList = list; |
121 | | - } |
122 | | - |
123 | | - /** |
124 | | - * @see org.eclipse.core.resources.IResourceProxyVisitor#visit(org.eclipse.core.resources.IResourceProxy) |
125 | | - */ |
126 | | - @Override |
127 | | - public boolean visit(IResourceProxy proxy) { |
128 | | - if (proxy.getType() == IResource.FILE) { |
129 | | - if (ICoreConstants.TARGET_FILE_EXTENSION.equalsIgnoreCase(IPath.fromOSString(proxy.getName()).getFileExtension())) { |
130 | | - fList.add(proxy.requestResource()); |
131 | | - } |
132 | | - return false; |
133 | | - } |
134 | | - return true; |
135 | | - } |
136 | | - } |
137 | | - |
138 | 106 | private TargetPlatformService() { |
139 | | - fWorkspaceTarget = new AtomicReference<>(); |
140 | | - eventSendingJob = new EventDispatcher("Sending 'workspace target changed' event", TargetPlatformService.class); //$NON-NLS-1$ |
141 | 107 | } |
142 | 108 |
|
143 | 109 | /** |
@@ -271,21 +237,23 @@ private List<ITargetHandle> findLocalTargetDefinitions() { |
271 | 237 | * @return all target definition handles in the workspace |
272 | 238 | */ |
273 | 239 | private List<WorkspaceFileTargetHandle> findWorkspaceTargetDefinitions() { |
274 | | - List<IResource> files = new ArrayList<>(10); |
275 | | - ResourceProxyVisitor visitor = new ResourceProxyVisitor(files); |
| 240 | + List<IFile> files = new ArrayList<>(10); |
276 | 241 | try { |
277 | | - ResourcesPlugin.getWorkspace().getRoot().accept(visitor, IResource.NONE); |
| 242 | + ResourcesPlugin.getWorkspace().getRoot().accept(proxy -> { |
| 243 | + if (proxy.getType() == IResource.FILE) { |
| 244 | + if (ICoreConstants.TARGET_FILE_EXTENSION |
| 245 | + .equalsIgnoreCase(IPath.fromOSString(proxy.getName()).getFileExtension())) { |
| 246 | + files.add((IFile) proxy.requestResource()); |
| 247 | + } |
| 248 | + return false; |
| 249 | + } |
| 250 | + return true; |
| 251 | + }, IResource.NONE); |
278 | 252 | } catch (CoreException e) { |
279 | 253 | PDECore.log(e); |
280 | | - return new ArrayList<>(0); |
281 | | - } |
282 | | - Iterator<IResource> iter = files.iterator(); |
283 | | - List<WorkspaceFileTargetHandle> handles = new ArrayList<>(files.size()); |
284 | | - while (iter.hasNext()) { |
285 | | - IFile file = (IFile) iter.next(); |
286 | | - handles.add(new WorkspaceFileTargetHandle(file)); |
| 254 | + return List.of(); |
287 | 255 | } |
288 | | - return handles; |
| 256 | + return files.stream().map(WorkspaceFileTargetHandle::new).toList(); |
289 | 257 | } |
290 | 258 |
|
291 | 259 | @Override |
@@ -361,71 +329,20 @@ public synchronized ITargetDefinition getWorkspaceTargetDefinition() throws Core |
361 | 329 | */ |
362 | 330 | public void setWorkspaceTargetDefinition(ITargetDefinition target, boolean asyncEvents) { |
363 | 331 | ITargetDefinition oldTarget = fWorkspaceTarget.getAndSet(target); |
364 | | - boolean changed = !Objects.equals(oldTarget, target); |
365 | | - if (changed) { |
366 | | - if (asyncEvents) { |
367 | | - eventSendingJob.schedule(target); |
368 | | - } else { |
369 | | - notifyTargetChanged(target); |
370 | | - } |
| 332 | + if (!Objects.equals(oldTarget, target)) { |
| 333 | + notifyEvent(TargetEvents.TOPIC_WORKSPACE_TARGET_CHANGED, target, asyncEvents); |
371 | 334 | } |
372 | 335 | } |
373 | 336 |
|
374 | | - static void notifyTargetChanged(ITargetDefinition target) { |
| 337 | + private static void notifyEvent(String topic, Object data, boolean asyncEvents) { |
375 | 338 | IEclipseContext context = EclipseContextFactory.getServiceContext(PDECore.getDefault().getBundleContext()); |
376 | 339 | IEventBroker broker = context.get(IEventBroker.class); |
377 | 340 | if (broker != null) { |
378 | | - broker.send(TargetEvents.TOPIC_WORKSPACE_TARGET_CHANGED, target); |
379 | | - } |
380 | | - } |
381 | | - |
382 | | - static class EventDispatcher extends Job { |
383 | | - |
384 | | - private final ConcurrentLinkedQueue<ITargetDefinition> queue; |
385 | | - private final Object myFamily; |
386 | | - |
387 | | - /** |
388 | | - * @param jobName |
389 | | - * descriptive job name |
390 | | - * @param family |
391 | | - * non null object to control this job execution |
392 | | - **/ |
393 | | - public EventDispatcher(String jobName, Object family) { |
394 | | - super(jobName); |
395 | | - Assert.isNotNull(family); |
396 | | - this.myFamily = family; |
397 | | - this.queue = new ConcurrentLinkedQueue<>(); |
398 | | - setSystem(true); |
399 | | - } |
400 | | - |
401 | | - @Override |
402 | | - public boolean belongsTo(Object family) { |
403 | | - return myFamily == family; |
404 | | - } |
405 | | - |
406 | | - @Override |
407 | | - protected IStatus run(IProgressMonitor monitor) { |
408 | | - ITargetDefinition target; |
409 | | - while ((target = queue.poll()) != null && !monitor.isCanceled()) { |
410 | | - notifyTargetChanged(target); |
411 | | - } |
412 | | - if (!queue.isEmpty() && !monitor.isCanceled()) { |
413 | | - // in case actions got faster scheduled then processed |
414 | | - schedule(); |
415 | | - } |
416 | | - if (monitor.isCanceled()) { |
417 | | - queue.clear(); |
418 | | - return Status.CANCEL_STATUS; |
| 341 | + if (asyncEvents) { |
| 342 | + broker.post(topic, data); |
| 343 | + } else { |
| 344 | + broker.send(topic, data); |
419 | 345 | } |
420 | | - return Status.OK_STATUS; |
421 | | - } |
422 | | - |
423 | | - /** |
424 | | - * Enqueue a task asynchronously. |
425 | | - **/ |
426 | | - public void schedule(ITargetDefinition target) { |
427 | | - queue.offer(target); |
428 | | - schedule(); // will reschedule if already running |
429 | 346 | } |
430 | 347 | } |
431 | 348 |
|
|
0 commit comments