89 lines
2.5 KiB
Java
89 lines
2.5 KiB
Java
package nt.UI.display;
|
|
|
|
import java.awt.Color;
|
|
import java.awt.Component;
|
|
import java.net.URL;
|
|
import java.util.HashMap;
|
|
|
|
import javax.swing.Icon;
|
|
import javax.swing.ImageIcon;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JTable;
|
|
import javax.swing.table.TableCellRenderer;
|
|
|
|
import nt.UI.util.IconResizer;
|
|
import nt.UI.util.Icons;
|
|
|
|
public class ObjTableCellRenderer implements TableCellRenderer{
|
|
|
|
private HashMap<Class<?>, Icon> mappedIcons = new HashMap<>();
|
|
private Color bgEven = new Color(Integer.decode("#e8e8e8"));
|
|
private Color bgOdd = Color.WHITE;
|
|
private Color bgSelect = new Color(Integer.decode("#5e87ed"));
|
|
|
|
public ObjTableCellRenderer() {
|
|
putIcon(String.class, Icons.STRING_SYMBOL_URL);
|
|
putIcon(Long.class, Icons.NUMBER_SYMBOL_URL);
|
|
putIcon(Integer.class, Icons.NUMBER_SYMBOL_URL);
|
|
putIcon(Short.class, Icons.NUMBER_SYMBOL_URL);
|
|
putIcon(Float.class, Icons.NUMBER_SYMBOL_URL);
|
|
putIcon(Double.class, Icons.NUMBER_SYMBOL_URL);
|
|
putIcon(Object.class, Icons.OBJECT_SYMBOL_URL);
|
|
}
|
|
|
|
@Override
|
|
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
|
|
int row, int column) {
|
|
JLabel label = new JLabel(value == null? "" : value.toString());;
|
|
if (column == 0) {
|
|
Class<?> clazz = value.getClass();
|
|
if(clazz == String.class) {
|
|
try {
|
|
clazz = Class.forName(value.toString());
|
|
} catch (ClassNotFoundException e) {
|
|
clazz = value.getClass();
|
|
}
|
|
}
|
|
Icon icon = mappedIcons.get(clazz);
|
|
icon = icon == null ? new ImageIcon(Icons.FILE_SYMBOL_URL) : icon;
|
|
|
|
int fHeight = table.getFontMetrics(table.getFont()).getHeight() - 4;
|
|
int iWidth = (int) ((fHeight / (float) icon.getIconHeight()) * icon.getIconWidth());
|
|
icon = IconResizer.resizeIcon(icon, iWidth, fHeight);
|
|
|
|
label.setIcon(icon);
|
|
}
|
|
|
|
Color bg = (row % 2)==0?bgEven:bgOdd;
|
|
bg = isSelected? bgSelect : bg;
|
|
Color fg = isSelected? table.getSelectionForeground() : table.getForeground();
|
|
label.setIconTextGap(10);
|
|
label.setFont(table.getFont());
|
|
label.setOpaque(true);
|
|
label.setBackground(bg);
|
|
label.setForeground(fg);
|
|
return label;
|
|
}
|
|
|
|
public void putIcon(Class<?> clazz, Icon icon) {
|
|
this.mappedIcons.put(clazz, icon);
|
|
}
|
|
public void putIcon(Class<?> clazz, URL url) {
|
|
putIcon(clazz, new ImageIcon(url));
|
|
}
|
|
|
|
public Color getBackgroundEven() {
|
|
return this.bgEven;
|
|
}
|
|
public void setBackgroundEven(Color bg) {
|
|
this.bgEven = bg;
|
|
}
|
|
public Color getBackgroundOdd() {
|
|
return this.bgOdd;
|
|
}
|
|
public void setBackgroundOdd(Color bg) {
|
|
this.bgOdd = bg;
|
|
}
|
|
|
|
}
|