public void paint(Graphics g, JComponent component) {
if (component != view || view.getModel() == null)
return;
Graphics2D g2 = (Graphics2D) g;
ListModel model = view.getModel();
Rectangle paintBounds = g.getClipBounds();
Insets insets = view.getInsets();
Dimension size = view.getSize();
int modelSize = model.getSize();
int xcell = view.getXCellSize(), ycell = view.getYCellSize(), counter = 0;
metrics = g.getFontMetrics(view.getFont());
if (metrics == null) {
throw new RuntimeException("Could not resolve font metrics");
}
textHeight = metrics.getHeight();
/* Fix sizes */
if (paintBounds.x < insets.left)
paintBounds.x = insets.left;
if (paintBounds.width > size.width - insets.left - insets.right)
paintBounds.width = size.width - insets.left - insets.right;
if (paintBounds.y < insets.top)
paintBounds.y = insets.top;
if (view.getParent() instanceof JViewport) {
paintBounds.height += 2 * ycell;
if (paintBounds.height > size.height - insets.top - insets.bottom + ycell)
paintBounds.height = size.height - insets.top - insets.bottom + ycell;
} else {
if (paintBounds.height > size.height - insets.top - insets.bottom)
paintBounds.height = size.height - insets.top - insets.bottom;
}
/* Clear background */
g.setColor(view.getBackground());
g.fillRect(paintBounds.x, paintBounds.y, paintBounds.width, paintBounds.height);
g.setColor(view.getForeground());
int result[] = new int[(paintBounds.width / xcell) * (paintBounds.height / ycell)];
for (int y = paintBounds.y; y < paintBounds.y + paintBounds.height; y += ycell) {
for (int x = paintBounds.x; x <= paintBounds.x + paintBounds.width; x += xcell) {
int index = getIndexForXY(x, y);
if (index == -1)
continue;
int pos[] = getRowColumnForIndex(index);
int xpos = convertColumnToX(pos[1]);
int ypos = convertRowToY(pos[0]);
IconElement element = (IconElement) model.getElementAt(index);
ImageIcon icon = element.getIcon();
String name = element.getText();
int stringWidth = metrics.stringWidth(name);
int iconHeight = icon.getIconHeight();
int spacing = view.getIconTextSpacing();