<MyColourRendererPlugin Project>/src/org/myorg/mycolourrendererplugin/ColouringRenderer.java |
package org.myorg.mycolourrendererplugin;
import com.im.commons.ui.UIUtilities;
import com.im.ijc.core.api.renderers.IJCStyledPrintRenderer;
import com.im.ijc.core.api.renderers.IJCWidgetTextRenderer;
import com.im.ijc.core.api.renderers.IJCWidgetTextRenderer.TextFormatInfo;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class ColouringRenderer extends DefaultTableCellRenderer implements IJCWidgetTextRenderer, IJCWidgetTextRenderer.TextFormatInfo {
public static final String PROP_SPLIT_VALUE = "splitValue";
public static final String PROP_LOWER_COLOR = "lowerColor";
public static final String PROP_HIGHER_COLOR = "higherColor";
private float splitValue;
private Color lowerColor;
private Color higherColor;
public ColouringRenderer() {
splitValue = 0f;
lowerColor = new Color(200, 255, 200);
higherColor = new Color(255, 200, 200);
}
@Override
public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setValue(value);
if (value instanceof Number) {
Number n = (Number) value;
boolean under = (n.floatValue() < splitValue);
Color bg = (under ? lowerColor : higherColor);
if (isSelected) {
bg = UIUtilities.mixColors(comp.getBackground(), bg, 0.5f);
}
setBackground(bg);
}
return comp;
}
public void setSplitValue(float splitValue) {
float oldValue = this.splitValue;
this.splitValue = splitValue;
firePropertyChange(PROP_SPLIT_VALUE, oldValue, splitValue);
}
public float getSplitValue() {
return splitValue;
}
public void setLowerColor(Color lowerColor) {
if (lowerColor == null) {
throw new IllegalArgumentException();
}
Color oldValue = this.lowerColor;
this.lowerColor = lowerColor;
firePropertyChange(PROP_LOWER_COLOR, oldValue, lowerColor);
}
public Color getLowerColor() {
return lowerColor;
}
public void setHigherColor(Color higherColor) {
if (higherColor == null) {
throw new IllegalArgumentException();
}
Color oldValue = this.higherColor;
this.higherColor = higherColor;
firePropertyChange(PROP_HIGHER_COLOR, oldValue, higherColor);
}
public Color getHigherColor() {
return higherColor;
}
@Override
public TextFormatInfo getTextFormatInfo(Object value) {
setValue(value);
return this;
}
@Override
public String getValueAsText() {
return getText();
}
@Override
public boolean getLineWrap() {
return false;
}
@Override
public boolean isMultiLine() {
return false;
}
@Override
public IJCStyledPrintRenderer getIJCStyledPrintRenderer() {
return null;
}
}