001 /** 002 * iFish - An iRiver iHP jukebox database creation tool 003 * 004 * Copyright (C) 2009 Richard "Shred" Körber 005 * http://ifish.shredzone.org 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as published by 009 * the Free Software Foundation, either version 3 of the License, or 010 * (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 019 */ 020 package net.shredzone.ifish.gui; 021 022 import net.shredzone.ifish.i18n.L; 023 import net.shredzone.jshred.swing.JCollapsiblePanel; 024 025 import java.awt.*; 026 import java.io.PrintWriter; 027 import java.io.StringWriter; 028 029 import javax.swing.*; 030 import java.text.*; 031 032 /** 033 * Informs the user about an Exception that has occured. 034 * 035 * @author Richard Körber <dev@shredzone.de> 036 * @version $Id: ExceptionDialog.java 291 2009-04-28 21:29:27Z shred $ 037 */ 038 public class ExceptionDialog { 039 private static boolean debugMode = false; 040 041 /** 042 * Set the debug mode. If set, a stacktrace will be shown along with the 043 * dialog. 044 * 045 * @param mode true: debug mode, false: standard mode 046 */ 047 public static void setDebug( boolean mode ) { 048 debugMode = mode; 049 } 050 051 /** 052 * Check if the debug mode is enabled. 053 * 054 * @return true: debug mode, false: standard mode 055 */ 056 public static boolean isDebug() { 057 return debugMode; 058 } 059 060 /** 061 * Show an exception dialog if an exception occured. 062 * 063 * @param parent Parent component to be locked during dialog 064 * @param op Operation that was being performed when the 065 * exception was raised. 066 * @param ex Exception that was raised. 067 */ 068 public static void show( Component parent, String op, Exception ex ) { 069 //--- Write a stacktrace to stderr --- 070 ex.printStackTrace(); 071 072 //--- Create the arguments array --- 073 Object[] args = new Object[] { 074 op, 075 ex.getLocalizedMessage() 076 }; 077 078 //--- Create a message string --- 079 String message = MessageFormat.format( L.tr("exception.msg"), args ); 080 081 //--- Show the dialog --- 082 Object msg = message; 083 084 //--- Debug: Show a stacktrace --- 085 if( isDebug() ) { 086 StringWriter sw = new StringWriter(); 087 ex.printStackTrace( new PrintWriter( sw ) ); 088 089 JTextArea jtaTrace = new JTextArea(); 090 jtaTrace.setRows(3); 091 jtaTrace.setColumns(40); 092 jtaTrace.setEditable(false); 093 jtaTrace.setText(sw.toString()); 094 jtaTrace.setCaretPosition(0); 095 096 msg = new Object[] { message, new JScrollPane( jtaTrace ) }; 097 } 098 099 JOptionPane.showMessageDialog( 100 parent, 101 msg, 102 L.tr("exception.title"), 103 JOptionPane.ERROR_MESSAGE 104 ); 105 } 106 107 }