From 40484d1f57fd128168fd50574aa3a217305f3569 Mon Sep 17 00:00:00 2001 From: mdb Date: Mon, 19 Mar 2001 23:03:56 +0000 Subject: [PATCH] Created a utility for invoking tasks such that they can be retried by the user if they fail (presumably after making some external change like changing file permissions or restarting a database). git-svn-id: https://samskivert.googlecode.com/svn/trunk@104 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../samskivert/swing/util/RetryableTask.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 projects/samskivert/src/java/com/samskivert/swing/util/RetryableTask.java diff --git a/projects/samskivert/src/java/com/samskivert/swing/util/RetryableTask.java b/projects/samskivert/src/java/com/samskivert/swing/util/RetryableTask.java new file mode 100644 index 00000000..061fdce8 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/swing/util/RetryableTask.java @@ -0,0 +1,52 @@ +// +// $Id: RetryableTask.java,v 1.1 2001/03/19 23:03:56 mdb Exp $ + +package com.samskivert.swing.util; + +import java.awt.Component; +import javax.swing.*; +import com.samskivert.Log; + +/** + * A retryable task is one that is allowed to fail at which point a dialog + * is presented to the user asking them if they would like to retry the + * task. In general, such practices are discouraged because the software + * should handle retrying itself, but in cases where failure results in + * the abandonment of a lot of work and automatic retries have already + * been tried, it can be reasonable to give the user one last chance to + * remedy any problems that are causing the error. + */ +public abstract class RetryableTask +{ + /** + * This should be implemented by the retryable task user to perform + * whatever they wish to be done in a retryable manner. + */ + public abstract void invoke () throws Exception; + + /** + * Invokes the supplied task and catches any thrown exceptions. In the + * event of an exception, the provided message is displayed to the + * user and the are allowed to retry the task or allow it to fail. + */ + public void invokeTask (Component parent, String retryMessage) + throws Exception + { + while (true) { + try { + invoke(); + + } catch (Exception e) { + Object[] options = new Object[] { + "Retry operation", "Abort operation" }; + int rv = JOptionPane.showOptionDialog( + parent, retryMessage + "\n\n" + e.getMessage(), + "Operation failure", JOptionPane.YES_NO_OPTION, + JOptionPane.WARNING_MESSAGE, null, options, options[1]); + if (rv == 1) { + throw e; + } + } + } + } +}