KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > navtree > TreeCellRenderer


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.console.navtree;
23
24 import java.awt.Component JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 import javax.swing.ImageIcon JavaDoc;
29 import javax.swing.JTree JavaDoc;
30 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
31
32 /**
33  * Tree cell rendered. Can display another icon if available in the
34  * plugin description.
35  *
36  * @see org.jboss.console.navtree.AdminTreeBrowser
37  *
38  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
39  * @version $Revision: 37459 $
40  *
41  * <p><b>Revisions:</b>
42  *
43  * <p><b>20 decembre 2002 Sacha Labourey:</b>
44  * <ul>
45  * <li> First implementation </li>
46  * </ul>
47  */

48
49 public class TreeCellRenderer extends DefaultTreeCellRenderer JavaDoc
50 {
51    protected TreeContext ctx = null;
52    protected static HashMap JavaDoc cache = new HashMap JavaDoc ();
53    
54    public TreeCellRenderer (TreeContext ctx)
55    {
56       super();
57       this.ctx = ctx;
58    }
59
60    public Component JavaDoc getTreeCellRendererComponent(
61                         JTree JavaDoc tree,
62                         Object JavaDoc value,
63                         boolean sel,
64                         boolean expanded,
65                         boolean leaf,
66                         int row,
67                         boolean hasFocus) {
68
69       super.getTreeCellRendererComponent(
70                      tree, value, sel,
71                      expanded, leaf, row,
72                      hasFocus);
73       if (value instanceof NodeWrapper)
74       {
75          NodeWrapper node = (NodeWrapper)value;
76          
77          String JavaDoc targetUrl = node.getIconUrl ();
78          ImageIcon JavaDoc img = (ImageIcon JavaDoc)cache.get( targetUrl );
79          
80          if (img != null)
81          {
82             setIcon (img);
83          }
84          else
85          {
86             URL JavaDoc target = null;
87             
88             try { target = new URL JavaDoc(this.ctx.localizeUrl(targetUrl)); } catch (Exception JavaDoc ignored) {}
89             
90             if (target != null)
91             {
92                try
93                {
94                   img = new ImageIcon JavaDoc(target);
95                   cache.put (targetUrl, img);
96                   setIcon (img);
97                }
98                catch (Exception JavaDoc tobad) {}
99             }
100          }
101          
102          
103          String JavaDoc desc = node.getDescription ();
104          if (desc != null)
105          {
106             setToolTipText (desc);
107          }
108       }
109
110       return this;
111    }
112    
113    // Constants -----------------------------------------------------
114

115    // Attributes ----------------------------------------------------
116

117    // Static --------------------------------------------------------
118

119    // Constructors --------------------------------------------------
120

121    public TreeCellRenderer ()
122    {
123    }
124    
125    // Public --------------------------------------------------------
126

127    // Z implementation ----------------------------------------------
128

129    // Y overrides ---------------------------------------------------
130

131    // Package protected ---------------------------------------------
132

133    // Protected -----------------------------------------------------
134

135    // Private -------------------------------------------------------
136

137    // Inner classes -------------------------------------------------
138

139 }
140
Popular Tags