KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > parserapplications > filterbuilder > HtmlTreeCellRenderer


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2005 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/parserapplications/filterbuilder/HtmlTreeCellRenderer.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 20:43:06 $
10
// $Revision: 1.1 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.parserapplications.filterbuilder;
28
29 import java.awt.Component JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import javax.swing.JTree JavaDoc;
33 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
34 import javax.swing.tree.TreeCellRenderer JavaDoc;
35
36 import org.htmlparser.Attribute;
37 import org.htmlparser.Node;
38 import org.htmlparser.lexer.Cursor;
39 import org.htmlparser.nodes.TagNode;
40 import org.htmlparser.nodes.TextNode;
41 import org.htmlparser.util.ParserException;
42 import org.htmlparser.util.Translate;
43
44 /**
45  * @author derrick
46  *
47  * TODO To change the template for this generated type comment go to
48  * Window - Preferences - Java - Code Style - Code Templates
49  */

50 public class HtmlTreeCellRenderer
51     extends
52         DefaultTreeCellRenderer JavaDoc
53     implements
54         TreeCellRenderer JavaDoc
55 {
56     public HtmlTreeCellRenderer ()
57     {
58         setLeafIcon (null);
59         setClosedIcon (null);
60         setOpenIcon (null);
61     }
62
63     /**
64      * Render the tag as HTML.
65      * This is different from the tag's normal toHtml() method in that it
66      * doesn't process children or end tags, just the initial tag, and
67      * it also wraps the tag in something a label would expect.
68      * @see org.htmlparser.Node#toHtml()
69      */

70     public String JavaDoc toHtml (TagNode tag)
71     {
72         int length;
73         int size;
74         Vector JavaDoc attributes;
75         Attribute attribute;
76         String JavaDoc s;
77         boolean children;
78         StringBuffer JavaDoc ret;
79
80         length = 2;
81         attributes = tag.getAttributesEx ();
82         size = attributes.size ();
83         for (int i = 0; i < size; i++)
84         {
85             attribute = (Attribute)attributes.elementAt (i);
86             length += attribute.getLength ();
87         }
88         ret = new StringBuffer JavaDoc (length);
89         ret.append ("<");
90         for (int i = 0; i < size; i++)
91         {
92             attribute = (Attribute)attributes.elementAt (i);
93             attribute.toString (ret);
94         }
95         ret.append (">");
96         s = Translate.encode (ret.toString ());
97         children = null != tag.getChildren ();
98         ret = new StringBuffer JavaDoc (s.length () + 13 + (children ? 16 : 0));
99         ret.append ("<html>");
100         if (children)
101             ret.append ("<font color=\"blue\">");
102         ret.append (s);
103         if (children)
104             ret.append ("</font>");
105         ret.append ("</html>");
106
107         return (ret.toString ());
108     }
109     
110     /**
111      * Express this string node as a printable string
112      * This is suitable for display in a debugger or output to a printout.
113      * Control characters are replaced by their equivalent escape
114      * sequence and contents is truncated to 80 characters.
115      * @return A string representation of the string node.
116      */

117     public String JavaDoc toText (TextNode node)
118     {
119         int startpos;
120         int endpos;
121         String JavaDoc s;
122         char c;
123         StringBuffer JavaDoc ret;
124
125         startpos = node.getStartPosition ();
126         endpos = node.getEndPosition ();
127         ret = new StringBuffer JavaDoc (endpos - startpos + 20);
128         s = node.toHtml ();
129         for (int i = 0; i < s.length (); i++)
130         {
131             c = s.charAt (i);
132             switch (c)
133             {
134                 case '\t':
135                     ret.append ("\\t");
136                     break;
137                 case '\n':
138                     ret.append ("\\n");
139                     break;
140                 case '\r':
141                     ret.append ("\\r");
142                     break;
143                 default:
144                     ret.append (c);
145             }
146             if (77 <= ret.length ())
147             {
148                 ret.append ("...");
149                 break;
150             }
151         }
152
153         return (ret.toString ());
154     }
155
156     /**
157      * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree, java.lang.Object, boolean, boolean, boolean, int, boolean)
158      */

159     public Component JavaDoc getTreeCellRendererComponent (JTree JavaDoc tree, Object JavaDoc value,
160             boolean selected, boolean expanded, boolean leaf, int row,
161             boolean hasFocus)
162     {
163         Node node;
164
165         super.getTreeCellRendererComponent (tree, value, selected, expanded, leaf, row, hasFocus);
166         node = (Node)value;
167         if (node instanceof TagNode)
168             setText (toHtml ((TagNode)node));
169         else if (node instanceof TextNode)
170             setText (toText ((TextNode)node));
171         else
172             setText (node.toHtml ());
173
174         return (this);
175     }
176
177 }
178
Popular Tags