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