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 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      //--- Constant(s) ---
38  
39      public static int WIDTH = 10;
40  
41      public static int ICON_PER_SCORES = 10;
42  
43      //--- Data Field(s) ---
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      //--- Constructor(s) ---
57  
58      public AvailabilityCellRenderer() 
59      {
60      }
61  
62      //--- Method(s) ---
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      // FIX: possible race condition, score might have changed in the mean time
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 }