package forum;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.tree.TreePath;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.renderer.AbstractRenderer;
import org.jdesktop.swingx.renderer.ComponentProvider;
import org.jdesktop.swingx.renderer.IconValue;
import org.jdesktop.swingx.renderer.LabelProvider;
import org.jdesktop.swingx.renderer.MappedValue;
import org.jdesktop.swingx.renderer.StringValue;
import org.jdesktop.swingx.renderer.TableCellContext;
@SuppressWarnings("serial")
public class MyDefaultTableRenderer extends AbstractRenderer implements
TableCellRenderer {
private TableCellContext cellContext;
/**
* Instantiates a default table renderer with the default component
* provider.
*
* @see #DefaultTableRenderer(ComponentProvider)
*/
public MyDefaultTableRenderer() {
this((ComponentProvider<?>) null);
}
/**
* Instantiates a default table renderer with the given component provider.
* If the controller is null, creates and uses a default. The default
* provider is of type <code>LabelProvider</code>.
*
* @param componentProvider
* the provider of the configured component to use for cell
* rendering
*/
public MyDefaultTableRenderer(ComponentProvider<?> componentProvider) {
super(componentProvider);
this.cellContext = new TableCellContext();
}
/**
* Instantiates a default table renderer with a default component provider
* using the given converter.
*
* @param converter
* the converter to use for mapping the content value to a String
* representation.
*
* @see #DefaultTableRenderer(ComponentProvider)
*/
public MyDefaultTableRenderer(StringValue converter) {
this(new LabelProvider(converter));
}
/**
* Instantiates a default table renderer with a default component provider
* using the given converter and horizontal alignment.
*
* @param converter
* the converter to use for mapping the content value to a String
* representation.
*
* @see #DefaultTableRenderer(ComponentProvider)
*/
public MyDefaultTableRenderer(StringValue converter, int alignment) {
this(new LabelProvider(converter, alignment));
}
/**
* Intantiates a default table renderer with default component provider
* using both converters.
*
* @param stringValue
* the converter to use for the string representation
* @param iconValue
* the converter to use for the icon representation
*/
public MyDefaultTableRenderer(StringValue stringValue, IconValue iconValue) {
this(new MappedValue(stringValue, iconValue));
}
/**
* Intantiates a default table renderer with default component provider
* using both converters and the given alignment.
*
* @param stringValue
* the converter to use for the string representation
* @param iconValue
* the converter to use for the icon representation
* @param alignment
* the rendering component's horizontal alignment
*/
public MyDefaultTableRenderer(StringValue stringValue, IconValue iconValue,
int alignment) {
this(new MappedValue(stringValue, iconValue), alignment);
}
// -------------- implements javax.swing.table.TableCellRenderer
/**
*
* Returns a configured component, appropriate to render the given list
* cell.
* <p>
*
* Note: The component's name is set to "Table.cellRenderer" for the sake of
* Synth-based LAFs.
*
* @param table
* the <code>JTable</code>
* @param value
* the value to assign to the cell at <code>[row, column]</code>
* @param isSelected
* true if cell is selected
* @param hasFocus
* true if cell has focus
* @param row
* the row of the cell to render
* @param column
* the column of the cell to render
* @return the default table cell renderer
*/
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
cellContext.installContext(table, value, row, column, isSelected,
hasFocus, true, true);
Component comp = componentController.getRendererComponent(cellContext);
// fix issue #1040-swingx: memory leak if value not released
cellContext.replaceValue(null);
if (table instanceof JXTreeTable) {
JXTreeTable xtable = (JXTreeTable) table;
// xtable.getModel().getValueAt(row, 1);
TreePath path = xtable.getPathForRow(row);
Object node = path.getLastPathComponent();
if (node instanceof ChildNode) {
ChildNode cnode = (ChildNode) node;
if (cnode.getData().length == 3) {
Object v1 = cnode.getData()[1];
Object v2 = cnode.getData()[2];
if (v1 != null && v2 != null) {
String str1 = ((String) v1).toLowerCase();
String str2 = ((String) v2).toLowerCase();
if (str1.equals("nein") && str2.equals("x")) {
comp.setBackground(Color.RED.brighter().brighter());
comp.setForeground(Color.BLACK);
} else if (str1.equals("ja") && str2.equals("x")) {
comp.setBackground(Color.GREEN.darker());
comp.setForeground(Color.BLACK);
}
}
}
}
}
return comp;
}
/**
* {@inheritDoc}
*/
@Override
protected ComponentProvider<?> createDefaultComponentProvider() {
return new LabelProvider();
}
}