Skip to content

Commit f98cd50

Browse files
committed
[PoC] Add option to hide the Cancel button of jobs in the progress view
For long-running jobs that can't be reliably cancelled, it should be possible to hide the Cancel button, so that the job can still be used to provide feedback to the user without having to worry about the potential cancellation in the progress monitor. This behavior is currently controlled via a property that needs to be set for the job before it is scheduled. But it might also be worthwhile to add this API directly to the Job class. Note: In order to ensure an equal width for cancellable and non-cancellable jobs, the button is created, but simply turned invisible. This should also make it easier to dynamically hide/show the button, if such a functionality is needed at a later stage. Relates to: - https://bugs.eclipse.org/bugs/show_bug.cgi?id=155479 - https://bugs.eclipse.org/bugs/show_bug.cgi?id=102343
1 parent 9c7305e commit f98cd50

File tree

8 files changed

+93
-10
lines changed

8 files changed

+93
-10
lines changed

bundles/org.eclipse.e4.ui.progress/src/org/eclipse/e4/ui/progress/IProgressConstants.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2004, 2014 IBM Corporation and others.
2+
* Copyright (c) 2004, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -165,4 +165,15 @@ public interface IProgressConstants {
165165
PROPERTY_PREFIX, "inTaskBarIcon"); //$NON-NLS-1$
166166

167167
public static final String RUN_IN_BACKGROUND = "RUN_IN_BACKGROUND"; //$NON-NLS-1$
168+
169+
/**
170+
* This property provides a hint to the progress UI to hide the "Cancel" button,
171+
* effectively making the job uncancellable by the user.
172+
* <p>
173+
* The property must be of type {@code Boolean}.
174+
* </p>
175+
*
176+
* @since 0.4.700
177+
*/
178+
QualifiedName CANCELLABLE = new QualifiedName(PROPERTY_PREFIX, "cancellable"); //$NON-NLS-1$
168179
}

