nt.UI/src/nt/UI/display/ObjListCellRenderer.java

89 lines
2.5 KiB
Java

package nt.UI.display;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.filechooser.FileSystemView;
import javafx.stage.FileChooser;
import nt.UI.util.IconResizer;
import nt.UI.util.Icons;
public class ObjListCellRenderer implements ListCellRenderer<Object>{
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 ObjListCellRenderer() {
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 getListCellRendererComponent(JList<? extends Object> list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
if(value == null) {
return null;
}
Icon icon = mappedIcons.get(value.getClass());
icon = icon==null? new ImageIcon(Icons.FILE_SYMBOL_URL): icon;
int fHeight = list.getFontMetrics(list.getFont()).getHeight() - 4;
int iWidth = (int)((fHeight / (float)icon.getIconHeight()) * icon.getIconWidth());
icon = IconResizer.resizeIcon(icon, iWidth, fHeight);
JLabel label = new JLabel(value.toString());
label.setIconTextGap(10);
label.setFont(list.getFont());
label.setOpaque(true);
label.setIcon(icon);
Color bg = (index % 2)==0?bgEven:bgOdd;
bg = isSelected? bgSelect : bg;
Color fg = isSelected? list.getSelectionForeground() : list.getForeground();
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;
}
}