KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > chainsaw > DetailPanel


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

16 package org.apache.log4j.chainsaw;
17
18 import java.awt.BorderLayout JavaDoc;
19 import java.text.MessageFormat JavaDoc;
20 import java.util.Date JavaDoc;
21 import javax.swing.BorderFactory JavaDoc;
22 import javax.swing.JEditorPane JavaDoc;
23 import javax.swing.JPanel JavaDoc;
24 import javax.swing.JScrollPane JavaDoc;
25 import javax.swing.JTable JavaDoc;
26 import javax.swing.ListSelectionModel JavaDoc;
27 import javax.swing.event.ListSelectionEvent JavaDoc;
28 import javax.swing.event.ListSelectionListener JavaDoc;
29 import org.apache.log4j.Logger;
30
31 /**
32  * A panel for showing a stack trace.
33  *
34  * @author <a HREF="mailto:oliver@puppycrawl.com">Oliver Burn</a>
35  */

36 class DetailPanel
37     extends JPanel JavaDoc
38     implements ListSelectionListener JavaDoc
39 {
40     /** used to log events **/
41     private static final Logger LOG =
42         Logger.getLogger(DetailPanel.class);
43
44     /** used to format the logging event **/
45     private static final MessageFormat JavaDoc FORMATTER = new MessageFormat JavaDoc(
46         "<b>Time:</b> <code>{0,time,medium}</code>" +
47         "&nbsp;&nbsp;<b>Priority:</b> <code>{1}</code>" +
48         "&nbsp;&nbsp;<b>Thread:</b> <code>{2}</code>" +
49         "&nbsp;&nbsp;<b>NDC:</b> <code>{3}</code>" +
50         "<br><b>Logger:</b> <code>{4}</code>" +
51         "<br><b>Location:</b> <code>{5}</code>" +
52         "<br><b>Message:</b>" +
53         "<pre>{6}</pre>" +
54         "<b>Throwable:</b>" +
55         "<pre>{7}</pre>");
56
57     /** the model for the data to render **/
58     private final MyTableModel mModel;
59     /** pane for rendering detail **/
60     private final JEditorPane JavaDoc mDetails;
61
62     /**
63      * Creates a new <code>DetailPanel</code> instance.
64      *
65      * @param aTable the table to listen for selections on
66      * @param aModel the model backing the table
67      */

68     DetailPanel(JTable JavaDoc aTable, final MyTableModel aModel) {
69         mModel = aModel;
70         setLayout(new BorderLayout JavaDoc());
71         setBorder(BorderFactory.createTitledBorder("Details: "));
72
73         mDetails = new JEditorPane JavaDoc();
74         mDetails.setEditable(false);
75         mDetails.setContentType("text/html");
76         add(new JScrollPane JavaDoc(mDetails), BorderLayout.CENTER);
77
78         final ListSelectionModel JavaDoc rowSM = aTable.getSelectionModel();
79         rowSM.addListSelectionListener(this);
80     }
81
82     /** @see ListSelectionListener **/
83     public void valueChanged(ListSelectionEvent JavaDoc aEvent) {
84         //Ignore extra messages.
85
if (aEvent.getValueIsAdjusting()) {
86             return;
87         }
88
89         final ListSelectionModel JavaDoc lsm = (ListSelectionModel JavaDoc) aEvent.getSource();
90         if (lsm.isSelectionEmpty()) {
91             mDetails.setText("Nothing selected");
92         } else {
93             final int selectedRow = lsm.getMinSelectionIndex();
94             final EventDetails e = mModel.getEventDetails(selectedRow);
95             final Object JavaDoc[] args =
96             {
97                 new Date JavaDoc(e.getTimeStamp()),
98                 e.getPriority(),
99                 escape(e.getThreadName()),
100                 escape(e.getNDC()),
101                 escape(e.getCategoryName()),
102                 escape(e.getLocationDetails()),
103                 escape(e.getMessage()),
104                 escape(getThrowableStrRep(e))
105             };
106             mDetails.setText(FORMATTER.format(args));
107             mDetails.setCaretPosition(0);
108         }
109     }
110
111     ////////////////////////////////////////////////////////////////////////////
112
// Private methods
113
////////////////////////////////////////////////////////////////////////////
114

115     /**
116      * Returns a string representation of a throwable.
117      *
118      * @param aEvent contains the throwable information
119      * @return a <code>String</code> value
120      */

121     private static String JavaDoc getThrowableStrRep(EventDetails aEvent) {
122         final String JavaDoc[] strs = aEvent.getThrowableStrRep();
123         if (strs == null) {
124             return null;
125         }
126
127         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
128         for (int i = 0; i < strs.length; i++) {
129             sb.append(strs[i]).append("\n");
130         }
131
132         return sb.toString();
133     }
134
135     /**
136      * Escape &lt;, &gt; &amp; and &quot; as their entities. It is very
137      * dumb about &amp; handling.
138      * @param aStr the String to escape.
139      * @return the escaped String
140      */

141     private String JavaDoc escape(String JavaDoc aStr) {
142         if (aStr == null) {
143             return null;
144         }
145
146         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
147         for (int i = 0; i < aStr.length(); i++) {
148             char c = aStr.charAt(i);
149             switch (c) {
150             case '<':
151                 buf.append("&lt;");
152                 break;
153             case '>':
154                 buf.append("&gt;");
155                 break;
156             case '\"':
157                 buf.append("&quot;");
158                 break;
159             case '&':
160                 buf.append("&amp;");
161                 break;
162             default:
163                 buf.append(c);
164                 break;
165             }
166         }
167         return buf.toString();
168     }
169 }
170
Popular Tags