bundles/org.eclipse.e4.ui.progress/src/org/eclipse/e4/ui/progress/internal/JobInfo.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2003, 2020 IBM Corporation and others.
2+
* Copyright (c) 2003, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -21,6 +21,7 @@
2121
import org.eclipse.core.runtime.IProgressMonitor;
2222
import org.eclipse.core.runtime.IStatus;
2323
import org.eclipse.core.runtime.jobs.Job;
24+
import org.eclipse.e4.ui.progress.IProgressConstants;
2425
import org.eclipse.jface.resource.JFaceResources;
2526
import org.eclipse.osgi.util.NLS;
2627
import org.eclipse.swt.graphics.Image;
@@ -374,7 +375,16 @@ public boolean isCanceled() {
374375

375376
@Override
376377
public boolean isCancellable() {
377-
return super.isCancellable();
378+
return super.isCancellable() || isJobCancellable();
379+
}
380+
381+
private boolean isJobCancellable() {
382+
Boolean cancellable = (Boolean) job.getProperty(IProgressConstants.CANCELLABLE);
383+
// cancellable by default
384+
if (cancellable == null) {
385+
return true;
386+
}
387+
return cancellable;
378388
}
379389

380390
@Override

bundles/org.eclipse.e4.ui.progress/src/org/eclipse/e4/ui/progress/internal/ProgressInfoItem.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2005, 2017 IBM Corporation and others.
2+
* Copyright (c) 2005, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -657,6 +657,15 @@ private void updateToolBarValues() {
657657
}
658658
JobInfo[] infos = getJobInfos();
659659

660+
for (JobInfo jobInfo : getJobInfos()) {
661+
// Hide cancel button if job not cancellable
662+
if (!jobInfo.isCancellable()) {
663+
actionBar.setVisible(false);
664+
return;
665+
}
666+
}
667+
actionBar.setVisible(true);
668+
660669
for (JobInfo info : infos) {
661670
// Only disable if there is an unresponsive operation
662671
if (info.isCanceled() && !isCompleted()) {

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/progress/JobInfo.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2003, 2020 IBM Corporation and others.
2+
* Copyright (c) 2003, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -24,6 +24,7 @@
2424
import org.eclipse.jface.resource.JFaceResources;
2525
import org.eclipse.osgi.util.NLS;
2626
import org.eclipse.swt.graphics.Image;
27+
import org.eclipse.ui.progress.IProgressConstants2;
2728

2829
/**
2930
* JobInfo is the class that keeps track of the tree structure for objects that
@@ -317,7 +318,16 @@ public boolean isCanceled() {
317318

318319
@Override
319320
public boolean isCancellable() {
320-
return super.isCancellable();
321+
return super.isCancellable() || isJobCancellable();
322+
}
323+
324+
private boolean isJobCancellable() {
325+
Boolean cancellable = (Boolean) job.getProperty(IProgressConstants2.CANCELLABLE);
326+
// cancellable by default
327+
if (cancellable == null) {
328+
return true;
329+
}
330+
return cancellable;
321331
}
322332

323333
@Override

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/progress/ProgressInfoItem.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2005, 2015 IBM Corporation and others.
2+
* Copyright (c) 2005, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -637,6 +637,15 @@ private void updateToolBarValues() {
637637

638638
}
639639

640+
for (JobInfo jobInfo : getJobInfos()) {
641+
// Hide cancel button if job not cancellable
642+
if (!jobInfo.isCancellable()) {
643+
actionBar.setVisible(false);
644+
return;
645+
}
646+
}
647+
actionBar.setVisible(true);
648+
640649
for (JobInfo jobInfo : getJobInfos()) {
641650
// Only disable if there is an unresponsive operation
642651
if (jobInfo.isCanceled() && !isCompleted()) {

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/progress/IProgressConstants2.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2010, 2020 IBM Corporation and others.
2+
* Copyright (c) 2010, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -60,4 +60,15 @@ public interface IProgressConstants2 extends IProgressConstants {
6060
* </p>
6161
*/
6262
QualifiedName SHOW_IN_TASKBAR_ICON_PROPERTY = new QualifiedName(PROPERTY_PREFIX, "inTaskBarIcon"); //$NON-NLS-1$
63+
64+
/**
65+
* This property provides a hint to the progress UI to hide the "Cancel" button,
66+
* effectively making the job uncancellable by the user.
67+
* <p>
68+
* The property must be of type {@code Boolean}.
69+
* </p>
70+
*
71+
* @since 3.135
72+
*/
73+
QualifiedName CANCELLABLE = new QualifiedName(IProgressConstants.PROPERTY_PREFIX, "cancellable"); //$NON-NLS-1$
6374
}

examples/org.eclipse.e4.ui.examples.job/src/org/eclipse/e4/ui/examples/jobs/views/JobsView.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2014, 2019 Wojciech Sudol and others.
2+
* Copyright (c) 2014, 2025 Wojciech Sudol and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -62,6 +62,7 @@ public class JobsView {
6262
private Text quantityField, delayField, rescheduleDelay;
6363
private Button schedulingRuleField;
6464
private Button noPromptField;
65+
private Button cancellableField;
6566

6667
Composite parent;
6768

@@ -110,6 +111,7 @@ protected void createJobs() {
110111
boolean keepOne = keepOneField.getSelection();
111112
boolean gotoAction = gotoActionField.getSelection();
112113
boolean schedulingRule = schedulingRuleField.getSelection();
114+
boolean cancellable = cancellableField.getSelection();
113115

114116
int groupIncrement = IProgressMonitor.UNKNOWN;
115117
IProgressMonitor group = new NullProgressMonitor();
@@ -134,6 +136,8 @@ protected void createJobs() {
134136
result = new TestJob(duration, lock, failure, unknown,
135137
reschedule, rescheduleWait);
136138

139+
result.setProperty(IProgressConstants.CANCELLABLE, Boolean
140+
.valueOf(cancellable));
137141
result.setProperty(IProgressConstants.KEEP_PROPERTY, Boolean
138142
.valueOf(keep));
139143
result.setProperty(IProgressConstants.KEEPONE_PROPERTY, Boolean
@@ -459,6 +463,13 @@ private void createCheckboxGroup(Composite parent) {
459463
noPromptField
460464
.setToolTipText("Set the IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY to true");
461465
noPromptField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
466+
467+
// cancellable
468+
cancellableField = new Button(group, SWT.CHECK);
469+
cancellableField.setText("Cancellable"); //$NON-NLS-1$
470+
cancellableField.setToolTipText("Whether the job can be cancelled by the user"); //$NON-NLS-1$
471+
cancellableField.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
472+
cancellableField.setSelection(true);
462473
}
463474

464475
protected void doRun(long duration, IProgressMonitor monitor) {

examples/org.eclipse.ui.examples.job/src/org/eclipse/ui/examples/jobs/views/JobsView.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2004, 2019 IBM Corporation and others.
2+
* Copyright (c) 2004, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -45,6 +45,7 @@
4545
import org.eclipse.ui.examples.jobs.UITestJob;
4646
import org.eclipse.ui.part.ViewPart;
4747
import org.eclipse.ui.progress.IProgressConstants;
48+
import org.eclipse.ui.progress.IProgressConstants2;
4849
import org.eclipse.ui.progress.IProgressService;
4950

5051
/**
@@ -59,6 +60,7 @@ public class JobsView extends ViewPart {
5960
private Text quantityField, delayField, rescheduleDelay;
6061
private Button schedulingRuleField;
6162
private Button noPromptField;
63+
private Button cancellableField;
6264

6365
protected void busyCursorWhile() {
6466
try {
@@ -95,6 +97,7 @@ protected void createJobs() {
9597
boolean keepOne = keepOneField.getSelection();
9698
boolean gotoAction = gotoActionField.getSelection();
9799
boolean schedulingRule = schedulingRuleField.getSelection();
100+
boolean cancellable = cancellableField.getSelection();
98101

99102
int groupIncrement = IProgressMonitor.UNKNOWN;
100103
IProgressMonitor group = new NullProgressMonitor();
@@ -119,6 +122,8 @@ protected void createJobs() {
119122
result = new TestJob(duration, lock, failure, unknown,
120123
reschedule, rescheduleWait);
121124

125+
result.setProperty(IProgressConstants2.CANCELLABLE, Boolean
126+
.valueOf(cancellable));
122127
result.setProperty(IProgressConstants.KEEP_PROPERTY, Boolean
123128
.valueOf(keep));
124129
result.setProperty(IProgressConstants.KEEPONE_PROPERTY, Boolean
@@ -427,6 +432,13 @@ private void createCheckboxGroup(Composite parent) {
427432
noPromptField.setText("No Prompt"); //$NON-NLS-1$
428433
noPromptField.setToolTipText("Set the IProgressConstants.NO_IMMEDIATE_ERROR_PROMPT_PROPERTY to true"); //$NON-NLS-1$
429434
noPromptField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
435+
436+
// cancellable
437+
cancellableField = new Button(group, SWT.CHECK);
438+
cancellableField.setText("Cancellable"); //$NON-NLS-1$
439+
cancellableField.setToolTipText("Whether the job can be cancelled by the user"); //$NON-NLS-1$
440+
cancellableField.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
441+
cancellableField.setSelection(true);
430442
}
431443

432444
protected void doRun(long duration, IProgressMonitor monitor) {

0 commit comments

Comments
 (0)