View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      //--- Constant(s) ---
41  
42      //--- Data Field(s) ---
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      //--- Constructor(s) ---
53  
54      public ProgressCellRenderer() 
55      {
56      }
57  
58      //--- Method(s) ---
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  		// draw progress
100 		if (progress > 0) {
101 			g.setColor((progress < 100) ? runningColor : finishedColor);
102 			g.fillRect(xoff, yoff, (width * (int)progress) / 100, height);
103 		}
104 
105 		// draw text
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 		// draw frame
118 		g.setColor(Color.lightGray);
119 		g.drawRect(1, 1, getWidth() - 2, getHeight() - 3);
120     }
121 
122 }