001: /*
002: * Copyright (C) Jahia Ltd. All rights reserved.
003: *
004: * This software is published under the terms of the Jahia Open Software
005: * License version 1.1, a copy of which has been included with this
006: * distribution in the LICENSE.txt file.
007: */
008: package org.jahia.sqlprofiler.gui;
009:
010: import javax.swing.JPanel;
011: import javax.swing.event.ListSelectionListener;
012: import org.apache.log4j.Category;
013: import java.text.MessageFormat;
014: import javax.swing.JEditorPane;
015: import javax.swing.JTable;
016: import java.awt.BorderLayout;
017: import javax.swing.BorderFactory;
018: import javax.swing.JScrollPane;
019: import javax.swing.ListSelectionModel;
020: import javax.swing.event.ListSelectionEvent;
021: import org.jahia.sqlprofiler.QueryEntry;
022: import java.util.Date;
023:
024: /**
025: * <p>Title: A panel that displays more details about the currently selected
026: * SQL Statement</p>
027: * <p>Description: </p>
028: * <p>Copyright: Copyright (c) 2003</p>
029: * <p>Company: Jahia Ltd</p>
030: * @author Serge Huber
031: * @version 1.0
032: */
033:
034: class ProfileDetailsPanel extends JPanel implements
035: ListSelectionListener {
036:
037: private static final Category LOG = Category
038: .getInstance(ProfileDetailsPanel.class);
039:
040: /** used to format the logging event **/
041: private static final MessageFormat FORMATTER = new MessageFormat(
042: "<b>Time:</b> <code>{0,time,medium}</code>"
043: + " <b>Elapsed time:</b> <code>{1,time,medium}</code>"
044: + " <b>Category:</b> <code>{2}</code>"
045: + " <b>Connection ID:</b> <code>{3}</code>"
046: + "<br><b>Prepared SQL:</b><br>" + "{4}<br>"
047: + "<b>SQL Statement:</b><br>" + "{5}");
048:
049: /** the model for the data to render **/
050: private final ProfileStatementTableModel mModel;
051: /** pane for rendering detail **/
052: private final JEditorPane mDetails;
053:
054: /**
055: * Creates a new <code>DetailPanel</code> instance.
056: *
057: * @param aTable the table to listen for selections on
058: * @param aModel the model backing the table
059: */
060: ProfileDetailsPanel(JTable aTable,
061: final ProfileStatementTableModel aModel) {
062: mModel = aModel;
063: setLayout(new BorderLayout());
064: setBorder(BorderFactory.createTitledBorder("Details: "));
065:
066: mDetails = new JEditorPane();
067: mDetails.setEditable(false);
068: mDetails.setContentType("text/html");
069: add(new JScrollPane(mDetails), BorderLayout.CENTER);
070:
071: final ListSelectionModel rowSM = aTable.getSelectionModel();
072: rowSM.addListSelectionListener(this );
073: }
074:
075: /** @see ListSelectionListener **/
076: public void valueChanged(ListSelectionEvent aEvent) {
077: //Ignore extra messages.
078: if (aEvent.getValueIsAdjusting()) {
079: return;
080: }
081:
082: final ListSelectionModel lsm = (ListSelectionModel) aEvent
083: .getSource();
084: if (lsm.isSelectionEmpty()) {
085: mDetails.setText("Nothing selected");
086: } else {
087: final int selectedRow = lsm.getMinSelectionIndex();
088: final QueryEntry queryEntry = mModel
089: .getStatementDetails(selectedRow);
090: final Object[] args = { new Date(queryEntry.getTime()),
091: new Long(queryEntry.getElapsedTime()),
092: escape(queryEntry.getCategory()),
093: escape(queryEntry.getConnectionID()),
094: escape(queryEntry.getPreparedSQL()),
095: escape(queryEntry.getSqlStatement()) };
096: mDetails.setText(FORMATTER.format(args));
097: mDetails.setCaretPosition(0);
098: }
099: }
100:
101: ////////////////////////////////////////////////////////////////////////////
102: // Private methods
103: ////////////////////////////////////////////////////////////////////////////
104:
105: /**
106: * Escape <, > & and " as their entities. It is very
107: * dumb about & handling.
108: * @param aStr the String to escape.
109: * @return the escaped String
110: */
111: private String escape(String aStr) {
112: if (aStr == null) {
113: return null;
114: }
115:
116: final StringBuffer buf = new StringBuffer();
117: for (int i = 0; i < aStr.length(); i++) {
118: char c = aStr.charAt(i);
119: switch (c) {
120: case '<':
121: buf.append("<");
122: break;
123: case '>':
124: buf.append(">");
125: break;
126: case '\"':
127: buf.append(""");
128: break;
129: case '&':
130: buf.append("&");
131: break;
132: default:
133: buf.append(c);
134: break;
135: }
136: }
137: return buf.toString();
138: }
139: }
|