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.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      //--- Constant(s) ---
38  
39      //--- Data Field(s) ---
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      //--- Constructor(s) ---
49  
50      public SegmentCellRenderer() 
51      {
52      }
53  
54      //--- Method(s) ---
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  			// draw progress
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  			// fill remaining part with availability
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 		// draw frame
104 		g.setColor(Color.lightGray);
105 		g.drawRect(1, 1, getWidth() - 2, getHeight() - 3);
106     }
107 
108 }