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 org.xnap.util.Formatter;
25  
26  /***
27   * Renders time for a table cell. If the value is an Integer object it
28   * is rendered as a length. If value is a Long object it is rendered
29   * as absolute time relative to the 1.1.1970.
30   *
31   * @see Formatter
32   */
33  public class TimeCellRenderer extends DefaultTableCellRenderer
34  {
35  
36      //--- Constructor(s) ---
37  
38      public TimeCellRenderer() 
39      {
40  	setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
41      }
42  
43      //--- Method(s) ---
44  
45      protected void setValue(Object value) 
46      {
47  	String s = "";
48  
49  	if (value != null) {
50  	    if (value instanceof Integer) {
51  		int i = ((Number)value).intValue();
52  		if (i >= 0) {
53  		    s = Formatter.formatLength(i);
54  		}
55  	    }
56  	    else if (value instanceof Long) {
57  		long l = ((Long)value).longValue();
58  		if (l >= 0) {
59  		    s = Formatter.formatTime(l);
60  		}
61  	    }
62  	}
63  
64  	super.setValue(s);
65      } 
66  }