KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > sqlprofiler > gui > DetailPanel


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software
5  * License version 1.1, a copy of which has been included with this
6  * distribution in the APACHE.txt file. */

7 package org.jahia.sqlprofiler.gui;
8
9 import java.awt.BorderLayout JavaDoc;
10 import java.text.MessageFormat JavaDoc;
11 import java.util.Date JavaDoc;
12 import javax.swing.BorderFactory JavaDoc;
13 import javax.swing.JEditorPane JavaDoc;
14 import javax.swing.JPanel JavaDoc;
15 import javax.swing.JScrollPane JavaDoc;
16 import javax.swing.JTable JavaDoc;
17 import javax.swing.ListSelectionModel JavaDoc;
18 import javax.swing.event.ListSelectionEvent JavaDoc;
19 import javax.swing.event.ListSelectionListener JavaDoc;
20 import org.apache.log4j.Category;
21
22 /**
23  * A panel for showing a stack trace.
24  *
25  * @author <a HREF="mailto:oliver@puppycrawl.com">Oliver Burn</a>
26  */

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

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

106     /**
107      * Returns a string representation of a throwable.
108      *
109      * @param aEvent contains the throwable information
110      * @return a <code>String</code> value
111      */

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

132     private String JavaDoc escape(String JavaDoc aStr) {
133         if (aStr == null) {
134             return null;
135         }
136
137         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
138         for (int i = 0; i < aStr.length(); i++) {
139             char c = aStr.charAt(i);
140             switch (c) {
141             case '<':
142                 buf.append("&lt;");
143                 break;
144             case '>':
145                 buf.append("&gt;");
146                 break;
147             case '\"':
148                 buf.append("&quot;");
149                 break;
150             case '&':
151                 buf.append("&amp;");
152                 break;
153             default:
154                 buf.append(c);
155                 break;
156             }
157         }
158         return buf.toString();
159     }
160 }
161
Popular Tags