KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > gui > JErrorPane


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.util.gui;
19
20 import java.awt.BorderLayout JavaDoc;
21 import java.awt.Component JavaDoc;
22 import java.awt.FlowLayout JavaDoc;
23 import java.awt.GridBagConstraints JavaDoc;
24 import java.awt.GridBagLayout JavaDoc;
25 import java.awt.Insets JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.ResourceBundle JavaDoc;
33
34 import javax.swing.AbstractAction JavaDoc;
35 import javax.swing.Action JavaDoc;
36 import javax.swing.BorderFactory JavaDoc;
37 import javax.swing.JButton JavaDoc;
38 import javax.swing.JComponent JavaDoc;
39 import javax.swing.JDialog JavaDoc;
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JOptionPane JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.JScrollPane JavaDoc;
44 import javax.swing.JSeparator JavaDoc;
45 import javax.swing.JTextArea JavaDoc;
46
47 import org.apache.batik.util.gui.resource.ActionMap;
48 import org.apache.batik.util.gui.resource.ButtonFactory;
49 import org.apache.batik.util.gui.resource.MissingListenerException;
50 import org.apache.batik.util.gui.resource.ResourceManager;
51
52 /**
53  * This class represents a dialog to display an error (message + Exception).
54  *
55  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
56  * @version $Id: JErrorPane.java,v 1.7 2004/08/18 07:15:54 vhardy Exp $
57  */

58 public class JErrorPane extends JPanel JavaDoc implements ActionMap {
59
60     /**
61      * The resource file name
62      */

63     protected final static String JavaDoc RESOURCES =
64         "org.apache.batik.util.gui.resources.JErrorPane";
65
66     /**
67      * The resource bundle
68      */

69     protected static ResourceBundle JavaDoc bundle;
70
71     /**
72      * The resource manager
73      */

74     protected static ResourceManager resources;
75
76     static {
77         bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault());
78         resources = new ResourceManager(bundle);
79     }
80
81     /**
82      * The error message.
83      */

84     protected String JavaDoc msg;
85
86     /**
87      * The stack trace.
88      */

89     protected String JavaDoc stacktrace;
90
91     /**
92      * The button factory.
93      */

94     protected ButtonFactory bf = new ButtonFactory(bundle, this);
95
96     /**
97      * The text area used to show the stack trace.
98      */

99     protected JComponent JavaDoc detailsArea;
100
101     /**
102      * The button used to show or not the details.
103      */

104     protected JButton JavaDoc showDetailButton;
105
106     /**
107      * This flag bit indicates whether or not the stack trace is shown.
108      */

109     protected boolean isDetailShown = false;
110
111     /**
112      * The sub panel that contains the stack trace text area.
113      */

114     protected JPanel JavaDoc subpanel;
115
116     /**
117      * Constructs a new JErrorPane.
118      *
119      * @param th the throwable object that describes the errror
120      * @param type the dialog type
121      */

