From c5f3d888b4b8906391dac2422da97bbc4ffb9993 Mon Sep 17 00:00:00 2001 From: mdb Date: Wed, 7 May 2003 17:27:12 +0000 Subject: [PATCH] Moved error reporting method into utility class. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1120 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../src/java/robodj/chooser/Chooser.java | 30 ++++------------ .../src/java/robodj/util/ErrorUtil.java | 36 +++++++++++++++++++ 2 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 projects/robodj/src/java/robodj/util/ErrorUtil.java diff --git a/projects/robodj/src/java/robodj/chooser/Chooser.java b/projects/robodj/src/java/robodj/chooser/Chooser.java index 1e08f340..b6737027 100644 --- a/projects/robodj/src/java/robodj/chooser/Chooser.java +++ b/projects/robodj/src/java/robodj/chooser/Chooser.java @@ -1,5 +1,5 @@ // -// $Id: Chooser.java,v 1.11 2003/05/04 18:16:06 mdb Exp $ +// $Id: Chooser.java,v 1.12 2003/05/07 17:27:12 mdb Exp $ package robodj.chooser; @@ -21,6 +21,7 @@ import com.samskivert.util.PropertiesUtil; import robodj.Log; import robodj.repository.Model; import robodj.repository.Repository; +import robodj.util.ErrorUtil; import robodj.util.RDJPrefs; import robodj.util.RDJPrefsPanel; import robodj.util.ServerControl; @@ -61,8 +62,8 @@ public class Chooser model = new Model(repository); } catch (PersistenceException pe) { - if (reportError("Unable to establish communication " + - "with database:", pe)) { + String errmsg = "Unable to communicate with database:"; + if (ErrorUtil.reportError(errmsg, pe)) { System.exit(-1); } continue; @@ -78,9 +79,9 @@ public class Chooser scontrol = new ServerControl( host, RDJPrefs.getMusicDaemonPort()); } catch (IOException ioe) { - if (reportError("Unable to establish communication with " + - "music server on host '" + - RDJPrefs.getMusicDaemonHost() + "' ", ioe)) { + String errmsg = "Unable to communicate with music " + + "server on host '" + RDJPrefs.getMusicDaemonHost() + "' "; + if (ErrorUtil.reportError(errmsg, ioe)) { System.exit(-1); } continue; @@ -96,21 +97,4 @@ public class Chooser SwingUtil.centerWindow(frame); frame.setVisible(true); } - - protected static boolean reportError (String error, Exception e) - { - String text = error; - if (e != null) { - text = text + "\n\n" + e.getMessage(); - if (e.getCause() != null) { - text = text + "\n" + e.getCause().getMessage(); - } - } - Object[] options = { "Edit configuration", "Exit" }; - int choice = JOptionPane.showOptionDialog( - null, text, "Error", JOptionPane.DEFAULT_OPTION, - JOptionPane.ERROR_MESSAGE, null, options, options[0]); - System.out.println("Chose " + choice + "."); - return (choice == 1 || choice == JOptionPane.CLOSED_OPTION); - } } diff --git a/projects/robodj/src/java/robodj/util/ErrorUtil.java b/projects/robodj/src/java/robodj/util/ErrorUtil.java new file mode 100644 index 00000000..90ddbc57 --- /dev/null +++ b/projects/robodj/src/java/robodj/util/ErrorUtil.java @@ -0,0 +1,36 @@ +// +// $Id: ErrorUtil.java,v 1.1 2003/05/07 17:27:12 mdb Exp $ + +package robodj.util; + +import javax.swing.JOptionPane; + +/** + * Contains a useful method for reporting errors. + */ +public class ErrorUtil +{ + /** + * Reports an error, giving the details from the supplied (potentially + * nested) exception. The user is given the choice of editing their + * configuration or exiting the application. + * + * @return false if the user chose to edit the configuration, true if + * they chose to exit. + */ + public static boolean reportError (String error, Exception e) + { + String text = error; + if (e != null) { + text = text + "\n\n" + e.getMessage(); + if (e.getCause() != null) { + text = text + "\n" + e.getCause().getMessage(); + } + } + Object[] options = { "Edit configuration", "Exit" }; + int choice = JOptionPane.showOptionDialog( + null, text, "Error", JOptionPane.DEFAULT_OPTION, + JOptionPane.ERROR_MESSAGE, null, options, options[0]); + return (choice == 1 || choice == JOptionPane.CLOSED_OPTION); + } +}