Skip to content

Commit

Permalink
Perform element changes always in a non UI thread
Browse files Browse the repository at this point in the history
Currently PackageExplorerContentProvider receives events for changed
elements from different threads and already has handling to perform only
certain actions in the UI thread. But it does not really account for the
case when the event itself is triggered from the UI thread in which case
lengthy operation can take place in the UI.

This now checks if the change is using the UI and otherwise schedules a
job to perform the required actions in the background.
  • Loading branch information
laeubi committed Jan 10, 2025
1 parent 642cdca commit f361f4b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -29,7 +31,10 @@
import org.eclipse.jdt.testplugin.JavaProjectHelper;
import org.eclipse.jdt.testplugin.JavaTestPlugin;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.jobs.Job;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
Expand All @@ -54,6 +59,7 @@
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;

import org.eclipse.jdt.internal.ui.packageview.PackageExplorerContentProvider;
import org.eclipse.jdt.internal.ui.util.CoreUtility;


Expand Down Expand Up @@ -212,9 +218,25 @@ public void testDeleteBottomLevelFragmentFolding() throws Exception {
protected void sendEvent(IJavaElementDelta delta) {
IElementChangedListener listener= (IElementChangedListener) fProvider;
listener.elementChanged(new ElementChangedEvent(delta, ElementChangedEvent.POST_CHANGE));

//force events from dispaly
while(fMyPart.getTreeViewer().getControl().getDisplay().readAndDispatch()) {
CountDownLatch latch = new CountDownLatch(1);
new Thread(new Runnable() {

@Override
public void run() {
try {
Job.getJobManager().join(PackageExplorerContentProvider.class, new NullProgressMonitor());
} catch (OperationCanceledException | InterruptedException e) {
}
latch.countDown();
}
}).start();
//force events from display
try {
while(!latch.await(10, TimeUnit.MILLISECONDS)) {
while(fMyPart.getTreeViewer().getControl().getDisplay().readAndDispatch()) {
}
}
} catch (InterruptedException e) {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
Expand All @@ -28,6 +30,7 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
Expand Down Expand Up @@ -93,6 +96,8 @@ public class PackageExplorerContentProvider extends StandardJavaElementContentPr

private UIJob fUpdateJob;

private final DeltaJob fdeltaJob;

/**
* We use a cache to know whether a package has a single child for the hierarchical representation.
* This avoids looping over all packages for each call to
Expand All @@ -107,6 +112,7 @@ public class PackageExplorerContentProvider extends StandardJavaElementContentPr
*/
public PackageExplorerContentProvider(boolean provideMembers) {
super(provideMembers);
fdeltaJob= new DeltaJob();
fShowLibrariesNode= false;
fIsFlatLayout= false;
fFoldPackages= arePackagesFoldedInHierarchicalLayout();
Expand All @@ -127,6 +133,12 @@ protected Object getViewerInput() {

@Override
public void elementChanged(final ElementChangedEvent event) {
IJavaElementDelta delta= event.getDelta();
fdeltaJob.queue.add(delta);
fdeltaJob.schedule();
}

protected void processDelta(IJavaElementDelta delta) {
final ArrayList<Runnable> runnables= new ArrayList<>();
try {
clearPackageCache();
Expand All @@ -136,7 +148,7 @@ public void elementChanged(final ElementChangedEvent event) {
if (inputDeleted(runnables))
return;

processDelta(event.getDelta(), runnables);
processDelta(delta, runnables);
} catch (JavaModelException e) {
JavaPlugin.log(e);
} finally {
Expand Down Expand Up @@ -1006,4 +1018,29 @@ public void propertyChange(PropertyChangeEvent event) {
}
}
}

private final class DeltaJob extends Job {
private Queue<IJavaElementDelta> queue= new ConcurrentLinkedQueue<>();

DeltaJob() {
super(PackagesMessages.PackageExplorerContentProvider_update_job_description);
setSystem(true);
}

@Override
public boolean belongsTo(Object family) {
return family == PackageExplorerContentProvider.class;
}

@Override
protected IStatus run(IProgressMonitor monitor) {

IJavaElementDelta delta;
while ((delta= queue.poll()) != null) {
processDelta(delta);
}
return Status.OK_STATUS;
}

}
}

0 comments on commit f361f4b

Please sign in to comment.