File: ITunesRatingTableCellRenderer.java

package info (click to toggle)
mac-widgets 0.9.5%2Bsvn369-dfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,920 kB
  • sloc: java: 8,318; makefile: 13; sh: 12
file content (47 lines) | stat: -rw-r--r-- 1,542 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.explodingpixels.macwidgets;

import com.explodingpixels.data.Rating;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.Component;
import java.util.HashMap;
import java.util.Map;

public class ITunesRatingTableCellRenderer extends DefaultTableCellRenderer {

    private Map<Rating, RatingComponent> fRatingsToRatingComponents =
            new HashMap<Rating, RatingComponent>();

    public ITunesRatingTableCellRenderer() {
        // iterate over each value in the rating enumeration and add a
        // corresponding rating component.
        for (Rating rating : Rating.values()) {
            fRatingsToRatingComponents.put(rating, new RatingComponent(rating));
        }

    }

    @Override
    public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {

        super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                row, column);

        Rating rating = (Rating) value;

        // grab the cached rating component for the corresponding rating.
        RatingComponent renderer = value == null
                ? fRatingsToRatingComponents.get(Rating.NO_RATING)
                : fRatingsToRatingComponents.get(rating);
        renderer.setFocused(table.hasFocus());
        renderer.setSelected(table.isRowSelected(row));

        renderer.getComponent().setBackground(getBackground());

        return renderer.getComponent();
    }

}