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.event.MouseEvent;
24  import java.util.Iterator;
25  
26  import javax.swing.Icon;
27  import javax.swing.JTree;
28  import javax.swing.tree.DefaultTreeCellRenderer;
29  
30  import org.xnap.XNap;
31  import org.xnap.gui.table.SearchResultWrapper;
32  import org.xnap.gui.table.TreeTableModel;
33  import org.xnap.gui.util.GUIHelper;
34  import org.xnap.search.SearchResult;
35  
36  public class SearchTreeCellRenderer extends DefaultTreeCellRenderer
37  {
38  
39  	private TreeTableModel ttm;
40  	private SearchResult result;
41  	
42  	public SearchTreeCellRenderer(TreeTableModel ttm)
43  	{
44  		this.ttm = ttm;
45  	}
46  
47      public Component getTreeCellRendererComponent(JTree tree, Object value,
48  												  boolean sel,
49  												  boolean expanded,
50  												  boolean leaf, int row,
51  												  boolean hasFocus) 
52      {
53  		super.getTreeCellRendererComponent
54  			(tree, value, sel, expanded, leaf, row, hasFocus);
55  
56  		setIcon((Icon)ttm.getValueAt(value, 0));
57  		
58  		SearchResultWrapper srw = (SearchResultWrapper)ttm.getValueAt(value, 1);
59  		if (srw == null) {
60  			result = null;
61  			return this;
62  		}
63  		
64  		result = srw.sr; 
65  		setText(result.getShortFilename());
66  
67  		return this;
68  	}
69  	
70  	/***
71  	 *  @see javax.swing.JComponent#getToolTipText()
72  	 */
73  	public String getToolTipText(MouseEvent event)
74  	{
75  		if (result == null) {
76  			return null;
77  		}
78  		
79  		StringBuffer sb = new StringBuffer("<html><table>");
80  		sb.append(GUIHelper.tableRow(XNap.tr("Filename"), result.getFilename()));
81  			
82  		Iterator it = result.keys();
83  		if (it != null) {
84  			while (it.hasNext()) {
85  				String key = (String)it.next();
86  				if (!key.startsWith(SearchResult.HIDE_PREFIX)) {
87  					sb.append(GUIHelper.tableRow(key, result.get(key) + ""));
88  				}
89  			}
90  		}
91  
92  		sb.append("</table></html>");
93  
94  		return sb.toString();
95  	}
96  
97  }