KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > parserapplications > filterbuilder > SubFilterList


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/SubFilterList.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 22:45:48 $
10
// $Revision: 1.2 $
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;
28
29 import java.awt.*;
30
31 import javax.swing.*;
32 import javax.swing.border.*;
33
34 import org.htmlparser.NodeFilter;
35 import org.htmlparser.parserapplications.filterbuilder.layouts.VerticalLayoutManager;
36
37 public class SubFilterList
38     extends
39         JPanel
40 {
41     protected int mExtra = 25; // for now
42

43     protected Component mSpacer;
44     
45     protected Filter mHome;
46
47     protected String JavaDoc mTitle;
48
49     protected int mMax;
50
51     /**
52      * Creates a container panel.
53      * Set the panel minimum size to the same width as the container
54      * but with a bit of extra length.
55      * @param home The filter we belong to.
56      * @param title The border title.
57      * @param max The maximum number of filters in the list (0 for no limit).
58      */

59     public SubFilterList (Filter home, String JavaDoc title, int max)
60     {
61         mHome = home;
62         mTitle = title;
63         mMax = max;
64
65         // not quite:
66
// new BoxLayout (this, BoxLayout.Y_AXIS));
67
setLayout (new VerticalLayoutManager ());
68         addSpacer ();
69         setSelected (false);
70     }
71
72     /**
73      * Set the 'selected look' for the component.
74      * @param selected If <code>true</code>, 'select' this component,
75      * otherwise 'deselect' it.
76      */

77     public void setSelected (boolean selected)
78     {
79         if (selected)
80             setBorder (
81                     new CompoundBorder (
82                         new TitledBorder (
83                             null,
84                             mTitle,
85                             TitledBorder.LEFT,
86                             TitledBorder.TOP),
87                             new CompoundBorder (
88                                 new LineBorder (Color.green, 2),
89                                 new EmptyBorder (1, 1, 1, 1))));
90         else
91             setBorder (
92                     new CompoundBorder (
93                         new TitledBorder (
94                             null,
95                             mTitle,
96                             TitledBorder.LEFT,
97                             TitledBorder.TOP),
98                         new EmptyBorder (3,3,3,3)));
99     }
100     
101     protected void addSpacer ()
102     {
103         Dimension dimension;
104         Insets insets;
105         
106         // set the command area size by adding a rigid area
107
dimension = mHome.getSize ();
108         insets = mHome.getInsets ();
109         // todo: this should resize with the container
110
dimension.setSize (dimension.width - insets.left - insets.right, mExtra);
111         mSpacer = Box.createRigidArea (dimension);
112         add (mSpacer);
113     }
114
115     protected void removeSpacer ()
116     {
117         remove (mSpacer);
118         mSpacer = null;
119     }
120     /**
121      * Get the components in which to drop commands.
122      * @return The component to act as a drop target.
123      */

124     public Component[] getDropTargets ()
125     {
126         return (new Component[] { this });
127     }
128
129     /**
130      * Add a filter to the container contents.
131      * @param filter The command to add to the container.
132      */

133     public void addFilter (Filter filter)
134     {
135         int count;
136         
137         count = getComponentCount ();
138         if (null != mSpacer)
139             count--; // insert before the spacer
140
addFilter (filter, count);
141     }
142
143     /**
144      * Add a filter to the container at a specific position.
145      * @param filter The filter to add to the container.
146      * @param index The index at which to add it.
147      */

148     public void addFilter (Filter filter, int index)
149     {
150         NodeFilter[] before;
151         NodeFilter[] after;
152         int offset;
153
154         add (filter, index);
155         before = mHome.getSubNodeFilters ();
156         after = new NodeFilter[before.length + 1];
157         offset = 0;
158         for (int i = 0; i < after.length; i++)
159             after[i] = (i == index) ? filter : before[offset++];
160         mHome.setSubNodeFilters (after);
161         if ((null != mSpacer) && (0 != mMax) && (after.length >= mMax))
162             removeSpacer ();
163     }
164
165     /**
166      * Remove a filter from the container.
167      * @param filter The filter to remove from the container.
168      */

169     public void removeFilter (Filter filter)
170     {
171         Filter[] filters;
172         int index;
173         
174         filters = getFilters ();
175         index = -1;
176         for (int i = 0; ((-1 == index) && (i < filters.length)); i++)
177             if (filter == filters[i])
178                 index = i;
179         if (-1 != index)
180             removeFilter (index);
181     }
182
183     /**
184      * Remove a filter from the container.
185      * @param index The index of the filter to remove from the container.
186      */

187     public void removeFilter (int index)
188     {
189         NodeFilter[] before;
190         NodeFilter[] after;
191         int offset;
192
193         remove (index);
194         before = mHome.getSubNodeFilters ();
195         if (0 != before.length)
196         {
197             after = new NodeFilter[before.length - 1];
198             offset = 0;
199             for (int i = 0; i < before.length; i++)
200                 if (i != index)
201                     after[offset++] = before[i];
202             mHome.setSubNodeFilters (after);
203             if ((null == mSpacer) && (0 != mMax) && (after.length < mMax))
204                 addSpacer ();
205         }
206     }
207
208     /**
209      * Return the list of filters in this container.
210      * @return The list of contained filters.
211      */

212     public Filter[] getFilters ()
213     {
214         NodeFilter[] list;
215         Filter[] ret;
216
217         list = mHome.getSubNodeFilters ();
218         ret = new Filter[list.length];
219         System.arraycopy (list, 0, ret, 0, list.length);
220
221         return (ret);
222     }
223
224     public boolean canAccept ()
225     {
226         int count;
227         boolean ret;
228
229         if (0 == mMax)
230             ret = true;
231         else
232         {
233             count = getComponentCount ();
234             if (null != mSpacer)
235                 count--;
236             ret = count < mMax;
237         }
238
239         return (ret);
240     }
241
242     /**
243      * Get the bytes for this command as a String.
244      * @param indent The number of spaces to indent a block.
245      * @param level The current indentation level.
246      * The first non-whitespace character should be at
247      * indented <code>indent</code> * <code>level</code> spaces.
248      * @return The string representing this command.
249      */

250     public String JavaDoc toString (int indent, int level)
251     {
252         Filter[] filters;
253         StringBuffer JavaDoc ret;
254         
255         ret = new StringBuffer JavaDoc ();
256
257         filters = getFilters ();
258         for (int i = 0; i < filters.length; i++)
259         {
260             ret.append (filters[i].toString ());
261             if (i + 1 != filters.length)
262                 ret.append ("\n");
263         }
264         
265         return (ret.toString ());
266     }
267     
268 }
269
Popular Tags