001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.databinding.datagrid.api.filter;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: /**
025: * <p>
026: * The FilterModel class supports grouping a data grid's {@link Filter} objects into a single JavaBean that
027: * implements functionality for interacting with them together.
028: * </p>
029: * <p>
030: * The list of {@link Filter} objects in a FilterModel are ordered. This class provides methods that
031: * can be used to find all the filters for a data grid and to determine if a given filter expression is sorted.
032: * </p>
033: */
034: public class FilterModel implements java.io.Serializable {
035:
036: private List/*<Filter>*/_filters = null;
037:
038: /**
039: * Construct an a filter model given a {@link List} of {@link Filter} instances.
040: *
041: * @param filters the filters for a data grid.
042: */
043: public FilterModel(List/*<Filter>*/filters) {
044: _filters = filters;
045: }
046:
047: /**
048: * Get the {@link List} of filters for a data grid.
049: * @return a data grid's fitlers
050: */
051: public List/*<Filter>*/getFilters() {
052: if (_filters == null)
053: return null;
054: else
055: return _filters;
056: }
057:
058: /**
059: * Get a {@link List} of {@link Filter} objects. The list returned will contain
060: * all of the {@link Filter} objects whose {@link Filter#getFilterExpression()} matches the given
061: * <code>filterExpression</code>.
062: * @param filterExpression the expression whose matching filters to find
063: * @return <code>null</code> if no matching {@link Filter} objects are found; a {@link List} of {@link Filter}
064: * objects otherwise.
065: */
066: public List/*<Filter>*/getFilters(String filterExpression) {
067: if (_filters == null || filterExpression == null)
068: return null;
069: else
070: return lookupFilters(filterExpression);
071: }
072:
073: /**
074: * Utility method that checks to see if the given <code>filterExpression</code> matches any of the current
075: * {@link Filter} instances.
076: * @param filterExpression the filter expression to check
077: * @return <code>true</code> if at least one filter matches the <code>filterExpression</code>; <code>false</code>
078: * otherwise.
079: */
080: public boolean isFiltered(String filterExpression) {
081: if (_filters == null || filterExpression == null)
082: return false;
083:
084: ArrayList list = lookupFilters(filterExpression);
085: if (list != null && list.size() > 0)
086: return true;
087: else
088: return false;
089: }
090:
091: /**
092: * Internal method used to lookup {@link Filter} instances by <code>filterExpression</code>.
093: * @param filterExpression the filter expression whose filters to find
094: * @return <code>null</code> if no matching {@link Filter} objects are found; a {@link List} of {@link Filter}
095: * objects otherwise.
096: */
097: private ArrayList/*<Filter>*/lookupFilters(String filterExpression) {
098: assert filterExpression != null;
099: assert !filterExpression.equals("");
100:
101: /* todo: perf. caching or abstraction to make this faster */
102: ArrayList/*<Filter>*/filters = new ArrayList/*<Filter>*/();
103: for (int i = 0; i < _filters.size(); i++) {
104: assert _filters.get(i) instanceof Filter;
105: Filter filter = (Filter) _filters.get(i);
106: if (filter.getFilterExpression().equals(filterExpression))
107: filters.add(filter);
108: }
109:
110: return filters.size() > 0 ? filters : null;
111: }
112: }
|