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.plugin;
21  
22  import java.awt.Component;
23  import java.awt.Font;
24  
25  import javax.swing.Icon;
26  import javax.swing.JTree;
27  import javax.swing.UIManager;
28  import javax.swing.tree.DefaultMutableTreeNode;
29  import javax.swing.tree.DefaultTreeCellRenderer;
30  
31  import org.xnap.gui.util.IconHelper;
32  import org.xnap.pkg.PackageSection;
33  
34  /***
35   * This class provides a list renderer for {@link PluginInfo} objects. 
36   * An icon is shown if an update is available.
37   */
38  public class PluginTreeCellRenderer extends DefaultTreeCellRenderer
39  {
40  
41      //--- Constant(s) ---
42  
43      public static final Icon ICON_ENABLED
44  		= IconHelper.getIcon("ok.png", 16);
45      public static final Icon ICON_NOT_INSTALLED
46  		= IconHelper.getIcon("noball.png", 16);
47      public static final Icon ICON_INSTALLED
48  		= IconHelper.getIcon("tick.png", 16);
49      public static final Icon ICON_NEW
50  		= IconHelper.getIcon("snew.png", 16);
51      public static final Icon ICON_UPDATE_AVAILABLE 
52  		= IconHelper.getIcon("supdated.png", 16, false);
53  
54  	//--- Data Field(s) ---
55  
56      //--- Constructor(s) ---
57  
58      public PluginTreeCellRenderer() 
59      {
60      }
61  
62      //--- Method(s) ---
63  
64      public Component getTreeCellRendererComponent(JTree tree, Object value,
65  												  boolean sel,
66  												  boolean expanded,
67  												  boolean leaf, int row,
68  												  boolean hasFocus) 
69  	{	
70  		super.getTreeCellRendererComponent
71  			(tree, value, sel, expanded, leaf, row, hasFocus);
72  
73  		Font font = UIManager.getFont("Label.font");
74  		if (value instanceof PluginNode) {
75  			// package node
76  			PluginNode node = (PluginNode)value;
77  			setFont(font);
78  			if (node.getPluginInfo() != null) {
79  				setIcon(node.isUpdateAvailable()
80  						? ICON_UPDATE_AVAILABLE
81  						: node.getPluginInfo().isEnabled()
82  						? ICON_ENABLED
83  						: ICON_INSTALLED);
84  			}
85  			else {
86  				setIcon(node.getPackageInfo().isNew()
87  						? ICON_NEW
88  						: node.getPackageInfo().isInstalled()
89  						? ICON_INSTALLED
90  						: ICON_NOT_INSTALLED);
91  			}
92  			if (node.isActionChanged()) {
93  				font = getFont();
94  				setFont((font != null) ? font.deriveFont(Font.ITALIC) : null);
95  			}
96  			setText(node.getPackageInfo().getName());
97  		}
98  		else if (value instanceof DefaultMutableTreeNode) {
99  			DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
100 			if (node.getUserObject() instanceof PackageSection) {
101 				PackageSection s = (PackageSection)node.getUserObject();
102 				// section node
103 				setFont((font != null) ? font.deriveFont(Font.BOLD) : null);
104 				setText(s.getName());
105 				setIcon(s.getIcon());
106 			}
107 		}
108 
109 		return this;
110     }
111 
112 }