KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > gui > frames > GuiExceptionFrame


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.console.gui.frames;
26
27 import java.awt.BorderLayout JavaDoc;
28 import java.awt.Color JavaDoc;
29 import java.awt.Dimension JavaDoc;
30 import java.awt.GridLayout JavaDoc;
31 import java.awt.event.ActionEvent JavaDoc;
32 import java.awt.event.ActionListener JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35
36 import javax.swing.Icon JavaDoc;
37 import javax.swing.JButton JavaDoc;
38 import javax.swing.JDialog JavaDoc;
39 import javax.swing.JFrame JavaDoc;
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42 import javax.swing.JScrollPane JavaDoc;
43 import javax.swing.JTextArea JavaDoc;
44 import javax.swing.JTextField JavaDoc;
45
46 import org.objectweb.cjdbc.common.i18n.GuiTranslate;
47 import org.objectweb.cjdbc.console.gui.constants.GuiCommands;
48 import org.objectweb.cjdbc.console.gui.constants.GuiConstants;
49 import org.objectweb.cjdbc.console.gui.constants.GuiIcons;
50 import org.objectweb.cjdbc.console.gui.jtools.JTextAreaWriter;
51
52 /**
53  * This class defines a GuiExceptionFrame
54  *
55  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
56  * @version 1.0
57  */

58 public class GuiExceptionFrame extends JDialog JavaDoc implements ActionListener JavaDoc
59 {
60   JTextField JavaDoc errorMessage;
61   JTextField JavaDoc classMessage;
62   JTextArea JavaDoc traceMessage;
63   JTextAreaWriter writer;
64   PrintWriter JavaDoc printWriter;
65   JButton JavaDoc showMe;
66   static final int FRAME_WIDTH = 500;
67   static final int SHORT_HEIGHT = 200;
68   static final int LONG_HEIGHT = 400;
69   private JScrollPane JavaDoc scrollPane;
70
71   /**
72    * Creates a new <code>GuiExceptionFrame.java</code> object
73    *
74    * @param gui the parent frame
75    */

76   public GuiExceptionFrame(JFrame JavaDoc gui)
77   {
78     super(gui, true);
79     setTitle(GuiTranslate.get("frame.exception.title"));
80
81     
82     GuiConstants.centerComponent(this,FRAME_WIDTH,SHORT_HEIGHT);
83     setVisible(false);
84
85     getContentPane().setLayout(new BorderLayout JavaDoc());
86
87     GridLayout JavaDoc layout = new GridLayout JavaDoc(6, 1);
88     JPanel JavaDoc messagePanel = new JPanel JavaDoc(layout);
89     messagePanel.add(new JLabel JavaDoc(GuiTranslate.get("frame.exception.error.type")));
90     classMessage = new JTextField JavaDoc(0);
91     classMessage.setBackground(Color.white);
92     classMessage.setAlignmentX(CENTER_ALIGNMENT);
93     classMessage.setEditable(false);
94     messagePanel.add(classMessage);
95
96     messagePanel.add(new JLabel JavaDoc(GuiTranslate.get("frame.exception.error.message")));
97     errorMessage = new JTextField JavaDoc(0);
98     errorMessage.setBackground(Color.white);
99     errorMessage.setAlignmentX(CENTER_ALIGNMENT);
100     errorMessage.setEditable(false);
101     messagePanel.add(errorMessage);
102
103     // Trace
104
JLabel JavaDoc label = new JLabel JavaDoc(GuiTranslate.get("frame.exception.stack.trace"));
105     // Show/hide trace
106
showMe = new JButton JavaDoc();
107     showMe.addActionListener(this);
108     setShowMeToShow();
109
110     messagePanel.add(label);
111     messagePanel.add(showMe);
112
113     traceMessage = new JTextArea JavaDoc();
114     traceMessage.setVisible(false);
115     traceMessage.setAlignmentX(CENTER_ALIGNMENT);
116     traceMessage.setEditable(false);
117     traceMessage.setFont(GuiConstants.CENTER_PANE_FONT);
118     traceMessage.setPreferredSize(new Dimension JavaDoc(FRAME_WIDTH, LONG_HEIGHT / 2));
119     writer = new JTextAreaWriter(traceMessage);
120     printWriter = new PrintWriter JavaDoc(writer);
121     scrollPane = new JScrollPane JavaDoc();
122     scrollPane.getViewport().add(traceMessage);
123     scrollPane.setVisible(false);
124
125     // on the left
126
JButton JavaDoc iconPane = new JButton JavaDoc();
127     Icon JavaDoc icon = GuiIcons.FRAME_ERROR_ICON;
128     iconPane.setIcon(icon);
129     Dimension JavaDoc dime = new Dimension JavaDoc(icon.getIconWidth(), icon.getIconHeight());
130     iconPane.setMaximumSize(dime);
131     iconPane.setPreferredSize(dime);
132     iconPane.setActionCommand(GuiCommands.COMMAND_HIDE_ERROR_FRAME);
133     iconPane.addActionListener(this);
134     getContentPane().add(iconPane, BorderLayout.EAST);
135
136     // on the center
137
getContentPane().add(messagePanel, BorderLayout.CENTER);
138     
139     // on the right
140
getContentPane().add(scrollPane, BorderLayout.SOUTH);
141
142     setVisible(false);
143     setBackground(Color.white);
144     setDefaultCloseOperation(HIDE_ON_CLOSE);
145     validate();
146   }
147
148   /**
149    * Show the exception in a dialog box
150    *
151    * @param e the exception
152    */

153   public void showException(Exception JavaDoc e)
154   {
155     errorMessage.setText(e.getMessage());
156     classMessage.setText(e.getClass().getName());
157     traceMessage.setText("");
158     e.printStackTrace(printWriter);
159     try
160     {
161       writer.flush();
162     }
163     catch (IOException JavaDoc e1)
164     {
165       // can't show, do not show
166
}
167     setVisible(true);
168   }
169
170   private void setShowMeToShow()
171   {
172     showMe.setText(GuiTranslate.get("frame.exception.show.trace"));
173     showMe.setActionCommand(GuiCommands.COMMAND_SHOW_ERROR_TRACE);
174     showMe.validate();
175     showMe.repaint();
176   }
177
178   private void setShowMeToHide()
179   {
180     showMe.setText(GuiTranslate.get("frame.exception.hide.trace"));
181     showMe.setActionCommand(GuiCommands.COMMAND_HIDE_ERROR_TRACE);
182     showMe.validate();
183     showMe.repaint();
184   }
185
186   /**
187    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
188    */

189   public void actionPerformed(ActionEvent JavaDoc e)
190   {
191     if (e.getActionCommand().equals(GuiCommands.COMMAND_HIDE_ERROR_FRAME))
192     {
193       scrollPane.setVisible(false);
194       this.setVisible(false);
195     }
196     else if (e.getActionCommand().equals(GuiCommands.COMMAND_SHOW_ERROR_TRACE))
197     {
198       setSize(FRAME_WIDTH, LONG_HEIGHT);
199       traceMessage.setVisible(true);
200       scrollPane.setVisible(true);
201       validate();
202       repaint();
203       scrollPane.repaint();
204       setShowMeToHide();
205     }
206     else if (e.getActionCommand().equals(GuiCommands.COMMAND_HIDE_ERROR_TRACE))
207     {
208       setSize(FRAME_WIDTH, SHORT_HEIGHT);
209       traceMessage.setVisible(false);
210       scrollPane.setVisible(false);
211       validate();
212       repaint();
213       scrollPane.repaint();
214       setShowMeToShow();
215     }
216   }
217 }
218
Popular Tags