team-maker/src/com/jdbernard/teammaker/PlayerListCellRenderer.groovy
2010-07-06 08:07:09 -05:00

75 lines
2.1 KiB
Groovy

package com.jdbernard.teammaker
import java.awt.BorderLayout
import java.awt.Color
import java.awt.Component
import javax.swing.JLabel
import javax.swing.JList
import javax.swing.JPanel
import javax.swing.ListCellRenderer
import javax.swing.BorderFactory
public class PlayerListCellRenderer extends JPanel implements ListCellRenderer {
boolean showStats
boolean colored
private TeamMaker teamMaker
private JLabel nameLabel
private JLabel statsLabel
private static def linedBorder = BorderFactory.createLineBorder(Color.BLACK)
private static def emptyBorder = BorderFactory.createEmptyBorder()
private def colors = [Color.getHSBColor(0.32f, 1f, 1f),
Color.getHSBColor(0.24f,1f,1f), Color.getHSBColor(0.16f, 1f, 1f),
Color.getHSBColor(0.08f, 1f, 1f), Color.getHSBColor(0.0f, 1f, 1f)]
public PlayerListCellRenderer(Map params, def tm) {
teamMaker = tm
showStats = params.showStats ?: false
colored = params.colored ?: false
setLayout(new BorderLayout())
setOpaque(colored)
nameLabel = new JLabel()
nameLabel.opaque = false
nameLabel.horizontalAlignment = JLabel.LEADING
add(nameLabel, BorderLayout.WEST)
if (showStats) {
statsLabel = new JLabel()
statsLabel.horizontalAlignment = JLabel.TRAILING
statsLabel.opaque = false
add(statsLabel, BorderLayout.EAST)
}
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
assert value instanceof Player
nameLabel.setText(value.name)
if (isSelected) setBorder(linedBorder)
else setBorder(emptyBorder)
if (showStats) {
def odds = teamMaker.oddsCalculator.odds[(value)]
odds = odds ? Math.round(odds * 100) : "?"
statsLabel.text = "${value.gamesSat} (${odds}%)"
}
if (colored) {
def c = colors[Math.min(4, value.gamesSat)]
setBackground(c)
}
return this
}
}