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.tree;
21  
22  import java.awt.Component;
23  import java.awt.Font;
24  import java.awt.event.MouseEvent;
25  import javax.swing.Icon;
26  import javax.swing.JTree;
27  import javax.swing.UIManager;
28  import javax.swing.tree.DefaultTreeCellRenderer;
29  import org.xnap.gui.table.TransferWrapper;
30  import org.xnap.gui.table.TreeTableModel;
31  import org.xnap.transfer.Transfer;
32  
33  public class TransferTreeCellRenderer extends DefaultTreeCellRenderer
34  {
35  
36  	private Font plainFont;
37  	private Font boldFont;
38  	private TreeTableModel ttm;
39  	private Transfer transfer;
40  	private boolean highlightRunning;
41  	
42  	public TransferTreeCellRenderer(TreeTableModel ttm, 
43  									boolean highlightRunning)
44  	{
45  		this.ttm = ttm;
46  		this.highlightRunning = highlightRunning;
47  		
48  		plainFont = UIManager.getFont("Label.font");
49  		if (plainFont != null) {
50  			boldFont = plainFont.deriveFont(Font.BOLD);
51  		}
52  	}
53  
54      public Component getTreeCellRendererComponent(JTree tree, Object value,
55  												  boolean sel,
56  												  boolean expanded,
57  												  boolean leaf, int row,
58  												  boolean hasFocus) 
59      {
60  		super.getTreeCellRendererComponent
61  			(tree, value, sel, expanded, leaf, row, hasFocus);
62  
63  		setIcon((Icon)ttm.getValueAt(value, 0));
64  		
65  		TransferWrapper container = (TransferWrapper)ttm.getValueAt(value, 1);
66  		if (container == null) {
67  			transfer = null;
68  			return this;
69  		}
70  		
71  		transfer = container.transfer; 
72  		setText(transfer.getFilename());
73  
74  		if (highlightRunning && transfer.isRunning()) {
75  			setFont(boldFont);
76  		}
77  		else {
78  			setFont(plainFont);
79  		}
80  
81  		return this;
82  	}
83  	
84  	/***
85  	 *  @see javax.swing.JComponent#getToolTipText()
86  	 */
87  	public String getToolTipText(MouseEvent event)
88  	{
89  		if (transfer == null) {
90  			return null;
91  		}
92  		String description = transfer.getDescription();
93  		return (description != null) ? description : transfer.getFilename();
94  	}
95  
96  }