1 16 package org.apache.log4j.chainsaw; 17 18 import java.awt.BorderLayout ; 19 import java.text.MessageFormat ; 20 import java.util.Date ; 21 import javax.swing.BorderFactory ; 22 import javax.swing.JEditorPane ; 23 import javax.swing.JPanel ; 24 import javax.swing.JScrollPane ; 25 import javax.swing.JTable ; 26 import javax.swing.ListSelectionModel ; 27 import javax.swing.event.ListSelectionEvent ; 28 import javax.swing.event.ListSelectionListener ; 29 import org.apache.log4j.Logger; 30 31 36 class DetailPanel 37 extends JPanel 38 implements ListSelectionListener 39 { 40 41 private static final Logger LOG = 42 Logger.getLogger(DetailPanel.class); 43 44 45 private static final MessageFormat FORMATTER = new MessageFormat ( 46 "<b>Time:</b> <code>{0,time,medium}</code>" + 47 " <b>Priority:</b> <code>{1}</code>" + 48 " <b>Thread:</b> <code>{2}</code>" + 49 " <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 58 private final MyTableModel mModel; 59 60 private final JEditorPane mDetails; 61 62 68 DetailPanel(JTable aTable, final MyTableModel aModel) { 69 mModel = aModel; 70 setLayout(new BorderLayout ()); 71 setBorder(BorderFactory.createTitledBorder("Details: ")); 72 73 mDetails = new JEditorPane (); 74 mDetails.setEditable(false); 75 mDetails.setContentType("text/html"); 76 add(new JScrollPane (mDetails), BorderLayout.CENTER); 77 78 final ListSelectionModel rowSM = aTable.getSelectionModel(); 79 rowSM.addListSelectionListener(this); 80 } 81 82 83 public void valueChanged(ListSelectionEvent aEvent) { 84 if (aEvent.getValueIsAdjusting()) { 86 return; 87 } 88 89 final ListSelectionModel lsm = (ListSelectionModel ) 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 [] args = 96 { 97 new Date (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 115 121 private static String getThrowableStrRep(EventDetails aEvent) { 122 final String [] strs = aEvent.getThrowableStrRep(); 123 if (strs == null) { 124 return null; 125 } 126 127 final StringBuffer sb = new StringBuffer (); 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 141 private String escape(String aStr) { 142 if (aStr == null) { 143 return null; 144 } 145 146 final StringBuffer buf = new StringBuffer (); 147 for (int i = 0; i < aStr.length(); i++) { 148 char c = aStr.charAt(i); 149 switch (c) { 150 case '<': 151 buf.append("<"); 152 break; 153 case '>': 154 buf.append(">"); 155 break; 156 case '\"': 157 buf.append("""); 158 break; 159 case '&': 160 buf.append("&"); 161 break; 162 default: 163 buf.append(c); 164 break; 165 } 166 } 167 return buf.toString(); 168 } 169 } 170 | Popular Tags |