122     public JErrorPane(Throwable JavaDoc th, int type) {
123         super(new GridBagLayout JavaDoc());
124
125         setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
126
127         listeners.put("ShowDetailButtonAction", new ShowDetailButtonAction());
128         listeners.put("OKButtonAction", new OKButtonAction());
129         this.msg = bundle.getString("Heading.text") + "\n\n" + th.getMessage();
130
131         StringWriter JavaDoc writer = new StringWriter JavaDoc();
132         th.printStackTrace(new PrintWriter JavaDoc(writer));
133         writer.flush();
134         this.stacktrace = writer.toString();
135
136         ExtendedGridBagConstraints constraints =
137             new ExtendedGridBagConstraints();
138
139         JTextArea JavaDoc msgArea = new JTextArea JavaDoc();
140         msgArea.setText(msg);
141         msgArea.setColumns(50);
142         msgArea.setFont(new JLabel JavaDoc().getFont());
143         msgArea.setForeground(new JLabel JavaDoc().getForeground());
144         msgArea.setOpaque(false);
145         msgArea.setEditable(false);
146         msgArea.setLineWrap(true);
147
148         constraints.setWeight(0, 0);
149         constraints.anchor = GridBagConstraints.WEST;
150         constraints.fill = GridBagConstraints.NONE;
151         constraints.setGridBounds(0, 0, 1, 1);
152         add(msgArea, constraints);
153
154         constraints.setWeight(1, 0);
155         constraints.anchor = GridBagConstraints.CENTER;
156         constraints.fill = GridBagConstraints.HORIZONTAL;
157         constraints.setGridBounds(0, 1, 1, 1);
158         add(createButtonsPanel(), constraints);
159
160         JTextArea JavaDoc details = new JTextArea JavaDoc();
161         msgArea.setColumns(50);
162         details.setText(stacktrace);
163         details.setEditable(false);
164
165         detailsArea = new JPanel JavaDoc(new BorderLayout JavaDoc(0, 10));
166         detailsArea.add(new JSeparator JavaDoc(), BorderLayout.NORTH);
167         detailsArea.add(new JScrollPane JavaDoc(details), BorderLayout.CENTER);
168
169         subpanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
170
171         constraints.insets = new Insets JavaDoc(10, 4, 4, 4);
172         constraints.setWeight(1, 1);
173         constraints.anchor = GridBagConstraints.CENTER;
174         constraints.fill = GridBagConstraints.BOTH;
175         constraints.setGridBounds(0, 2, 1, 1);
176         add(subpanel, constraints);
177     }
178
179     public JDialog JavaDoc createDialog(Component JavaDoc owner, String JavaDoc title) {
180         JDialog JavaDoc dialog =
181             new JDialog JavaDoc(JOptionPane.getFrameForComponent(owner), title);
182         dialog.getContentPane().add(this, BorderLayout.CENTER);
183         dialog.pack();
184         return dialog;
185     }
186
187     protected JPanel JavaDoc createButtonsPanel() {
188         JPanel JavaDoc panel = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.RIGHT));
189
190         showDetailButton = bf.createJButton("ShowDetailButton");
191         panel.add(showDetailButton);
192
193         JButton JavaDoc okButton = bf.createJButton("OKButton");
194         panel.add(okButton);
195
196         return panel;
197     }
198
199     /**
200      * The map that contains the listeners
201      */

202     protected Map JavaDoc listeners = new HashMap JavaDoc();
203
204     /**
205      * Returns the action associated with the given string or null on error
206      *
207      * @param key the key mapped with the action to get
208      * @throws MissingListenerException if the action is not found
209      */

210     public Action JavaDoc getAction(String JavaDoc key) throws MissingListenerException {
211         return (Action JavaDoc)listeners.get(key);
212     }
213
214     /**
215      * The action associated with the 'OK' button.
216      */

217     protected class OKButtonAction extends AbstractAction JavaDoc {
218
219         public void actionPerformed(ActionEvent JavaDoc evt) {
220             ((JDialog JavaDoc)getTopLevelAncestor()).dispose();
221         }
222     }
223
224     /**
225      * The action associated with the 'Show Detail' button.
226      */

227     protected class ShowDetailButtonAction extends AbstractAction JavaDoc {
228
229         public void actionPerformed(ActionEvent JavaDoc evt) {
230             if (isDetailShown) {
231                 subpanel.remove(detailsArea);
232                 isDetailShown = false;
233                 showDetailButton.setText
234                     (resources.getString("ShowDetailButton.text"));
235             } else {
236                 subpanel.add(detailsArea, BorderLayout.CENTER);
237                 showDetailButton.setText
238                     (resources.getString("ShowDetailButton.text2"));
239                 isDetailShown = true;
240             }
241             ((JDialog JavaDoc)getTopLevelAncestor()).pack();
242         }
243     }
244 }
245
Popular Tags