1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.table;
21
22 import java.awt.Graphics;
23 import java.awt.Image;
24
25 import javax.swing.ImageIcon;
26 import javax.swing.table.DefaultTableCellRenderer;
27
28 import org.xnap.gui.util.IconHelper;
29 import org.xnap.XNap;
30
31 /***
32 * Renders time for a table cell. One litte green checkmarks is shown
33 * per 5 score points.
34 */
35 public class AvailabilityCellRenderer extends DefaultTableCellRenderer {
36
37
38
39 public static int WIDTH = 10;
40
41 public static int ICON_PER_SCORES = 10;
42
43
44
45 public static Image img;
46 static {
47 ImageIcon icon = IconHelper.getImage("16/services.png");
48 if (icon != null) {
49 img = icon.getImage().getScaledInstance(WIDTH, WIDTH,
50 Image.SCALE_SMOOTH);
51 }
52 }
53
54 private int score = 0;
55
56
57
58 public AvailabilityCellRenderer()
59 {
60 }
61
62
63
64 protected void setValue(Object value)
65 {
66 if (value instanceof Availability) {
67 score = ((Availability)value).availability;
68 score = (score > 0) ? score / ICON_PER_SCORES + 1 : 0;
69
70 setToolTipText
71 (XNap.tr("{0} sources",
72 new Integer(((Availability)value).sourcesCount)));
73
74 if (img == null) {
75 value = score + "";
76 }
77 else {
78 value = null;
79 }
80 }
81
82 super.setValue(value);
83 }
84
85
86 public void paintComponent(Graphics g)
87 {
88 super.paintComponent(g);
89
90 if (img != null) {
91 for (int i = 0; i < score; i++) {
92 g.drawImage(img, 2 + i * WIDTH, 2, null);
93 }
94 }
95 }
96
97 public static class Availability implements Comparable
98 {
99
100 int availability;
101 int sourcesCount;
102
103 public Availability(int availability, int sourcesCount)
104 {
105 this.availability = availability;
106 this.sourcesCount = sourcesCount;
107 }
108
109 public int compareTo(Object o1)
110 {
111 return this.availability - ((Availability)o1).availability;
112 }
113
114 }
115
116 }