KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > salsa > tree > XmlTreeNodeRenderer


1 /*
2 ** Salsa - Swing Add-On Suite
3 ** Copyright (c) 2001, 2002 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package salsa.tree;
24
25 import java.awt.Color JavaDoc;
26 import java.awt.Component JavaDoc;
27 import java.util.*;
28 import javax.swing.*;
29 import javax.swing.tree.*;
30 import org.jdom.*;
31 import houston.*;
32
33 public class XmlTreeNodeRenderer implements TreeCellRenderer
34 {
35    static Logger T = Logger.getLogger( XmlTreeNodeRenderer.class );
36
37    private Element _el;
38    private JLabel _label;
39
40    public XmlTreeNodeRenderer()
41    {
42       _label = new JLabel();
43    }
44
45    public Component JavaDoc getTreeCellRendererComponent( JTree tree,
46          Object JavaDoc value,
47          boolean is_selected,
48          boolean is_expanded,
49          boolean is_leaf,
50          int row,
51          boolean has_focus )
52    {
53       _el = ( Element ) value;
54
55       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
56
57       if( is_selected )
58       {
59          buf.append( "<body bgcolor=black><font color=white>" );
60          buf.append( "<b>" + _el.getName() + "</b> " );
61
62          List attributes = _el.getAttributes();
63          for( Iterator it = attributes.iterator(); it.hasNext(); )
64          {
65             Attribute attr = ( Attribute ) it.next();
66
67             buf.append( "<em>" + attr.getName() + "=</em>" );
68             buf.append( attr.getValue() + " " );
69          }
70
71          if( _el.getTextTrim() != null )
72             buf.append( _el.getTextTrim() );
73
74          buf.append( "</font></body>" );
75       }
76       else
77       {
78          buf.append( "<b>" + _el.getName() + "</b> " );
79
80          List attributes = _el.getAttributes();
81          for( Iterator it = attributes.iterator(); it.hasNext(); )
82          {
83             Attribute attr = ( Attribute ) it.next();
84
85             buf.append( "<em>" + attr.getName() );
86             buf.append( "<font color=grey>=</font></em>" );
87             buf.append( "<font color=blue>" + attr.getValue() + "</font>" );
88             buf.append( " " );
89          }
90
91          if( _el.getTextTrim() != null )
92             buf.append( _el.getTextTrim() );
93       }
94
95       _label.setText( "<html>" + buf.toString() + "</html>" );
96
97       return _label;
98    }
99
100 }
101
Popular Tags