From 0af0c1007d0c42f207616155b6e683104ba6ae2d Mon Sep 17 00:00:00 2001 From: ricky barrette Date: Thu, 6 Jan 2011 17:21:13 +0000 Subject: [PATCH] created an exception handler that creates a report and dispalys it to the user asking them to email it to twentycodes@gmail.com --- .../java/OrderProcessor/OrderDB.java | 3 +- .../java/OrderProcessor/UI/MainWindow.java | 8 +- .../java/OrderProcessor/UI/SearchDialog.java | 3 + .../java/OrderProcessor/UI/ShowAllDialog.java | 31 +++++-- .../UI/UncaughtExceptionHandler.java | 89 +++++++++++++++++++ 5 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/UncaughtExceptionHandler.java diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/OrderDB.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/OrderDB.java index 7b86cbb..8e132db 100644 --- a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/OrderDB.java +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/OrderDB.java @@ -131,7 +131,8 @@ public class OrderDB { || array.get(i).getCustomerName().toLowerCase(Locale.ENGLISH).contains(text.toLowerCase(Locale.ENGLISH))) list.add(array.get(i)); - mListener.onProgressUpdate(i); + if(mListener != null) + mListener.onProgressUpdate(i); } return list; } diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java index 38a2c14..35f93ec 100644 --- a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/MainWindow.java @@ -38,12 +38,16 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen public static final boolean DEBUG = true; private JProgressBar mProgressBar; private String mCurrentFile; + private UncaughtExceptionHandler mExceptionReport = new UncaughtExceptionHandler(this.getClass()); /** * Creates a new MainWindow * @author ricky barrette */ public MainWindow() { + + Thread.setDefaultUncaughtExceptionHandler(mExceptionReport); + setTitle("Twenty Codes, LLC Order Database"); JPanel panel = new JPanel(); @@ -89,6 +93,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen if(e.getSource() == mShowAllButton){ new ShowAllDialog(db.toString()); } + if (e.getSource() == mSearchButton) { new SearchDialog(); } @@ -98,7 +103,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen */ if(e.getSource() == mLoadFileButton){ try { - if(fc.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) + if(fc.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION){ db.setOnProgressListerner(this); mProgressBar.setIndeterminate(true); mCurrentFile = fc.getSelectedFile().toString(); @@ -117,6 +122,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen }); pack(); + } } catch (FileNotFoundException e1) { // TODO Auto-generated catch block diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/SearchDialog.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/SearchDialog.java index 9763108..4c40918 100644 --- a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/SearchDialog.java +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/SearchDialog.java @@ -198,6 +198,9 @@ public class SearchDialog extends JFrame implements ActionListener, ProgressList */ @Override public void onProgressUpdate(int progress) { + if(MainWindow.DEBUG) + System.out.println("search progress = "+ progress); + progress++; if(mProgressBar.isIndeterminate()) { mProgressBar.setIndeterminate(false); diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/ShowAllDialog.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/ShowAllDialog.java index bcded9e..68c665e 100644 --- a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/ShowAllDialog.java +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/ShowAllDialog.java @@ -25,20 +25,41 @@ public class ShowAllDialog extends JFrame implements ActionListener { /** * Creates a new ShowAllDialog */ - public ShowAllDialog(String list){ + public ShowAllDialog(final String list){ super(); - setTitle("Show All Accounts"); + setTitle("Show All Orders"); + initialize(list); + } + + /** + * Creates a new ShowAllDialog + */ + public ShowAllDialog(String title, String body){ + super(); + setTitle(title); + + initialize(body); + } + + /** + * initializes the dialog's components + * @param body + * @author ricky barrette + */ + private void initialize(String body) { JButton ok = new JButton("Ok"); ok.addActionListener(this); JScrollPane scrollPane = new JScrollPane(); - JTextArea results = new JTextArea(); + final JTextArea results = new JTextArea(); scrollPane.setViewportView(results); - results.setEditable(false); - results.setText(list); + + results.setEditable(false); + results.setText(body); + this.getContentPane().add(scrollPane, BorderLayout.CENTER); this.getContentPane().add(ok, BorderLayout.SOUTH); pack(); diff --git a/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/UncaughtExceptionHandler.java b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/UncaughtExceptionHandler.java new file mode 100644 index 0000000..d89f648 --- /dev/null +++ b/Order Processor/src/com/TwentyCodes/java/OrderProcessor/UI/UncaughtExceptionHandler.java @@ -0,0 +1,89 @@ +/** + * UncaughtExceptionHandler.java + * @date Jan 6, 2011 + * @author ricky barrette + * @author Twenty Codes, LLC + */ +package com.TwentyCodes.java.OrderProcessor.UI; + +/** + * An exception handler used to help report bugs in our applications + * @author ricky barrette + */ +public class UncaughtExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler { + + private java.lang.Thread.UncaughtExceptionHandler mDefaultUEH; + + @SuppressWarnings("rawtypes") + private Class mApp; + + /** + * Creates a new UncaughtExceptionHandler + * @param c + * @author ricky barrette + */ + @SuppressWarnings("rawtypes") + public UncaughtExceptionHandler(Class c) { + mApp = c; + mDefaultUEH = Thread.getDefaultUncaughtExceptionHandler(); + } + + /** + * (non-Javadoc) + * @see java.lang.Thread.UncaughtExceptionHandler#uncaughtException(java.lang.Thread, java.lang.Throwable) + * @author ricky barrette + */ + @Override + public void uncaughtException(Thread t, Throwable e) { + new ShowAllDialog("We're Sorry....", getDebugReport(e)); + mDefaultUEH.uncaughtException(t,e); + } + + /** + * generates a human readable report about the exception + * @param e + * @return + * @author ricky barrette + */ + private String getDebugReport(Throwable e) { + + StackTraceElement[] theStackTrace = e.getStackTrace(); + + StringBuffer report = new StringBuffer(); + + report.append("We're sorry, but there was an error with your application.\n" + + " Please email this report to twentycodes@gmail.com to help make this application better. \n\n"); + + report.append("--------- Application ---------\n\n"); + + report.append(mApp.getName()); + + report.append(" generated the following exception:\n\n"); + + report.append(e.toString() + "\n\n"); + + report.append("-------------------------------\n\n"); + + report.append("--------- Stack trace ---------\n\n"); + for (int i = 0; i < theStackTrace.length; i++) { + report.append(" " + theStackTrace[i].toString() + "\n"); + } + report.append("-------------------------------\n\n"); + + // If the exception was thrown in a background thread inside + // AsyncTask, then the actual exception can be found with getCause + report.append("--------- Cause ---------------\n\n"); + Throwable cause = e.getCause(); + if (cause != null) { + report.append(cause.toString() + "\n\n"); + theStackTrace = cause.getStackTrace(); + for (int i = 0; i < theStackTrace.length; i++) { + report.append(" " + theStackTrace[i].toString() + "\n"); + } + } + report.append("-------------------------------\n\n"); + + return report.toString(); + } + +} \ No newline at end of file