Skip to content

Commit

Permalink
Allow the ProgressProvider to control the used instance of a monitor
Browse files Browse the repository at this point in the history
This enables the ProgressProvider to decide the instance of the
ProgressMonitor used in beginRule / join / suspend operations.

This is important if the progressprovider needs to react to certain
actions, e.g. in an UI a provider might make sure the UI is responsive
or show a "terminate" button even if the job is currently waiting on a
rule.

Fix eclipse-platform#40
  • Loading branch information
laeubi committed Apr 27, 2022
1 parent c45ef62 commit c64b821
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2003, 2020 IBM Corporation and others.
* Copyright (c) 2003, 2022 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -20,7 +20,8 @@
* Jan Koehnlein - Fix for bug 60964 (454698)
* Terry Parker - Bug 457504, Publish a job group's final status to IJobChangeListeners
* Xored Software Inc - Fix for bug 550738
* Christoph Laeubrich - remove deprecated API
* Christoph Laeubrich - remove deprecated API
* - Issue #40 - UI freezes when a ThreadJob is waiting to run in the UI Thread
*******************************************************************************/
package org.eclipse.core.internal.jobs;

Expand Down Expand Up @@ -1100,19 +1101,17 @@ boolean join(InternalJobGroup jobGroup, long timeout, IProgressMonitor monitor)
}

/**
* Returns a non-null progress monitor instance. If the monitor is null,
* returns the default monitor supplied by the progress provider, or a
* Returns a non-null progress monitor instance. If a progress provider is
* given, returns the monitor for this supplied by the progress provider, or a
* NullProgressMonitor if no default monitor is available.
*/
private IProgressMonitor monitorFor(IProgressMonitor monitor) {
if (monitor == null || (monitor instanceof NullProgressMonitor)) {
if (progressProvider != null) {
try {
monitor = progressProvider.getDefaultMonitor();
} catch (Exception e) {
String msg = NLS.bind(JobMessages.meta_pluginProblems, JobManager.PI_JOBS);
RuntimeLog.log(new Status(IStatus.ERROR, JobManager.PI_JOBS, JobManager.PLUGIN_ERROR, msg, e));
}
if (progressProvider != null) {
try {
return Objects.requireNonNull(progressProvider.monitorFor(monitor), "ProgressProvider returned null!"); //$NON-NLS-1$
} catch (Exception e) {
String msg = NLS.bind(JobMessages.meta_pluginProblems, JobManager.PI_JOBS);
RuntimeLog.log(new Status(IStatus.ERROR, JobManager.PI_JOBS, JobManager.PLUGIN_ERROR, msg, e));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2003, 2012 IBM Corporation and others.
* Copyright (c) 2003, 2022 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -10,6 +10,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
* Christoph Läubrich - Issue #40 - UI freezes when a ThreadJob is waiting to run in the UI Thread
*******************************************************************************/
package org.eclipse.core.runtime.jobs;

Expand Down Expand Up @@ -96,4 +97,24 @@ public IProgressMonitor createMonitor(Job job, IProgressMonitor group, int ticks
public IProgressMonitor getDefaultMonitor() {
return new NullProgressMonitor();
}

/**
* Returns a (possibly wrapped) progress monitor to use when running the job.
* <p>
* This default implementation returns ProgressProvider#getDefaultMonitor() when
* the monitor is <code>null</code> or an instance of
* {@link NullProgressMonitor} and the passed instance otherwise. Subclasses may
* override.
*
* @param monitor the monitor to wrap, might be null
* @return a progress monitor, either wrapped or the passed instance but never
* <code>null</code>
* @since 3.13
*/
public IProgressMonitor monitorFor(IProgressMonitor monitor) {
if (monitor == null || monitor instanceof NullProgressMonitor) {
return getDefaultMonitor();
}
return monitor;
}
}

0 comments on commit c64b821

Please sign in to comment.