View Javadoc
1   /**
2    * The contents of this file are subject to the Mozilla Public License Version 1.1
3    * (the "License"); you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
5    * Software distributed under the License is distributed on an "AS IS" basis,
6    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7    * specific language governing rights and limitations under the License.
8    *
9    * The Original Code is ""  Description:
10   * ""
11   *
12   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   * 2001.  All Rights Reserved.
14   *
15   * Contributor(s): ______________________________________.
16   *
17   * Alternatively, the contents of this file may be used under the terms of the
18   * GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
19   * applicable instead of those above.  If you wish to allow use of your version of this
20   * file only under the terms of the GPL and not to allow others to use your version
21   * of this file under the MPL, indicate your decision by deleting  the provisions above
22   * and replace  them with the notice and other provisions required by the GPL License.
23   * If you do not delete the provisions above, a recipient may use your version of
24   * this file under either the MPL or the GPL.
25   */
26  package ca.uhn.hl7v2.testpanel.ui;
27  
28  import java.awt.Color;
29  import java.awt.Component;
30  import java.text.SimpleDateFormat;
31  import java.util.Date;
32  
33  import javax.swing.JScrollBar;
34  import javax.swing.JScrollPane;
35  import javax.swing.JTable;
36  import javax.swing.SwingUtilities;
37  import javax.swing.table.DefaultTableCellRenderer;
38  import javax.swing.table.DefaultTableModel;
39  
40  import org.apache.log4j.Level;
41  import org.apache.log4j.spi.LoggingEvent;
42  
43  import ca.uhn.hl7v2.testpanel.util.SwingLogAppender;
44  import ca.uhn.hl7v2.testpanel.util.SwingLogAppender.ILogListener;
45  
46  public class LogTable extends JScrollPane {
47  	private static final Color CLR_NORMAL = Color.black;
48  	private static final Color CLR_ERROR = new Color(1.0f, 0.0f, 0.0f);
49  	private static final Color CLR_WARN = new Color(0.5f, 0.5f, 0.0f);
50  
51  	private JTable myLogTable;
52  	private Model myModel;
53  
54  	public LogTable() {
55  		this("");
56  	}
57  	
58  	public LogTable(String theNdc) {
59  		
60  		myLogTable = new JTable();
61          myLogTable.setFillsViewportHeight(true);
62          myLogTable.setShowHorizontalLines(false);
63          myLogTable.setShowVerticalLines(false);
64          
65          setViewportView(myLogTable);
66  		
67          myModel = new Model();
68          myLogTable.setModel(myModel);
69          myLogTable.setDefaultRenderer(Object.class, new Renderer());
70          
71          myLogTable.getColumnModel().getColumn(0).setMaxWidth(60);
72          myLogTable.getColumnModel().getColumn(0).setPreferredWidth(60);
73          myLogTable.getColumnModel().getColumn(1).setMaxWidth(120);
74          myLogTable.getColumnModel().getColumn(1).setPreferredWidth(120);
75          
76          SwingLogAppender.addListener(theNdc, myModel);
77  	}
78  
79  	private class Renderer extends DefaultTableCellRenderer
80  	{
81  
82  
83  		/**
84  		 * {@inheritDoc}
85  		 */
86  		@Override
87  		public Component getTableCellRendererComponent(JTable theTable, Object theValue, boolean theIsSelected, boolean theHasFocus, int theRow, int theColumn) {
88  			Component retVal = super.getTableCellRendererComponent(theTable, theValue, theIsSelected, theHasFocus, theRow, theColumn);
89  			
90  			Level level = (Level) myModel.getValueAt(theRow, 0);
91  			if (level == Level.WARN) {
92  				retVal.setForeground(CLR_WARN);
93  			} else if (level == Level.ERROR || level == Level.FATAL) {
94  				retVal.setForeground(CLR_ERROR);
95  			} else {
96  				retVal.setForeground(CLR_NORMAL);
97  			}
98  			
99  			if (theColumn == 0) {
100 				String levelName;
101 				if (level == Level.INFO) {
102 					levelName = "INFO";
103 				} else if (level == Level.WARN) {
104 					levelName = "WARN";
105 				} else if (level == Level.ERROR) {
106 					levelName = "ERROR";
107 				} else if (level == Level.FATAL) {
108 					levelName = "FATAL";
109 				} else if (level == Level.DEBUG) {
110 					levelName = "DEBUG";
111 				} else if (level == Level.ALL) {
112 					levelName = "ALL";
113 				} else if (level == Level.TRACE) {
114 					levelName = "TRACE";
115 				} else {
116 					levelName = "UNKNOWN";
117 				}
118 				setText(levelName);
119 			}
120 			
121 			return retVal;
122 		}
123 		
124 	}
125 	
126 	private class Model extends DefaultTableModel implements ILogListener
127 	{
128 		private SimpleDateFormat ourDateFmt = new SimpleDateFormat("HH:mm:ss.SSS");
129 		
130 		public Model() {
131 			addColumn("Level");
132 			addColumn("Time");
133 			addColumn("Log");
134 		}
135 
136 		public void handle(LoggingEvent theEvent, String theFormattedLine) {
137 			
138 			String date = ourDateFmt.format(new Date(theEvent.getTimeStamp()));
139 			Level level = theEvent.getLevel();
140 			super.addRow(new Object[] {level, date, theEvent.getMessage()});
141 			
142 			SwingUtilities.invokeLater(new Runnable() {
143 
144 				public void run() {
145 					JScrollBar vsb = LogTable.this.getVerticalScrollBar();
146 					vsb.setValue(vsb.getMaximum());
147 				}});
148 		}
149 		
150 	}
151 	
152 }