代码:
package rcpmail;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.IWorkbenchWindow;
public class MessagePopupAction extends Action {
private final IWorkbenchWindow window;
MessagePopupAction(String text, IWorkbenchWindow window) {
super(text);
this.window = window;
// The id is used to refer to the action in a menu or toolbar
setId(ICommandIds.CMD_OPEN_MESSAGE);
// Associate the action with a pre-defined command, to allow key bindings.
setActionDefinitionId(ICommandIds.CMD_OPEN_MESSAGE);
setImageDescriptor(rcpmail.Activator.getImageDescriptor("/icons/sample3.gif"));
}
public void run() {
try {
window.getWorkbench()
.getProgressService().run(true, true,
new IRunnableWithProgress() {
public void run(IProgressMonitor monitor)
throws InvocationTargetException,
InterruptedException {
monitor.beginTask(
"Simulated long running task #1",
60);
for (int i = 60; i > 0; --i) {
monitor.subTask("seconds left = " + i);
if (monitor.isCanceled())
break;
Thread.sleep(1000);
monitor.worked(1);
}
monitor.done();
}
});
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
MessageDialog.openInformation(window.getShell(), "Open", "Open Message Dialog!");
}
}