/ org.htmlparser / src / org / htmlparser / parserapplications / filterbuilder / HtmlTreeCellRenderer.java
HtmlTreeCellRenderer.java
1 // HTMLParser Library $Name: v1_6_20060319 $ - 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/04/12 11:27:42 $ 10 // $Revision: 1.2 $ 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; 30 import java.util.Vector; 31 32 import javax.swing.JTree; 33 import javax.swing.tree.DefaultTreeCellRenderer; 34 import javax.swing.tree.TreeCellRenderer; 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 * Renderer for tree view of a NodeList. 46 */ 47 public class HtmlTreeCellRenderer 48 extends 49 DefaultTreeCellRenderer 50 implements 51 TreeCellRenderer 52 { 53 /** 54 * Create a new tree cell renderer for Nodes. 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 html a label would expect. 68 * @see org.htmlparser.Node#toHtml() 69 * @param tag The tag to convert to HTML. 70 * @return A string suitable for rendering the tag. 71 */ 72 public String toHtml (TagNode tag) 73 { 74 int length; 75 int size; 76 Vector attributes; 77 Attribute attribute; 78 String s; 79 boolean children; 80 StringBuffer ret; 81 82 length = 2; 83 attributes = tag.getAttributesEx (); 84 size = attributes.size (); 85 for (int i = 0; i < size; i++) 86 { 87 attribute = (Attribute)attributes.elementAt (i); 88 length += attribute.getLength (); 89 } 90 ret = new StringBuffer (length); 91 ret.append ("<"); 92 for (int i = 0; i < size; i++) 93 { 94 attribute = (Attribute)attributes.elementAt (i); 95 attribute.toString (ret); 96 } 97 ret.append (">"); 98 s = Translate.encode (ret.toString ()); 99 children = null != tag.getChildren (); 100 ret = new StringBuffer (s.length () + 13 + (children ? 16 : 0)); 101 ret.append ("<html>"); 102 if (children) 103 ret.append ("<font color=\"blue\">"); 104 ret.append (s); 105 if (children) 106 ret.append ("</font>"); 107 ret.append ("</html>"); 108 109 return (ret.toString ()); 110 } 111 112 /** 113 * Express this string node as a printable string 114 * This is suitable for display in a debugger or output to a printout. 115 * Control characters are replaced by their equivalent escape 116 * sequence and contents is truncated to 80 characters. 117 * @param node The node to render. 118 * @return A string representation of the string node. 119 */ 120 public String toText (TextNode node) 121 { 122 int startpos; 123 int endpos; 124 String s; 125 char c; 126 StringBuffer ret; 127 128 startpos = node.getStartPosition (); 129 endpos = node.getEndPosition (); 130 ret = new StringBuffer (endpos - startpos + 20); 131 s = node.toHtml (); 132 for (int i = 0; i < s.length (); i++) 133 { 134 c = s.charAt (i); 135 switch (c) 136 { 137 case '\t': 138 ret.append ("\\t"); 139 break; 140 case '\n': 141 ret.append ("\\n"); 142 break; 143 case '\r': 144 ret.append ("\\r"); 145 break; 146 default: 147 ret.append (c); 148 } 149 if (77 <= ret.length ()) 150 { 151 ret.append ("..."); 152 break; 153 } 154 } 155 156 return (ret.toString ()); 157 } 158 159 /** 160 * Render the node for the tree cell. 161 * @see TreeCellRenderer#getTreeCellRendererComponent(JTree, Object, boolean, boolean, boolean, int, boolean) 162 * @param tree {@inheritDoc} 163 * @param value {@inheritDoc} 164 * @param selected {@inheritDoc} 165 * @param expanded {@inheritDoc} 166 * @param leaf {@inheritDoc} 167 * @param row {@inheritDoc} 168 * @param hasFocus {@inheritDoc} 169 * @return {@inheritDoc} 170 */ 171 public Component getTreeCellRendererComponent (JTree tree, Object value, 172 boolean selected, boolean expanded, boolean leaf, int row, 173 boolean hasFocus) 174 { 175 Node node; 176 177 super.getTreeCellRendererComponent (tree, value, selected, expanded, leaf, row, hasFocus); 178 node = (Node)value; 179 if (node instanceof TagNode) 180 setText (toHtml ((TagNode)node)); 181 else if (node instanceof TextNode) 182 setText (toText ((TextNode)node)); 183 else 184 setText (node.toHtml ()); 185 186 return (this); 187 } 188 189 }