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.FontMetrics;
26 import java.awt.Graphics;
27
28 import org.xnap.XNap;
29 import org.xnap.util.Formatter;
30 import org.xnap.util.Preferences;
31 import org.xnap.util.Progress;
32
33 /***
34 * Renders progress in a progress bar with a text for a table cell.
35 *
36 * @see Progress
37 */
38 public class ProgressCellRenderer extends DefaultTableCellRenderer {
39
40
41
42
43
44 private static Color runningColor
45 = Preferences.getInstance().getColor("progressRunningColor");
46 private static Color finishedColor
47 = Preferences.getInstance().getColor("progressFinishedColor");
48
49 private Progress value = new Progress();
50 private String text;
51
52
53
54 public ProgressCellRenderer()
55 {
56 }
57
58
59
60 protected void setValue(Object value)
61 {
62 this.value
63 = (value instanceof Progress)
64 ? (Progress)value
65 : new Progress();
66
67 this.text = getProgressText();
68 setToolTipText(Formatter.formatNumber(this.value.getPercent(), 2)
69 + "%" + ((text.length() == 0) ? "" : ", " + text));
70
71 super.setValue(null);
72 }
73
74 public String getProgressText()
75 {
76 long rate = value.getRate();
77 if (rate < 0) {
78 return "";
79 }
80 else if (rate == 0) {
81 return XNap.tr("stalled");
82 }
83 else {
84 return XNap.tr(Formatter.formatSize(rate) + "/s");
85 }
86 }
87
88 public void paint(Graphics g)
89 {
90 super.paint(g);
91
92 double progress = value.getPercent();
93
94 int xoff = 1;
95 int yoff = 1;
96 int height = getHeight() - 3;
97 int width = getWidth() - 2;
98
99
100 if (progress > 0) {
101 g.setColor((progress < 100) ? runningColor : finishedColor);
102 g.fillRect(xoff, yoff, (width * (int)progress) / 100, height);
103 }
104
105
106 if (text != null && text.length() > 0) {
107 FontMetrics fm = g.getFontMetrics();
108 int w = fm.stringWidth(text);
109
110 int x = (getWidth() - w) / 2;
111 int y = getHeight() - 4;
112
113 g.setColor(getForeground());
114 g.drawString(text, (x > 1) ? x : 1, y);
115 }
116
117
118 g.setColor(Color.lightGray);
119 g.drawRect(1, 1, getWidth() - 2, getHeight() - 3);
120 }
121
122 }