View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.gui.swing.exceptions;
18  
19  import java.awt.BorderLayout;
20  import java.awt.Rectangle;
21  import java.net.URL;
22  
23  import javax.swing.JButton;
24  import javax.swing.JDialog;
25  import javax.swing.JLabel;
26  import javax.swing.JPanel;
27  import javax.swing.JScrollPane;
28  import javax.swing.JTextArea;
29  import javax.swing.SwingUtilities;
30  import javax.swing.UIManager;
31  
32  import org.jdesktop.application.Action;
33  import org.jdesktop.application.ResourceMap;
34  
35  import ch.elca.el4j.services.gui.swing.GUIApplication;
36  
37  import cookxml.cookswing.CookSwing;
38  
39  /**
40   * The default dialog that appears when an exception occurred.
41   *
42   * @svnLink $Revision: 3875 $;$Date: 2009-08-04 14:35:53 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/swing/src/main/java/ch/elca/el4j/services/gui/swing/exceptions/ExceptionDialog.java $
43   *
44   * @author Stefan Wismer (SWI)
45   */
46  @SuppressWarnings("serial")
47  public class ExceptionDialog extends JDialog {
48  	/**
49  	 * The generic error message.
50  	 */
51  	protected JLabel m_message;
52  	
53  	/**
54  	 * The button to show/hide the details.
55  	 */
56  	protected JButton m_detailsButton;
57  	
58  	/**
59  	 * The scroll pane that makes the details text area scrollable.
60  	 */
61  	protected JScrollPane m_detailsScrollPane;
62  	
63  	/**
64  	 * The details of the exception (in general this is the stacktrace).
65  	 */
66  	protected JTextArea m_details;
67  	
68  	/**
69  	 * The error image.
70  	 */
71  	protected JLabel m_errorImage;
72  	
73  	/**
74  	 * The resource map.
75  	 */
76  	protected ResourceMap m_resourceMap;
77  	
78  	/**
79  	 * @param details    the details to show in the dialog
80  	 */
81  	public ExceptionDialog(String details) {
82  		super(GUIApplication.getInstance().getMainFrame());
83  		
84  		m_resourceMap = GUIApplication.getInstance().getContext().getResourceMap(ExceptionDialog.class);
85  		
86  		CookSwing cookSwing = new CookSwing(this);
87  		URL url = m_resourceMap.getClassLoader().getResource(m_resourceMap.getResourcesDir() + "exceptionDialog.xml");
88  		setLayout(new BorderLayout());
89  		add((JPanel) cookSwing.render(url.getFile()));
90  		
91  		setModal(true);
92  		setTitle(getRes("exceptionDialogTitle"));
93  		m_message.setText(getRes("exceptionDialogMessage"));
94  		m_details.setText(details);
95  		
96  		// adjust font size of details
97  		m_details.setFont(m_details.getFont().deriveFont(12.0f));
98  		
99  		m_errorImage.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
100 		
101 		pack();
102 		
103 		// hide details
104 		toggleDetails();
105 		
106 		// center dialog on the screen
107 		setLocationRelativeTo(null);
108 	}
109 	
110 	/**
111 	 * Close the dialog.
112 	 */
113 	@Action
114 	public void close() {
115 		dispose();
116 	}
117 	
118 	/**
119 	 * Show/hide the details.
120 	 */
121 	@Action
122 	public void toggleDetails() {
123 		Rectangle bounds = getBounds();
124 		if (m_detailsScrollPane.isVisible()) {
125 			m_detailsButton.setText(getRes("moreDetails"));
126 			bounds.height -= m_detailsScrollPane.getBounds().height;
127 			setBounds(bounds);
128 			m_detailsScrollPane.setVisible(false);
129 		} else {
130 			m_detailsButton.setText(getRes("lessDetails"));
131 			bounds.height += m_detailsScrollPane.getBounds().height;
132 			setBounds(bounds);
133 			m_detailsScrollPane.setVisible(true);
134 		}
135 		SwingUtilities.updateComponentTreeUI(this);
136 	}
137 	
138 	/**
139 	 * @param id    the resource ID
140 	 * @return      the String associated with the given resource ID
141 	 */
142 	protected String getRes(String id) {
143 		return m_resourceMap.getString(id);
144 	}
145 
146 }