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 javax.swing.table.DefaultTableCellRenderer;
23
24 import java.awt.Color;
25 import java.awt.Graphics;
26
27 import org.xnap.transfer.Segment;
28 import org.xnap.util.Preferences;
29
30 /***
31 * Renders segments.
32 *
33 * @see Segment
34 */
35 public class SegmentCellRenderer extends DefaultTableCellRenderer {
36
37
38
39
40
41 private static Color notAvailableColor = Color.white;
42 private static Color progressColor = new Color(0, 0, 128);
43 private static Color finishedColor
44 = Preferences.getInstance().getColor("segmentFinishedColor");
45
46 private Segment[] segments = new Segment[0];
47
48
49
50 public SegmentCellRenderer()
51 {
52 }
53
54
55
56 protected void setValue(Object value)
57 {
58 this.segments
59 = (value instanceof Segment[])
60 ? (Segment[])value
61 : new Segment[0];
62
63 super.setValue(null);
64 }
65
66 public void paint(Graphics g)
67 {
68 super.paint(g);
69
70 int xoff = 1;
71 int yoff = 1;
72 int height = getHeight() - 3;
73 int width = getWidth() - 2;
74
75 for (int i = 0; i < segments.length; i++) {
76 if (segments[i] == null || segments[i].getTotal() == 0) {
77 continue;
78 }
79
80 int x = xoff + (int)((segments[i].getStart() * width) / segments[i].getTotal());
81 int w = (int)(((segments[i].getEnd() - segments[i].getStart()) * width) / segments[i].getTotal());
82 w = Math.max(1, w);
83
84
85 g.setColor(new Color(0, 0, 128));
86 int myw = (int)((segments[i].getTransferred() * width) / segments[i].getTotal());
87 if (myw == 0 && segments[i].getTransferred() > 0) {
88 myw = 1;
89 }
90
91 g.fillRect(x, yoff + 1, myw, height - 1);
92
93
94 int availability = segments[i].getAvailability();
95 availability = Math.min(255, availability);
96 availability = Math.max(0, availability);
97 g.setColor
98 (new Color(255 - availability, 255 - availability,
99 255 - availability / 2));
100 g.fillRect(x + myw, yoff + 1, w - myw, height - 1);
101 }
102
103
104 g.setColor(Color.lightGray);
105 g.drawRect(1, 1, getWidth() - 2, getHeight() - 3);
106 }
107
108 }