001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.gui.toolkit;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Component;
017: import java.awt.Dimension;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020: import java.lang.reflect.Method;
021:
022: import javax.swing.DefaultListModel;
023: import javax.swing.JCheckBox;
024: import javax.swing.JLabel;
025: import javax.swing.JList;
026: import javax.swing.JPanel;
027: import javax.swing.JScrollPane;
028:
029: import org.apache.avalon.framework.CascadingException;
030: import org.apache.avalon.framework.logger.Logger;
031: import org.rapla.components.xmlbundle.I18nBundle;
032: import org.rapla.framework.RaplaContext;
033: import org.rapla.framework.RaplaException;
034:
035: final public class ErrorDialog {
036: RaplaContext sm;
037: Logger logger;
038: I18nBundle i18n;
039:
040: /**
041: * @param sm
042: * @throws RaplaException
043: */
044: public ErrorDialog(RaplaContext sm) throws RaplaException {
045: this .sm = sm;
046: this .i18n = (I18nBundle) sm.lookup(I18nBundle.ROLE
047: + "/org.rapla.RaplaResources");
048: this .logger = (Logger) sm.lookup(Logger.class.getName());
049: }
050:
051: private RaplaContext getContext() {
052: return sm;
053: }
054:
055: protected I18nBundle getI18n() {
056: return i18n;
057: }
058:
059: protected Logger getLogger() {
060: return logger;
061: }
062:
063: public static final int WARNING_MESSAGE = 1;
064: public static final int ERROR_MESSAGE = 2;
065: public static final int EXCEPTION_MESSAGE = 3;
066:
067: /** This is for the test-cases only. If this flag is set
068: the ErrorDialog throws an ErrorDialogException instead of
069: displaying the dialog. This is useful for testing. */
070: public static boolean THROW_ERROR_DIALOG_EXCEPTION = false;
071:
072: private void test(String message, int type) {
073: if (THROW_ERROR_DIALOG_EXCEPTION) {
074: throw new ErrorDialogException(new RaplaException(message),
075: type);
076: }
077: }
078:
079: private void test(Throwable ex, int type) {
080: if (THROW_ERROR_DIALOG_EXCEPTION) {
081: throw new ErrorDialogException(ex, type);
082: }
083: }
084:
085: private String createTitle(String key) {
086: return getI18n().format("exclamation.format",
087: getI18n().getString(key));
088: }
089:
090: public void show(String message) {
091: test(message, ERROR_MESSAGE);
092: try {
093: showDialog(createTitle("error"), message, null);
094: } catch (Exception ex) {
095: System.err.println(message);
096: }
097: }
098:
099: public void showWarningDialog(String message, Component owner) {
100: test(message, WARNING_MESSAGE);
101: try {
102: showWarningDialog(createTitle("warning"), message, owner);
103: } catch (Exception ex) {
104: System.err.println(message);
105: }
106: }
107:
108: static private String getCause(Throwable e) {
109: String message = e.getMessage();
110: if (message != null && message.length() > 0) {
111: return message;
112: }
113: if (e instanceof CascadingException) {
114: Throwable cause = ((CascadingException) e).getCause();
115: if (cause != null)
116: message = getCause(cause);
117: }
118: return message;
119: }
120:
121: static public String getMessage(Throwable e) {
122: String message = getCause(e);
123: if (message == null || message.length() == 0)
124: message = e.toString();
125: return message;
126: }
127:
128: public void showExceptionDialog(Throwable e, Component owner) {
129: test(e, EXCEPTION_MESSAGE);
130: try {
131: String message = getMessage(e);
132: if (getLogger() != null)
133: getLogger().error(message, e);
134: JPanel component = new JPanel();
135: component.setLayout(new BorderLayout());
136: component.add(new JLabel(message), BorderLayout.NORTH);
137: boolean showStacktrace = true;
138: Throwable nestedException = e;
139:
140: do {
141: if (nestedException instanceof RaplaException) {
142: showStacktrace = false;
143: nestedException = ((RaplaException) nestedException)
144: .getCause();
145: } else {
146: showStacktrace = true;
147: }
148: } while (nestedException != null && !showStacktrace);
149:
150: if (showStacktrace) {
151: try {
152: Method getStackTrace = Exception.class.getMethod(
153: "getStackTrace", new Class[] {});
154: final Object[] stackTrace = (Object[]) getStackTrace
155: .invoke(e, new Object[] {});
156: final JList lister = new JList();
157: final JScrollPane list = new JScrollPane(lister,
158: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
159: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
160: list.setBorder(null);
161: JPanel stackTracePanel = new JPanel();
162: final JCheckBox stackTraceChooser = new JCheckBox(
163: "show stacktrace");
164: stackTracePanel.setLayout(new BorderLayout());
165: stackTracePanel.add(stackTraceChooser,
166: BorderLayout.NORTH);
167: stackTracePanel.add(list, BorderLayout.CENTER);
168: stackTracePanel.setPreferredSize(new Dimension(300,
169: 200));
170: stackTracePanel.setMinimumSize(new Dimension(300,
171: 200));
172: component.add(stackTracePanel, BorderLayout.CENTER);
173: lister.setVisible(false);
174: stackTraceChooser
175: .addActionListener(new ActionListener() {
176:
177: public void actionPerformed(
178: ActionEvent e) {
179: DefaultListModel model = new DefaultListModel();
180: if (stackTraceChooser.isSelected()) {
181: for (int i = 0; i < stackTrace.length; i++) {
182: model
183: .addElement(stackTrace[i]);
184: }
185: }
186: lister.setModel(model);
187: lister.setVisible(stackTraceChooser
188: .isSelected());
189: }
190:
191: });
192: } catch (Exception ex) {
193: }
194: }
195:
196: DialogUI dlg = DialogUI.create(getContext(), owner, true,
197: component,
198: new String[] { getI18n().getString("ok") });
199: dlg.setTitle(createTitle("error"));
200: dlg.setIcon(getI18n().getIcon("icon.error"));
201: dlg.start();
202: } catch (Exception ex) {
203: ex.printStackTrace();
204: e.printStackTrace();
205: }
206: }
207:
208: private void showDialog(String title, String message,
209: Component owner) {
210: try {
211: DialogUI dlg = DialogUI.create(getContext(), owner, true,
212: title, message);
213: dlg.setIcon(getI18n().getIcon("icon.error"));
214: dlg.start();
215: } catch (Exception ex2) {
216: getLogger().error(ex2.getMessage());
217: }
218: }
219:
220: public void showWarningDialog(String title, String message,
221: Component owner) {
222: try {
223: DialogUI dlg = DialogUI.create(getContext(), owner, true,
224: title, message);
225: dlg.setIcon(getI18n().getIcon("icon.warning"));
226: dlg.start();
227: } catch (Exception ex2) {
228: getLogger().error(ex2.getMessage());
229: }
230: }
231: }
|