KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > parserapplications > filterbuilder > wrappers > StringFilterWrapper


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/wrappers/StringFilterWrapper.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.wrappers;
28
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.util.Locale JavaDoc;
32
33 import javax.swing.JCheckBox JavaDoc;
34 import javax.swing.JComboBox JavaDoc;
35 import javax.swing.JTextArea JavaDoc;
36 import javax.swing.border.BevelBorder JavaDoc;
37 import javax.swing.event.DocumentEvent JavaDoc;
38 import javax.swing.event.DocumentListener JavaDoc;
39 import javax.swing.text.BadLocationException JavaDoc;
40 import javax.swing.text.Document JavaDoc;
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 /**
49  * Wrapper for StringFilters.
50  */

51 public class StringFilterWrapper
52     extends
53         Filter
54     implements
55         ActionListener JavaDoc,
56         DocumentListener JavaDoc,
57         Runnable JavaDoc
58 {
59     /**
60      * The underlying filter.
61      */

62     protected StringFilter mFilter;
63
64     /**
65      * Text to check for.
66      */

67     protected JTextArea JavaDoc mPattern;
68
69     /**
70      * The check box for case sensitivity.
71      */

72     protected JCheckBox JavaDoc mCaseSensitivity;
73
74     /**
75      * Combo box for locale.
76      */

77     protected JComboBox JavaDoc mLocale;
78
79     /**
80      * Cached locales.
81      */

82     protected static Locale JavaDoc[] mLocales = null;
83
84     /**
85      * Create a wrapper over a new StringFilter.
86      */

87     public StringFilterWrapper ()
88     {
89         Thread JavaDoc thread;
90
91         mFilter = new StringFilter ();
92         mFilter.setCaseSensitive (true);
93
94         // add the text pattern
95
mPattern = new JTextArea JavaDoc (2, 20);
96         mPattern.setBorder (new BevelBorder JavaDoc (BevelBorder.LOWERED));
97         add (mPattern);
98         mPattern.getDocument ().addDocumentListener (this);
99         mPattern.setText (mFilter.getPattern ());
100
101         // add the case sensitivity flag
102
mCaseSensitivity = new JCheckBox JavaDoc ("Case Sensitive");
103         add (mCaseSensitivity);
104         mCaseSensitivity.addActionListener (this);
105         mCaseSensitivity.setSelected (mFilter.getCaseSensitive ());
106
107         // add the locales choice
108
mLocale = new JComboBox JavaDoc ();
109         synchronized (mLocale)
110         {
111             mLocale.addItem (mFilter.getLocale ().getDisplayName ());
112             thread = new Thread JavaDoc (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     //
124
// Filter overrides and concrete implementations
125
//
126

127     public String JavaDoc getDescription ()
128     {
129         return ("Nodes containing string");
130     }
131
132     public String JavaDoc 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         // should we complain?
166
}
167
168     public String JavaDoc toJavaCode (StringBuffer JavaDoc out, int[] context)
169     {
170         String JavaDoc 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     //
205
// NodeFilter interface
206
//
207

208     public boolean accept (Node node)
209     {
210         return (mFilter.accept (node));
211     }
212
213     //
214
// ActionListener interface
215
//
216

217     /**
218      * Invoked when an action occurs on the combo box.
219      */

220     public void actionPerformed (ActionEvent JavaDoc event)
221     {
222         Object JavaDoc source;
223         boolean sensitive;
224         Object JavaDoc[] selection;
225         String JavaDoc 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 JavaDoc)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     //
252
// Runnable interface
253
//
254
public void run()
255     {
256         String JavaDoc 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     //
270
// DocumentListener interface
271
//
272

273     public void insertUpdate (DocumentEvent JavaDoc e)
274     {
275         Document JavaDoc doc;
276         
277         doc = e.getDocument ();
278         try
279         {
280             mFilter.setPattern (doc.getText (0, doc.getLength ()));
281         }
282         catch (BadLocationException JavaDoc ble)
283         {
284             ble.printStackTrace ();
285         }
286     }
287
288     public void removeUpdate (DocumentEvent JavaDoc e)
289     {
290         Document JavaDoc doc;
291         
292         doc = e.getDocument ();
293         try
294         {
295             mFilter.setPattern (doc.getText (0, doc.getLength ()));
296         }
297         catch (BadLocationException JavaDoc ble)
298         {
299             ble.printStackTrace ();
300         }
301     }
302
303     public void changedUpdate (DocumentEvent JavaDoc e)
304     {
305         // plain text components don't fire these events
306
}
307 }
308
Popular Tags