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