1 27 package org.htmlparser.parserapplications.filterbuilder.wrappers; 28 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.util.Iterator ; 32 import java.util.Set ; 33 34 import javax.swing.JComboBox ; 35 40 import org.htmlparser.Node; 41 import org.htmlparser.NodeFactory; 42 import org.htmlparser.NodeFilter; 43 import org.htmlparser.Parser; 44 import org.htmlparser.PrototypicalNodeFactory; 45 import org.htmlparser.Tag; 46 import org.htmlparser.filters.NodeClassFilter; 47 import org.htmlparser.parserapplications.filterbuilder.Filter; 48 49 52 public class NodeClassFilterWrapper 53 extends 54 Filter 55 implements 56 ActionListener 57 { 60 63 protected NodeClassFilter mFilter; 64 65 68 protected JComboBox mClass; 69 70 73 public NodeClassFilterWrapper () 74 { 75 mFilter = new NodeClassFilter (); 76 77 mClass = new JComboBox (); 79 mClass.addItem (""); 80 add (mClass); 81 mClass.addActionListener (this); 82 } 83 84 88 public String getDescription () 89 { 90 return ("Nodes of class"); 91 } 92 93 public String getIconSpec () 94 { 95 return ("images/NodeClassFilter.gif"); 96 } 97 98 public NodeFilter getNodeFilter () 99 { 100 NodeClassFilter ret; 101 102 ret = new NodeClassFilter (); 103 ret.setMatchClass (mFilter.getMatchClass ()); 104 105 return (ret); 106 } 107 108 public void setNodeFilter (NodeFilter filter, Parser context) 109 { 110 NodeFactory factory; 111 PrototypicalNodeFactory proto; 112 Set names; 113 String name; 114 Tag tag; 115 116 mFilter = (NodeClassFilter)filter; 117 118 factory = context.getNodeFactory (); 119 if (factory instanceof PrototypicalNodeFactory) 120 { 121 proto = (PrototypicalNodeFactory)factory; 122 names = proto.getTagNames (); 124 for (Iterator iterator = names.iterator (); iterator.hasNext (); ) 125 { 126 name = (String )iterator.next (); 127 tag = proto.get (name); 128 mClass.addItem (tag.getClass ().getName ()); 129 } 130 } 131 mClass.setSelectedItem (mFilter.getMatchClass ().getName ()); 132 } 133 134 public NodeFilter[] getSubNodeFilters () 135 { 136 return (new NodeFilter[0]); 137 } 138 139 public void setSubNodeFilters (NodeFilter[] filters) 140 { 141 } 143 144 public String toJavaCode (StringBuffer out, int[] context) 145 { 146 String ret; 147 148 ret = "filter" + context[1]++; 149 spaces (out, context[0]); 150 out.append ("NodeClassFilter "); 151 out.append (ret); 152 out.append (" = new NodeClassFilter ();"); 153 newline (out); 154 spaces (out, context[0]); 155 out.append ("try { "); 156 out.append (ret); 157 out.append (".setMatchClass (Class.forName (\""); 158 out.append (mFilter.getMatchClass ().getName ()); 159 out.append ("\")); } catch (ClassNotFoundException cnfe) { cnfe.printStackTrace (); }"); 160 newline (out); 161 162 return (ret); 163 } 164 165 169 public boolean accept (Node node) 170 { 171 return (mFilter.accept (node)); 172 } 173 174 178 181 public void actionPerformed (ActionEvent event) 182 { 183 Object source; 184 185 source = event.getSource (); 186 if (source == mClass) 187 try 188 { 189 mFilter.setMatchClass (Class.forName ((String )mClass.getSelectedItem ())); 190 } 191 catch (ClassNotFoundException cnfe) 192 { 193 cnfe.printStackTrace (); 194 } 195 } 196 197 } 236 | Popular Tags |