001: /*
002: * Copyright (C) The Apache Software Foundation. All rights reserved.
003: *
004: * This software is published under the terms of the Apache Software
005: * License version 1.1, a copy of which has been included with this
006: * distribution in the APACHE.txt file. */
007: package org.jahia.sqlprofiler.gui;
008:
009: import java.awt.BorderLayout;
010: import java.text.MessageFormat;
011: import java.util.Date;
012: import javax.swing.BorderFactory;
013: import javax.swing.JEditorPane;
014: import javax.swing.JPanel;
015: import javax.swing.JScrollPane;
016: import javax.swing.JTable;
017: import javax.swing.ListSelectionModel;
018: import javax.swing.event.ListSelectionEvent;
019: import javax.swing.event.ListSelectionListener;
020: import org.apache.log4j.Category;
021:
022: /**
023: * A panel for showing a stack trace.
024: *
025: * @author <a href="mailto:oliver@puppycrawl.com">Oliver Burn</a>
026: */
027: class DetailPanel extends JPanel implements ListSelectionListener {
028: /** used to log events **/
029: private static final Category LOG = Category
030: .getInstance(DetailPanel.class);
031:
032: /** used to format the logging event **/
033: private static final MessageFormat FORMATTER = new MessageFormat(
034: "<b>Time:</b> <code>{0,time,medium}</code>"
035: + " <b>Priority:</b> <code>{1}</code>"
036: + " <b>Thread:</b> <code>{2}</code>"
037: + " <b>NDC:</b> <code>{3}</code>"
038: + "<br><b>Category:</b> <code>{4}</code>"
039: + "<br><b>Location:</b> <code>{5}</code>"
040: + "<br><b>Message:</b>" + "{6}<br>"
041: + "<b>Throwable:</b>" + "<pre>{7}</pre>");
042:
043: /** the model for the data to render **/
044: private final LoggerTableModel mModel;
045: /** pane for rendering detail **/
046: private final JEditorPane mDetails;
047:
048: /**
049: * Creates a new <code>DetailPanel</code> instance.
050: *
051: * @param aTable the table to listen for selections on
052: * @param aModel the model backing the table
053: */
054: DetailPanel(JTable aTable, final LoggerTableModel aModel) {
055: mModel = aModel;
056: setLayout(new BorderLayout());
057: setBorder(BorderFactory.createTitledBorder("Details: "));
058:
059: mDetails = new JEditorPane();
060: mDetails.setEditable(false);
061: mDetails.setContentType("text/html");
062: add(new JScrollPane(mDetails), BorderLayout.CENTER);
063:
064: final ListSelectionModel rowSM = aTable.getSelectionModel();
065: rowSM.addListSelectionListener(this );
066: }
067:
068: /** @see ListSelectionListener **/
069: public void valueChanged(ListSelectionEvent aEvent) {
070: //Ignore extra messages.
071: if (aEvent.getValueIsAdjusting()) {
072: return;
073: }
074:
075: final ListSelectionModel lsm = (ListSelectionModel) aEvent
076: .getSource();
077: if (lsm.isSelectionEmpty()) {
078: mDetails.setText("Nothing selected");
079: } else {
080: final int selectedRow = lsm.getMinSelectionIndex();
081: final EventDetails e = mModel.getEventDetails(selectedRow);
082: final Object[] args = { new Date(e.getTimeStamp()),
083: e.getPriority(), escape(e.getThreadName()),
084: escape(e.getNDC()), escape(e.getCategoryName()),
085: escape(e.getLocationDetails()),
086: escape(e.getMessage()),
087: escape(getThrowableStrRep(e)) };
088: mDetails.setText(FORMATTER.format(args));
089: mDetails.setCaretPosition(0);
090: }
091: }
092:
093: ////////////////////////////////////////////////////////////////////////////
094: // Private methods
095: ////////////////////////////////////////////////////////////////////////////
096:
097: /**
098: * Returns a string representation of a throwable.
099: *
100: * @param aEvent contains the throwable information
101: * @return a <code>String</code> value
102: */
103: private static String getThrowableStrRep(EventDetails aEvent) {
104: final String[] strs = aEvent.getThrowableStrRep();
105: if (strs == null) {
106: return null;
107: }
108:
109: final StringBuffer sb = new StringBuffer();
110: for (int i = 0; i < strs.length; i++) {
111: sb.append(strs[i]).append("\n");
112: }
113:
114: return sb.toString();
115: }
116:
117: /**
118: * Escape <, > & and " as their entities. It is very
119: * dumb about & handling.
120: * @param aStr the String to escape.
121: * @return the escaped String
122: */
123: private String escape(String aStr) {
124: if (aStr == null) {
125: return null;
126: }
127:
128: final StringBuffer buf = new StringBuffer();
129: for (int i = 0; i < aStr.length(); i++) {
130: char c = aStr.charAt(i);
131: switch (c) {
132: case '<':
133: buf.append("<");
134: break;
135: case '>':
136: buf.append(">");
137: break;
138: case '\"':
139: buf.append(""");
140: break;
141: case '&':
142: buf.append("&");
143: break;
144: default:
145: buf.append(c);
146: break;
147: }
148: }
149: return buf.toString();
150: }
151: }
|