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.Locale ; 32 33 import javax.swing.JCheckBox ; 34 import javax.swing.JComboBox ; 35 import javax.swing.JTextArea ; 36 import javax.swing.border.BevelBorder ; 37 import javax.swing.event.DocumentEvent ; 38 import javax.swing.event.DocumentListener ; 39 import javax.swing.text.BadLocationException ; 40 import javax.swing.text.Document ; 41 42 import org.htmlparser.Node; 43 import org.htmlparser.NodeFilter; 44 import org.htmlparser.Parser; 45 import org.htmlparser.filters.StringFilter; 46 import org.htmlparser.parserapplications.filterbuilder.Filter; 47 48 51 public class StringFilterWrapper 52 extends 53 Filter 54 implements 55 ActionListener , 56 DocumentListener , 57 Runnable 58 { 59 62 protected StringFilter mFilter; 63 64 67 protected JTextArea mPattern; 68 69 72 protected JCheckBox mCaseSensitivity; 73 74 77 protected JComboBox mLocale; 78 79 82 protected static Locale [] mLocales = null; 83 84 87 public StringFilterWrapper () 88 { 89 Thread thread; 90 91 mFilter = new StringFilter (); 92 mFilter.setCaseSensitive (true); 93 94 mPattern = new JTextArea (2, 20); 96 mPattern.setBorder (new BevelBorder (BevelBorder.LOWERED)); 97 add (mPattern); 98 mPattern.getDocument ().addDocumentListener (this); 99 mPattern.setText (mFilter.getPattern ()); 100 101 mCaseSensitivity = new JCheckBox ("Case Sensitive"); 103 add (mCaseSensitivity); 104 mCaseSensitivity.addActionListener (this); 105 mCaseSensitivity.setSelected (mFilter.getCaseSensitive ()); 106 107 mLocale = new JComboBox (); 109 synchronized (mLocale) 110 { 111 mLocale.addItem (mFilter.getLocale ().getDisplayName ()); 112 thread = new Thread (this); 113 thread.setName ("locale_getter"); 114 thread.setPriority (Thread.MIN_PRIORITY); 115 thread.run (); 116 } 117 add (mLocale); 118 mLocale.addActionListener (this); 119 mLocale.setSelectedIndex (0); 120 mLocale.setVisible (!mFilter.getCaseSensitive ()); 121 } 122 123 127 public String getDescription () 128 { 129 return ("Nodes containing string"); 130 } 131 132 public String getIconSpec () 133 { 134 return ("images/StringFilter.gif"); 135 } 136 137 public NodeFilter getNodeFilter () 138 { 139 StringFilter ret; 140 141 ret = new StringFilter (); 142 ret.setCaseSensitive (mFilter.getCaseSensitive ()); 143 ret.setLocale (mFilter .getLocale ()); 144 ret.setPattern (mFilter.getPattern ()); 145 146 return (ret); 147 } 148 149 public void setNodeFilter (NodeFilter filter, Parser context) 150 { 151 mFilter = (StringFilter)filter; 152 mPattern.setText (mFilter.getPattern ()); 153 mCaseSensitivity.setSelected (mFilter.getCaseSensitive ()); 154 mLocale.setVisible (!mFilter.getCaseSensitive ()); 155 mLocale.setSelectedItem (mFilter.getLocale ().getDisplayName ()); 156 } 157 158 public NodeFilter[] getSubNodeFilters () 159 { 160 return (new NodeFilter[0]); 161 } 162 163 public void setSubNodeFilters (NodeFilter[] filters) 164 { 165 } 167 168 public String toJavaCode (StringBuffer out, int[] context) 169 { 170 String ret; 171 172 ret = "filter" + context[1]++; 173 spaces (out, context[0]); 174 out.append ("StringFilter "); 175 out.append (ret); 176 out.append (" = new StringFilter ();"); 177 newline (out); 178 spaces (out, context[0]); 179 out.append (ret); 180 out.append (".setCaseSensitive ("); 181 out.append (mFilter.getCaseSensitive () ? "true" : "false"); 182 out.append (");"); 183 newline (out); 184 spaces (out, context[0]); 185 out.append (ret); 186 out.append (".setLocale (new java.util.Locale (\""); 187 out.append (mFilter .getLocale ().getLanguage ()); 188 out.append ("\", \""); 189 out.append (mFilter .getLocale ().getCountry ()); 190 out.append ("\", \""); 191 out.append (mFilter .getLocale ().getVariant ()); 192 out.append ("\"));"); 193 newline (out); 194 spaces (out, context[0]); 195 out.append (ret); 196 out.append (".setPattern (\""); 197 out.append (mFilter.getPattern ()); 198 out.append ("\");"); 199 newline (out); 200 201 return (ret); 202 } 203 204 208 public boolean accept (Node node) 209 { 210 return (mFilter.accept (node)); 211 } 212 213 217 220 public void actionPerformed (ActionEvent event) 221 { 222 Object source; 223 boolean sensitive; 224 Object [] selection; 225 String locale; 226 227 source = event.getSource (); 228 if (source == mCaseSensitivity) 229 { 230 sensitive = mCaseSensitivity.isSelected (); 231 mFilter.setCaseSensitive (sensitive); 232 mLocale.setVisible (!sensitive); 233 mLocale.setSelectedItem (mFilter.getLocale ().getDisplayName ()); 234 } 235 else if (source == mLocale) 236 { 237 synchronized (mLocale) 238 { 239 selection = mLocale.getSelectedObjects (); 240 if ((null != selection) && (0 != selection.length)) 241 { 242 locale = (String )selection[0]; 243 for (int i = 0; i < mLocales.length; i++) 244 if (locale.equals (mLocales[i].getDisplayName ())) 245 mFilter.setLocale (mLocales[i]); 246 } 247 } 248 } 249 } 250 251 public void run() 255 { 256 String locale; 257 258 synchronized (mLocale) 259 { 260 mLocales = Locale.getAvailableLocales (); 261 locale = mFilter.getLocale ().getDisplayName (); 262 for (int i = 0; i < mLocales.length; i++) 263 if (!locale.equals (mLocales[i].getDisplayName ())) 264 mLocale.addItem (mLocales[i].getDisplayName ()); 265 mLocale.invalidate (); 266 } 267 } 268 269 273 public void insertUpdate (DocumentEvent e) 274 { 275 Document doc; 276 277 doc = e.getDocument (); 278 try 279 { 280 mFilter.setPattern (doc.getText (0, doc.getLength ())); 281 } 282 catch (BadLocationException ble) 283 { 284 ble.printStackTrace (); 285 } 286 } 287 288 public void removeUpdate (DocumentEvent e) 289 { 290 Document doc; 291 292 doc = e.getDocument (); 293 try 294 { 295 mFilter.setPattern (doc.getText (0, doc.getLength ())); 296 } 297 catch (BadLocationException ble) 298 { 299 ble.printStackTrace (); 300 } 301 } 302 303 public void changedUpdate (DocumentEvent e) 304 { 305 } 307 } 308 | Popular Tags |