001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: ExceptionDialog.java,v $ 023 Revision 1.5 2004/05/12 18:12:03 markl 024 javadoc updates 025 026 Revision 1.4 2004/05/05 23:20:24 markl 027 comment block updates 028 029 Revision 1.3 2004/03/20 05:16:26 markl 030 layout enhancements 031 032 Revision 1.2 2004/03/16 06:43:39 markl 033 LocaleManager method change 034 035 Revision 1.1 2004/03/16 05:45:15 markl 036 new class 037 ---------------------------------------------------------------------------- 038*/ 039 040package kiwi.ui.dialog; 041 042import java.awt.*; 043import java.awt.event.*; 044import java.io.*; 045import javax.swing.*; 046import javax.swing.border.*; 047 048import kiwi.ui.*; 049import kiwi.util.*; 050 051/** A dialog for presenting an exception to the user. The dialog displays a 052 * message as well as the detail of the exception, including its stack trace. 053 * 054 * <p><center> 055 * <img src="snapshot/ExceptionDialog.gif"><br> 056 * <i>An example ExceptionDialog.</i> 057 * </center> 058 * 059 * <p><center> 060 * <img src="snapshot/ExceptionDialog_2.gif"><br> 061 * <i>An example ExceptionDialog with the detail expanded.</i> 062 * </center> 063 * 064 * @author Mark Lindner 065 * @since Kiwi 2.0 066 */ 067 068public class ExceptionDialog extends ComponentDialog 069 { 070 private KButton b_detail; 071 private JList l_trace; 072 private KPanel p_main, p_detail; 073 private JTextField t_exception; 074 private KLabelArea l_message; 075 private boolean detailShown = false; 076 private KScrollPane scroll; 077 private static Dimension listSize = new Dimension(350, 150); 078 private boolean expandable = true; 079 080 /** Construct a new modal <code>ExceptionDialog</code> with the specified 081 * parent window. 082 * 083 * @param parent The parent window for the dialog. 084 * @param title The title for the dialog window. 085 */ 086 087 public ExceptionDialog(Frame parent, String title) 088 { 089 super(parent, title, true, false); 090 } 091 092 /** Set the "expandable" mode of the dialog. If the mode is enabled, the 093 * dialog will include a "detail" button, which, when clicked, will 094 * expand the dialog to display the exception type and stack trace. 095 * This mode is enabled by default. 096 */ 097 098 public void setExpandable(boolean expandable) 099 { 100 this.expandable = expandable; 101 102 b_detail.setVisible(expandable); 103 } 104 105 /** Build the dialog user interface. */ 106 107 protected Component buildDialogUI() 108 { 109 setComment(null); 110 111 ResourceManager resmgr = KiwiUtils.getResourceManager(); 112 113 setIcon(resmgr.getIcon("alert.gif")); 114 115 LocaleData loc = LocaleManager.getDefault().getLocaleData("KiwiDialogs"); 116 117 b_detail = new KButton(loc.getMessage("kiwi.button.detail"), 118 resmgr.getIcon("down.gif")); 119 b_detail.addActionListener(new ActionListener() 120 { 121 public void actionPerformed(ActionEvent evt) 122 { 123 Object o = evt.getSource(); 124 125 if(o == b_detail) 126 { 127 detailShown = !detailShown; 128 129 if(detailShown) 130 p_main.add("Center", p_detail); 131 else 132 p_main.remove(p_detail); 133 134 pack(); 135 136 if(detailShown) 137 b_detail.setVisible(false); 138 } 139 } 140 }); 141 addButton(b_detail); 142 143 p_main = new KPanel(); 144 p_main.setLayout(new BorderLayout(5, 5)); 145 146 l_message = new KLabelArea(1, 30); 147 p_main.add("North", l_message); 148 149 p_detail = new KPanel(); 150 p_detail.setLayout(new BorderLayout(5, 5)); 151 152 t_exception = new JTextField(); 153 t_exception.setFont(KiwiUtils.boldFont); 154 t_exception.setOpaque(false); 155 t_exception.setEditable(false); 156 157 p_detail.add("North", t_exception); 158 159 l_trace = new JList(); 160 l_trace.setFont(KiwiUtils.plainFont); 161 scroll = new KScrollPane(l_trace); 162 scroll.setSize(listSize); 163 scroll.setPreferredSize(listSize); 164 165 p_detail.add("Center", scroll); 166 167 return(p_main); 168 } 169 170 /** Set a textual error message and the throwable object to be displayed in 171 * the dialog. 172 * 173 * @param message The message. 174 * @param throwable The throwable. 175 */ 176 177 public void setException(String message, Throwable throwable) 178 { 179 String tmsg = throwable.getMessage(); 180 if(tmsg != null) 181 message += "\n" + tmsg; 182 else 183 message += "\n(no message)"; 184 185 l_message.setText(message); 186 187 l_trace.setListData(throwable.getStackTrace()); 188 t_exception.setText(throwable.getClass().getName()); 189 } 190 191 /** Show or hide the dialog. */ 192 193 public void setVisible(boolean flag) 194 { 195 if(flag) 196 { 197 detailShown = false; 198 p_main.remove(p_detail); 199 b_detail.setVisible(expandable); 200 pack(); 201 202 b_ok.requestFocus(); 203 } 204 205 super.setVisible(flag); 206 } 207 208 } 209 210/* end of source file */