View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight.gui.renderers;
25  
26  import java.awt.Color;
27  import java.awt.Component;
28  import java.awt.Graphics;
29  
30  import javax.swing.Icon;
31  import javax.swing.JComponent;
32  import javax.swing.JTable;
33  import javax.swing.UIManager;
34  import javax.swing.border.LineBorder;
35  import javax.swing.table.DefaultTableCellRenderer;
36  
37  import com.mindtree.techworks.insight.InsightConstants;
38  import com.mindtree.techworks.insight.model.LogEventTableModel;
39  import com.mindtree.techworks.insight.receiver.RuntimeNamespaceContainer;
40  import com.mindtree.techworks.insight.spi.LogEvent;
41  
42  /**
43  *
44  * The <code>LogEventTableCellRenderer</code> class is the customized renderer of the cells
45  * in the JTable that uses this renderer.
46  * This Renderer primarily support rendering the org.apache.log4j.spi.LoggingEvent
47  * class instances 
48  *
49  * @author  Regunath B
50  * @version 1.0, 04/10/25
51  * @see     org.apache.log4j.spi.LoggingEvent
52  */
53  
54  public class LogEventTableCellRenderer extends DefaultTableCellRenderer implements Icon {
55  	
56  	/**
57  	 * Used for object serialization
58  	 */
59  	private static final long serialVersionUID = 4519016975132589236L;
60  	
61  	/*
62  	 * Useful constants to hold the legend Color values for events   
63  	 */
64  	private static final Color INFO_COLOR = Color.white;
65  	private static final Color DEBUG_COLOR = Color.green;
66  	private static final Color WARN_COLOR = Color.yellow;
67  	private static final Color ERROR_COLOR = Color.orange;
68  	private static final Color FATAL_COLOR = Color.red;
69  	
70  	/*
71  	 * Useful constants for the priority text start characters
72  	 */
73  	private static final char INFO_CHAR = 'I';
74  	private static final char DEBUG_CHAR = 'D';
75  	private static final char WARN_CHAR = 'W';
76  	private static final char ERROR_CHAR = 'E';
77  	private static final char FATAL_CHAR = 'F';
78  
79  	/*
80  	 * Useful constants to identify legend Icon width and height
81  	 */
82  	private static final int LEGEND_WIDTH = 8;
83  	private static final int LEGEND_HEIGHT = 8;
84  	
85  	/*
86  	 * Place holder for the LogEvent rendered by this renderer
87  	 */
88  	private Object value;
89  	
90  	/*
91  	 * Identifies the selected row
92  	 */
93      private int selectedRow = -1;
94  	
95  	/**
96  	 * Overriden superclass method
97  	 * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
98  	 */
99  	public Component getTableCellRendererComponent(JTable table,
100             Object value,
101             boolean isSelected,
102             boolean hasFocus,
103             int row,
104             int column) {
105 		this.value = value;
106 		int columnIdentifier = ((Integer)table.getColumnModel().getColumn(column).getIdentifier()).intValue();
107 		switch(columnIdentifier) {
108 			case LogEventTableModel.PRIORITY_INDEX:
109 				this.setIcon(this);					
110 				break;
111 			default :
112 				this.setIcon(null);
113 				break;
114 		}
115 		JComponent renderingComponent = (JComponent)super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
116 		if (isSelected) {
117 			if (selectedRow >= 0) {
118 				JComponent oldRenderingComponent = (JComponent)super.getTableCellRendererComponent(table,value,isSelected,hasFocus,selectedRow,column);
119 				oldRenderingComponent.setBorder(DefaultTableCellRenderer.noFocusBorder);
120 			}
121 			selectedRow = row;
122 			// Use darker shade of the PLAF row highlight color
123 			renderingComponent.setBorder( new TableCellBorder(UIManager.getDefaults().getColor(InsightConstants.TEXT_HIGHLIGHT).darker(),
124 					column==0, column==table.getModel().getColumnCount() - 1));
125 		}
126 		LogEvent event = ((LogEventTableModel)table.getModel()).getLoggingEvent(row);
127 		Color namespaceColor = RuntimeNamespaceContainer.getNamespaceColor(event.getNamespace());
128 		renderingComponent.setBackground(namespaceColor);
129 		return renderingComponent;
130 	}
131 	
132 	/**
133 	 * Icon interface method implementation
134 	 * @see javax.swing.Icon#getIconHeight()
135 	 */
136 	public int getIconHeight() {
137 		return LEGEND_HEIGHT;
138 	}
139 
140 	/**
141 	 * Icon interface method implementation
142 	 * @see javax.swing.Icon#getIconWidth()
143 	 */
144 	public int getIconWidth() {
145 		return LEGEND_WIDTH;
146 	}
147 	
148 	/*
149 	 * Icon interface method implementation
150 	 * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics, int, int)
151 	 */
152 	public void paintIcon(Component c, Graphics g, int x, int y) {
153 		char startChar = this.value.toString().charAt(0);
154 		switch(startChar) {
155 			case INFO_CHAR:
156 				g.setColor(INFO_COLOR);				
157 				break;
158 			case DEBUG_CHAR:
159 				g.setColor(DEBUG_COLOR);				
160 				break;
161 			case WARN_CHAR:
162 				g.setColor(WARN_COLOR);				
163 				break;
164 			case ERROR_CHAR:
165 				g.setColor(ERROR_COLOR);			
166 				break;
167 			case FATAL_CHAR:
168 				g.setColor(FATAL_COLOR);
169 				break;
170 		} 
171 		g.fill3DRect(x,y,LEGEND_WIDTH,LEGEND_HEIGHT,true);
172 	}
173 	
174 	/**
175 	 * Helper class used to paint the border around each cell. Paints border
176 	 * such that the entire row appears highlighted.
177 	 */
178     private class TableCellBorder extends LineBorder {
179 
180 		/**
181 		 * Used for object serialization
182 		 */
183 		private static final long serialVersionUID = 1582741766574751785L;
184 		
185 		private Color color;
186 		private boolean isLeftBoundary;
187 		private boolean isRightBoundary;
188 
189 		public TableCellBorder(Color color, boolean isLeftBoundary, boolean isRightBoundary) {
190 			super(color);
191 			this.color = color;
192 			this.isLeftBoundary = isLeftBoundary;
193 			this.isRightBoundary = isRightBoundary;
194 		}
195 
196 		public void paintBorder(Component c,
197                         Graphics g,
198                         int x,
199                         int y,
200                         int width,
201                         int height) {
202 			g.setColor(color);
203 			g.drawLine(0,0,width,0);
204 			g.drawLine(0,height - 1,width,height - 1);
205 			if (isLeftBoundary) {
206 				g.drawLine(0,0,0,height);
207 			} else if (isRightBoundary) {
208 				g.drawLine(width - 1,0,width - 1,height);
209 			}
210  		}
211 	}	
212 
213 }