KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > databinding > datagrid > api > filter > FilterModel


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.databinding.datagrid.api.filter;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 /**
24  * <p>
25  * The FilterModel class supports grouping a data grid's {@link Filter} objects into a single JavaBean that
26  * implements functionality for interacting with them together.
27  * </p>
28  * <p>
29  * The list of {@link Filter} objects in a FilterModel are ordered. This class provides methods that
30  * can be used to find all the filters for a data grid and to determine if a given filter expression is sorted.
31  * </p>
32  */

33 public class FilterModel
34     implements java.io.Serializable JavaDoc {
35
36     private List JavaDoc/*<Filter>*/ _filters = null;
37
38     /**
39      * Construct an a filter model given a {@link List} of {@link Filter} instances.
40      *
41      * @param filters the filters for a data grid.
42      */

43     public FilterModel(List JavaDoc/*<Filter>*/ filters) {
44         _filters = filters;
45     }
46
47     /**
48      * Get the {@link List} of filters for a data grid.
49      * @return a data grid's fitlers
50      */

51     public List JavaDoc/*<Filter>*/ getFilters() {
52         if(_filters == null)
53             return null;
54         else
55             return _filters;
56     }
57
58     /**
59      * Get a {@link List} of {@link Filter} objects. The list returned will contain
60      * all of the {@link Filter} objects whose {@link Filter#getFilterExpression()} matches the given
61      * <code>filterExpression</code>.
62      * @param filterExpression the expression whose matching filters to find
63      * @return <code>null</code> if no matching {@link Filter} objects are found; a {@link List} of {@link Filter}
64      * objects otherwise.
65      */

66     public List JavaDoc/*<Filter>*/ getFilters(String JavaDoc filterExpression) {
67         if(_filters == null || filterExpression == null)
68             return null;
69         else return lookupFilters(filterExpression);
70     }
71
72     /**
73      * Utility method that checks to see if the given <code>filterExpression</code> matches any of the current
74      * {@link Filter} instances.
75      * @param filterExpression the filter expression to check
76      * @return <code>true</code> if at least one filter matches the <code>filterExpression</code>; <code>false</code>
77      * otherwise.
78      */

79     public boolean isFiltered(String JavaDoc filterExpression) {
80         if(_filters == null || filterExpression == null)
81             return false;
82
83         ArrayList JavaDoc list = lookupFilters(filterExpression);
84         if(list != null && list.size() > 0)
85             return true;
86         else return false;
87     }
88
89     /**
90      * Internal method used to lookup {@link Filter} instances by <code>filterExpression</code>.
91      * @param filterExpression the filter expression whose filters to find
92      * @return <code>null</code> if no matching {@link Filter} objects are found; a {@link List} of {@link Filter}
93      * objects otherwise.
94      */

95     private ArrayList JavaDoc/*<Filter>*/ lookupFilters(String JavaDoc filterExpression) {
96         assert filterExpression != null;
97         assert !filterExpression.equals("");
98
99         /* todo: perf. caching or abstraction to make this faster */
100         ArrayList JavaDoc/*<Filter>*/ filters = new ArrayList JavaDoc/*<Filter>*/();
101         for(int i = 0; i < _filters.size(); i++) {
102             assert _filters.get(i) instanceof Filter;
103             Filter filter = (Filter)_filters.get(i);
104             if(filter.getFilterExpression().equals(filterExpression))
105                 filters.add(filter);
106         }
107
108         return filters.size() > 0 ? filters : null;
109     }
110 }
111
Popular Tags