I’m working on a routine that downloads content from a host to the users desktop.
As the content is being downloaded, a progress dialog is displayed (implemented as ProgressMonitorDialog).
In the progress monitor, the cancel button is enabled.
In the default implementation, if the cancel button is pressed on the ProgressMonitorDialog, the cancel button is disabled.
I wanted to be able to ask the user if they really want to cancel the operation. If they don’t, then continue on with the operation. If they do, then perform the cancellation as usual.
I couldn’t find a straight forward way to implement this with the default operation, so I came up with my own solution…
What I did was override the cancelPressed() method in the ProgressMonitorDialog class to confirm the cancel request before actually invoking the cancel logic.
IRunnableContext progressDialog = new ProgressMonitorDialog(shell) { /** * @see org.eclipse.jface.dialogs.ProgressMonitorDialog#cancelPressed() */ @Override protected void cancelPressed() { if (cancelConfirmed()) { super.cancelPressed(); } } };
In the cancelConfirmed method, I create a final MutablBoolean (part of the Apache Commons Lang project) to hold the users choice, open a message dialog in the UI thread, and set the results in the cancel field.
The cancelConfirmed method looks like this:
private boolean cancelConfirmed() { Display d = Display.getDefault(); final MutableBoolean cancel = new MutableBoolean(false); d.syncExec(new Runnable() { @Override public void run() { cancel.setValue(MessageDialog.openQuestion(null, "Confirm Cancel", "Are you sure you want to cancel the download?")); } }); return cancel.booleanValue(); }