package com.onaro.util.jfc.tables.renderers; import com.onaro.sanscreen.client.view.tabular.value.ValueDecoration; import com.onaro.util.enumeration.EnumPresentation; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.table.DefaultTableCellRenderer; import java.awt.Color; public class SimpleTableCellRenderer extends DefaultTableCellRenderer { private static final long serialVersionUID = 1L; static final Logger logger = LogManager.getLogger(SimpleTableCellRenderer.class); private Object value; public SimpleTableCellRenderer() { setVerticalAlignment( JLabel.CENTER ); } public SimpleTableCellRenderer(int horizontalAlignment) { this(); setHorizontalAlignment(horizontalAlignment); } protected void setValue(Object value) { //keep the value so we can identify if it's a decoration this.value = value; decorateCell(value); super.setValue(value); } private void decorateCell(Object value) { setIcon(null); if( value instanceof EnumPresentation.Attributes ) { EnumPresentation.Attributes attr = (EnumPresentation.Attributes)value; setEnumIcon(attr); }else if(value instanceof ValueDecoration) { ValueDecoration decoration = (ValueDecoration) value; //decorate the cell according to the value if(decoration.getIcon() != null) setIcon(decoration.getIcon()); if(decoration.getBackground() != null) super.setBackground(decoration.getBackground()); if(decoration.getForeground() != null) super.setForeground(decoration.getForeground()); } } protected void setEnumIcon(EnumPresentation.Attributes attr) { if( attr.smallIcon != null ) { ImageIcon smallIcon = attr.smallIcon; setIcon(smallIcon); } } //this works around the table changing the color back according to the selection of the row, ignoring the decoration public void setBackground(Color c) { if (value instanceof ValueDecoration){ ValueDecoration decoration = (ValueDecoration) value; if(decoration.getBackground() != null){ if(logger.isDebugEnabled()) logger.debug("ignore the setting of background to keep the background of the ValueDecoration"); //$NON-NLS-1$ return; } } super.setBackground(c); } //this works around the table changing the color back according to the selection of the row, ignoring the decoration public void setForeground(Color c) { if (value instanceof ValueDecoration){ ValueDecoration decoration = (ValueDecoration) value; if(decoration.getForeground() != null){ if(logger.isDebugEnabled()) logger.debug("ignore the setting of foreground to keep the foreground of the ValueDecoration"); //$NON-NLS-1$ return; } } super.setForeground(c); } }