KickJava   Java API By Example, From Geeks To Geeks.

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


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/HasChildFilterWrapper.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
32 import javax.swing.JCheckBox JavaDoc;
33
34 import org.htmlparser.Node;
35 import org.htmlparser.NodeFilter;
36 import org.htmlparser.Parser;
37 import org.htmlparser.filters.HasChildFilter;
38 import org.htmlparser.parserapplications.filterbuilder.Filter;
39 import org.htmlparser.parserapplications.filterbuilder.SubFilterList;
40
41 /**
42  * Wrapper for HasChildFilters.
43  */

44 public class HasChildFilterWrapper
45     extends
46         Filter
47     implements
48         ActionListener JavaDoc
49 {
50     /**
51      * The underlying filter.
52      */

53     protected HasChildFilter mFilter;
54
55     /**
56      * The check box for recursion.
57      */

58     protected JCheckBox JavaDoc mRecursive;
59
60     /**
61      * The drop target container.
62      */

63     protected SubFilterList mContainer;
64     
65     /**
66      * Create a wrapper over a new HasChildFilter.
67      */

68     public HasChildFilterWrapper ()
69     {
70         mFilter = new HasChildFilter ();
71
72         // add the recursive flag
73
mRecursive = new JCheckBox JavaDoc ("Recursive");
74         add (mRecursive);
75         mRecursive.addActionListener (this);
76         mRecursive.setSelected (mFilter.getRecursive ());
77
78         // add the subfilter container
79
mContainer = new SubFilterList (this, "Child Filter", 1);
80         add (mContainer);
81     }
82
83     //
84
// Filter overrides and concrete implementations
85
//
86

87     public String JavaDoc getDescription ()
88     {
89         return ("Has Child");
90     }
91
92     public String JavaDoc getIconSpec ()
93     {
94         return ("images/HasChildFilter.gif");
95     }
96
97     public NodeFilter getNodeFilter ()
98     {
99         NodeFilter filter;
100         HasChildFilter ret;
101         
102         ret = new HasChildFilter ();
103
104         ret.setRecursive (mFilter.getRecursive ());
105         filter = mFilter.getChildFilter ();
106         if (null != filter)
107             ret.setChildFilter (((Filter)filter).getNodeFilter ());
108             
109         return (ret);
110     }
111
112     public void setNodeFilter (NodeFilter filter, Parser context)
113     {
114         mFilter = (HasChildFilter)filter;
115         mRecursive.setSelected (mFilter.getRecursive ());
116     }
117
118     public NodeFilter[] getSubNodeFilters ()
119     {
120         NodeFilter filter;
121         NodeFilter[] ret;
122
123         filter = mFilter.getChildFilter ();
124         if (null != filter)
125             ret = new NodeFilter[] { filter };
126         else
127             ret = new NodeFilter[0];
128
129         return (ret);
130     }
131
132     public void setSubNodeFilters (NodeFilter[] filters)
133     {
134         if (0 != filters.length)
135             mFilter.setChildFilter (filters[0]);
136         else
137             mFilter.setChildFilter (null);
138     }
139
140     public String JavaDoc toJavaCode (StringBuffer JavaDoc out, int[] context)
141     {
142         String JavaDoc name;
143         String JavaDoc ret;
144
145         if (null != mFilter.getChildFilter ())
146             name = ((Filter)mFilter.getChildFilter ()).toJavaCode (out, context);
147         else
148             name = null;
149         ret = "filter" + context[1]++;
150         spaces (out, context[0]);
151         out.append ("HasChildFilter ");
152         out.append (ret);
153         out.append (" = new HasChildFilter ();");
154         newline (out);
155         spaces (out, context[0]);
156         out.append (ret);
157         out.append (".setRecursive (");
158         out.append (mFilter.getRecursive () ? "true" : "false");
159         out.append (");");
160         newline (out);
161         if (null != name)
162         {
163             spaces (out, context[0]);
164             out.append (ret);
165             out.append (".setChildFilter (");
166             out.append (name);
167             out.append (");");
168             newline (out);
169         }
170         
171         return (ret);
172     }
173
174     //
175
// NodeFilter interface
176
//
177

178     public boolean accept (Node node)
179     {
180         return (mFilter.accept (node));
181     }
182
183     //
184
// ActionListener interface
185
//
186

187     /**
188      * Invoked when an action occurs on the check box.
189      */

190     public void actionPerformed (ActionEvent JavaDoc event)
191     {
192         Object JavaDoc source;
193         boolean recursive;
194
195         source = event.getSource ();
196         if (source == mRecursive)
197         {
198             recursive = mRecursive.isSelected ();
199             mFilter.setRecursive (recursive);
200         }
201     }
202 }
203
Popular Tags