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 9, 2025
1 parent f186d2b commit 70ffedf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.jdt.testplugin.JavaTestPlugin;

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 Down Expand Up @@ -202,9 +203,14 @@ public void testDeleteBottomLevelFragmentFolding() throws Exception {
//send a delta indicating fragment deleted
IElementChangedListener listener= (IElementChangedListener) fProvider;
IJavaElementDelta delta= TestDelta.createDelta(fPack4, IJavaElementDelta.REMOVED);
listener.elementChanged(new ElementChangedEvent(delta, ElementChangedEvent.POST_CHANGE));

//force events from dispaly
//using a job prevents running the update in the UI what would internally create a job so better use one we can control...
Job job= Job.create("ElementChangeTestJob", m->{
listener.elementChanged(new ElementChangedEvent(delta, ElementChangedEvent.POST_CHANGE));
});
job.schedule();
//wait for job
job.join();
//force events from display
while(fMyPart.getTreeViewer().getControl().getDisplay().readAndDispatch())

assertTrue(fMyPart.hasRefreshHappened(), "Refresh happened"); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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 @@ -127,6 +128,20 @@ protected Object getViewerInput() {

@Override
public void elementChanged(final ElementChangedEvent event) {
IJavaElementDelta delta= event.getDelta();
if (Display.findDisplay(Thread.currentThread()) == null) {
processDelta(delta);
} else {
Job job= Job.create(PackagesMessages.PackageExplorerContentProvider_update_job_description, m -> {
processDelta(delta);
return m.isCanceled() ? Status.CANCEL_STATUS : Status.OK_STATUS;
});
job.setSystem(true);
job.schedule();
}
}

protected void processDelta(IJavaElementDelta delta) {
final ArrayList<Runnable> runnables= new ArrayList<>();
try {
clearPackageCache();
Expand All @@ -136,7 +151,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

0 comments on commit 70ffedf

Please sign in to comment.