KickJava   Java API By Example, From Geeks To Geeks.

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


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

8 package org.jahia.sqlprofiler.gui;
9
10 import javax.swing.JPanel JavaDoc;
11 import javax.swing.event.ListSelectionListener JavaDoc;
12 import org.apache.log4j.Category;
13 import java.text.MessageFormat JavaDoc;
14 import javax.swing.JEditorPane JavaDoc;
15 import javax.swing.JTable JavaDoc;
16 import java.awt.BorderLayout JavaDoc;
17 import javax.swing.BorderFactory JavaDoc;
18 import javax.swing.JScrollPane JavaDoc;
19 import javax.swing.ListSelectionModel JavaDoc;
20 import javax.swing.event.ListSelectionEvent JavaDoc;
21 import org.jahia.sqlprofiler.QueryEntry;
22 import java.util.Date JavaDoc;
23
24 /**
25  * <p>Title: A panel that displays more details about the currently selected
26  * SQL Statement</p>
27  * <p>Description: </p>
28  * <p>Copyright: Copyright (c) 2003</p>
29  * <p>Company: Jahia Ltd</p>
30  * @author Serge Huber
31  * @version 1.0
32  */

33
34 class ProfileDetailsPanel extends JPanel JavaDoc
35     implements ListSelectionListener JavaDoc {
36
37     private static final Category LOG =
38         Category.getInstance(ProfileDetailsPanel.class);
39
40     /** used to format the logging event **/
41     private static final MessageFormat JavaDoc FORMATTER = new MessageFormat JavaDoc(
42         "<b>Time:</b> <code>{0,time,medium}</code>" +
43         "&nbsp;&nbsp;<b>Elapsed time:</b> <code>{1,time,medium}</code>" +
44         "&nbsp;&nbsp;<b>Category:</b> <code>{2}</code>" +
45         "&nbsp;&nbsp;<b>Connection ID:</b> <code>{3}</code>" +
46         "<br><b>Prepared SQL:</b><br>" +
47         "{4}<br>" +
48         "<b>SQL Statement:</b><br>" +
49         "{5}");
50
51     /** the model for the data to render **/
52     private final ProfileStatementTableModel mModel;
53     /** pane for rendering detail **/
54     private final JEditorPane JavaDoc mDetails;
55
56     /**
57      * Creates a new <code>DetailPanel</code> instance.
58      *
59      * @param aTable the table to listen for selections on
60      * @param aModel the model backing the table
61      */

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

106     /**
107      * Escape &lt;, &gt; &amp; and &quot; as their entities. It is very
108      * dumb about &amp; handling.
109      * @param aStr the String to escape.
110      * @return the escaped String
111      */

112     private String JavaDoc escape(String JavaDoc aStr) {
113         if (aStr == null) {
114             return null;
115         }
116
117         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
118         for (int i = 0; i < aStr.length(); i++) {
119             char c = aStr.charAt(i);
120             switch (c) {
121                 case '<':
122                     buf.append("&lt;");
123                     break;
124                 case '>':
125                     buf.append("&gt;");
126                     break;
127                 case '\"':
128                     buf.append("&quot;");
129                     break;
130                 case '&':
131                     buf.append("&amp;");
132                     break;
133                 default:
134                     buf.append(c);
135                     break;
136             }
137         }
138         return buf.toString();
139     }
140 }
141
Popular Tags