1 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 50 public class HtmlTreeCellRenderer 51 extends 52 DefaultTreeCellRenderer 53 implements 54 TreeCellRenderer 55 { 56 public HtmlTreeCellRenderer () 57 { 58 setLeafIcon (null); 59 setClosedIcon (null); 60 setOpenIcon (null); 61 } 62 63 70 public String toHtml (TagNode tag) 71 { 72 int length; 73 int size; 74 Vector attributes; 75 Attribute attribute; 76 String s; 77 boolean children; 78 StringBuffer 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 (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 (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 117 public String toText (TextNode node) 118 { 119 int startpos; 120 int endpos; 121 String s; 122 char c; 123 StringBuffer ret; 124 125 startpos = node.getStartPosition (); 126 endpos = node.getEndPosition (); 127 ret = new StringBuffer (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 159 public Component getTreeCellRendererComponent (JTree tree, Object 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 |