/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2005, by :
* Corporate:
* EADS Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
*
* $Id: LoggingTableRenderer.java,v 1.5 2009/01/20 12:41:46 cazenave Exp $
*
* Changes
* -------
* 5 sept. 06 : Initial public release (CC);
*
*/
package simtools.logging.ui;
import java.awt.Color;
import java.awt.Component;
import java.util.logging.Level;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;
import simtools.logging.SimtoolsLevel;
public class LoggingTableRenderer extends DefaultTableCellRenderer {
protected Color[] backgroundColors;
protected Color[] foregroundColors;
/**
* Create a new renderer for a model
* @param tableModel the model
*/
public LoggingTableRenderer(AbstractLoggingTableModel tableModel) {
loadColors();
}
/**
* Configure background and foregroud colors versus logging level
*/
protected void loadColors() {
// check assumptions
int reducedRange=Level.SEVERE.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR;
if(Level.FINEST.intValue()<0){
throw new IllegalArgumentException("Unexpected log level mapping : FINEST="+Level.FINEST.intValue());
}
if(reducedRange<0 || reducedRange > 200){
throw new IllegalArgumentException("Unexpected log level mapping : SEVERE="+Level.SEVERE.intValue());
}
backgroundColors=new Color[reducedRange+1];
foregroundColors=new Color[reducedRange+1];
foregroundColors[Level.SEVERE.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.WHITE;
backgroundColors[Level.SEVERE.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.RED;
foregroundColors[Level.WARNING.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.BLACK;
backgroundColors[Level.WARNING.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.YELLOW;
foregroundColors[SimtoolsLevel.USER_WARNING.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.BLACK;
backgroundColors[SimtoolsLevel.USER_WARNING.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.ORANGE;
foregroundColors[Level.INFO.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.BLACK;
backgroundColors[Level.INFO.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.WHITE;
foregroundColors[Level.CONFIG.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.BLACK;
backgroundColors[Level.CONFIG.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.GREEN;
foregroundColors[Level.FINE.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.BLACK;
backgroundColors[Level.FINE.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.CYAN;
foregroundColors[Level.FINER.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.BLACK;
backgroundColors[Level.FINER.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.getHSBColor(0.5f, 0.2f, 1.0f);
foregroundColors[Level.FINEST.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.BLACK;
backgroundColors[Level.FINEST.intValue()/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR] = Color.LIGHT_GRAY;
}
/*
* (non-Javadoc)
*
* @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
* java.lang.Object, boolean, boolean, int, int)
*/
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
super.setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
//Retrieve the level of the log, in the table.
TableModel model = table.getModel();
Object object = model.getValueAt(row, 2);
String level = "0";
if(object != null){
level = object.toString();
}
//we have to get the int value of this level.
int l=-1;
//Try to retrieve the int level in the level names array.
for(int i = 0; i < AbstractLoggingTableModel.levelNames.length;i++){
if(level != null && level.equals(AbstractLoggingTableModel.levelNames[i])){
l = i;
}
}
//If not found, it means level is the real level, as an int.
if(l == -1){
//try to get it.
try{
l = Integer.parseInt(level)/AbstractLoggingTableModel.LEVEL_SCALE_FACTOR;
}
catch(NumberFormatException nfe){
}
}
Color b=null;
Color f=null;
if(l>=0 && l<foregroundColors.length){
b=backgroundColors[l];
f=foregroundColors[l];
}
if (b != null) {
super.setBackground(b);
} else {
super.setBackground(table.getBackground());
}
if (f != null) {
super.setForeground(f);
} else {
super.setForeground(table.getForeground());
}
}
setFont(table.getFont());
if (hasFocus) {
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
} else {
setBorder(noFocusBorder);
}
setValue(value);
return this;
}
}