Java tutorial
/* * Copyright (C) 2016 Artiom Blinvoas * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package xmlconverter.tools; import java.io.File; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JOptionPane; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; import xmlconverter.view.MainFrame; /** * This class provides a way to handle fatal errors. */ public class ErrorHandling { private Logger log = Logger.getLogger(this.getClass()); private MainFrame mainFrame; private SaveFile saveFile; private JFrame frame; public ErrorHandling(JFrame frame) { this.frame = frame; } public ErrorHandling(MainFrame frame) { this.mainFrame = frame; } /** * Handles an exception. This method will copy log file to whatever user * specifies. * * @param message * @param th */ public void errorHandling(String message, Throwable th) { if (mainFrame == null) { int opt = JOptionPane.showConfirmDialog(mainFrame.getFrame(), "This option is now allowed\n", "Nope. Use different constructor", JOptionPane.OK_OPTION); } else { String fs = File.separator; saveFile = new SaveFile(mainFrame); log.fatal(message, th); int opt = JOptionPane.showConfirmDialog(mainFrame.getFrame(), "Please save this file and mail it to developer for a review.\n" + "See debug console!\n" + "File will be saved in the selected directorie\n" + "Do you want to save this file?", "Save debug.log to", JOptionPane.YES_NO_OPTION); if (opt == JOptionPane.OK_OPTION) { try { File destLogDir = saveFile.saveFile(); File srcFile = new File(System.getProperty("java.io.tmpdir") + fs + "debug.log"); FileUtils.copyFileToDirectory(srcFile, destLogDir); } catch (IOException | NullPointerException | SecurityException e) { log.error("Could not copy log file!", e); } log.info("Log file was successfully saved!"); } } } public void showInfo(String message) { int opt = JOptionPane.showConfirmDialog(frame, message, "Tree is not ready to be shown!", JOptionPane.PLAIN_MESSAGE); } }