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 javax.swing.Icon;
23  import javax.swing.JTree;
24  import javax.swing.UIManager;
25  import javax.swing.tree.DefaultTreeCellRenderer;
26  
27  import java.awt.Component;
28  import java.awt.Font;
29  
30  import org.xnap.gui.table.TreeTableModel;
31  
32  /***
33   */
34  public class IconTreeCellRenderer extends DefaultTreeCellRenderer
35  {
36  
37      //--- Constant(s) ---
38  
39      //--- Data Field(s) ---
40  
41      private TreeTableModel ttm;
42  
43  	private Font plainFont;
44  	private Font boldFont;
45  	private String magicChar;
46  
47      //--- Constructor(s) ---
48  
49      public IconTreeCellRenderer(TreeTableModel ttm, String magicChar) 
50      {
51  		this.ttm = ttm;
52  		this.magicChar = magicChar;
53  
54  		plainFont = UIManager.getFont("Label.font");
55  		if (plainFont != null) {
56  			boldFont = plainFont.deriveFont(Font.BOLD);
57  		}
58      }
59  
60  	public IconTreeCellRenderer(TreeTableModel ttm)
61  	{
62  		this(ttm, null);
63  	}
64  
65      //--- Method(s) ---
66  
67  	public TreeTableModel getModel()
68  	{
69  		return ttm;
70  	}
71  
72      public Component getTreeCellRendererComponent(JTree tree, Object value,
73  												  boolean sel,
74  												  boolean expanded,
75  												  boolean leaf, int row,
76  												  boolean hasFocus) 
77      {
78  		super.getTreeCellRendererComponent
79  			(tree, null, sel, expanded, leaf, row, hasFocus);
80  
81  		setIcon((Icon)ttm.getValueAt(value, 0));
82  		
83  		Object o = ttm.getValueAt(value, 1);
84  
85  		if (o == null) {
86  			return this;
87  		}
88  
89  		String s = o.toString();
90  
91  		if (magicChar != null) {
92  			if (s != null && s.startsWith(magicChar)) {
93  				setFont(boldFont);
94  				s = s.substring(1);
95  			}
96  			else {
97  				setFont(plainFont);
98  			}
99  		}
100 
101 		setText(s);
102 		setToolTipText(s);
103 
104 		return this;
105     }
106 
107 }