📄 filterpipeline.java
字号:
/* * $Id: FilterPipeline.java,v 1.14 2005/10/13 08:59:52 kleopatra Exp $ * * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */package org.jdesktop.swingx.decorator;import java.util.BitSet;import java.util.List;import java.util.Vector;import javax.swing.event.EventListenerList;/** * <p>A <b><code>FilterPipeline</code></b> is used to define the set of * {@link org.jdesktop.swingx.decorator.Filter filters} * for a data-aware component such as a {@link org.jdesktop.swingx.JXList} or a * {@link org.jdesktop.swingx.JXTable}. Filtering involves interposing one or * more filters in a {@link org.jdesktop.swingx.decorator.FilterPipeline} between * a data model and a view to change the apparent order and/or number of records * in the data model. The order of filters in the filter pipeline determines the * order in which each filter is applied. The output from one filter in the * pipeline is piped as the input to the next filter in the pipeline.</p> * * <pre> * {@link org.jdesktop.swingx.decorator.Filter}[] filters = new {@link org.jdesktop.swingx.decorator.Filter}[] { * new {@link org.jdesktop.swingx.decorator.PatternFilter}("S.*", 0, 1), // regex, matchflags, column * new {@link org.jdesktop.swingx.decorator.ShuttleSorter}(1, false), // column 1, descending * new {@link org.jdesktop.swingx.decorator.ShuttleSorter}(0, true), // column 0, ascending * }; * {@link org.jdesktop.swingx.decorator.FilterPipeline} pipeline = new {@link org.jdesktop.swingx.decorator.FilterPipeline}(filters); * {@link org.jdesktop.swingx.JXTable} table = new {@link org.jdesktop.swingx.JXTable}(model); * table.setFilters(pipeline); * </pre> * * This is all you need to do in order to use <code>FilterPipeline</code>. Most * of the methods in this class are only for advanced developers who want to write * their own filter subclasses and want to override the way a filter pipeline works. * * @author Ramesh Gupta * @see org.jdesktop.swingx.decorator.Filter */public class FilterPipeline { protected EventListenerList listenerList = new EventListenerList(); private ComponentAdapter adapter = null; private Sorter sorter = null; private final Filter[] filters; /** * Creates an empty open pipeline. * */ public FilterPipeline() { this(new Filter[] {}); } /** * Constructs a new <code>FilterPipeline</code> populated with the specified * filters that are applied in the order they appear in the list. Since filters * maintain state about the view to which they are attached, an instance of * a filter may not ever be used in more than one pipeline. * * @param inList array of filters */ public FilterPipeline(Filter[] inList) { filters = reorderSorters(inList, locateSorters(inList)); assignFilters(); } /* * JW let each contained filter assign both order and pipeline. * Now we have a invariant * * (containedFilter.order >= 0) && (containedFilter.pipeline != null) * * which simplifies access logic (IMO) * */ private void assignFilters() { for (int i = 0; i < filters.length; i++) { // JW: changed to bind early and move // binding responsibility to filter // instead of fiddling around in other's bowels filters[i].assign(this, i); } } /** * Sets the sorter that the output of the filter pipeline is piped through. * This is the sorter that is installed interactively on a view by a user * action. * * This method is responsible for doing all the bookkeeping to assign/cleanup * pipeline/adapter assignments. * * @param sorter the interactive sorter, if any; null otherwise. */ public void setSorter(Sorter sorter) { Sorter oldSorter = getSorter(); if (oldSorter == sorter) return; if (oldSorter != null) { oldSorter.assign((FilterPipeline) null); } this.sorter = sorter; if (sorter != null) { sorter.assign((FilterPipeline) null); sorter.assign(this); if (adapter != null) { sorter.assign(adapter); sorter.refresh(); } } if ((sorter == null) && isAssigned()) { fireContentsChanged(); } } /** * returns the interactive sorter or null if none is set. * * @return */ public Sorter getSorter() { return sorter; } /** * Assigns a {@link org.jdesktop.swingx.decorator.ComponentAdapter} to this * pipeline if no adapter has previously been assigned to the pipeline. Once an * adapter has been assigned to this pipeline, any attempt to change that will * cause an exception to be thrown. * * @param adapter the <code>ComponentAdapter</code> to assign * @throws IllegalArgumentException if adapter is null * @throws IllegalStateException if an adapter is already assigned to this * pipeline and the new adapter is not the same the existing adapter */ public final void assign(ComponentAdapter adapter) { if (adapter == null) { throw new IllegalArgumentException("null adapter"); } // also assign individual filters when adapter is bound if (this.adapter == null) { this.adapter = adapter; for (int i = 0; i < filters.length; i++) { filters[i].assign(adapter); } if (sorter != null) { sorter.assign(adapter); } flush(); } else if (this.adapter != adapter){ throw new IllegalStateException("Can't bind to a different adapter"); } } /** * * @return */ public boolean isAssigned() { return adapter != null; } /** * Returns true if this pipeline contains the specified filter; * otherwise it returns false. * * @param filter filter whose membership in this pipeline is tested * @return true if this pipeline contains the specified filter; * otherwise it returns false * @throws NullPointerException if filter == null * */ boolean contains(Filter filter) { return (filter.equals(sorter)) || (filter.order >= 0) && (filters.length > 0) && (filters[filter.order] == filter); } /** * Returns the first filter, if any, in this pipeline, or null, if there are * no filters in this pipeline. * * @return the first filter, if any, in this pipeline, or null, if there are * no filters in this pipeline */ Filter first() { return (filters.length > 0) ? filters[0] : null; } /** * Returns the last filter, if any, in this pipeline, or null, if there are * no filters in this pipeline. * * @return the last filter, if any, in this pipeline, or null, if there are * no filters in this pipeline */ Filter last() { if (sorter != null) return sorter; return (filters.length > 0) ? filters[filters.length - 1] : null; } /** * Returns the filter after the supplied filter in this pipeline, or null, * if there aren't any filters after the supplied filter. * * @param filter a filter in this pipeline * @return the filter after the supplied filter in this pipeline, or null, * if there aren't any filters after the supplied filter */ Filter next(Filter filter) { if (last().equals(filter)) return null; return filter.order + 1 < filters.length ? filters[filter.order + 1] : getSorter(); } /** * Returns the filter before the supplied filter in this pipeline, * or null, if there aren't any filters before the supplied filter. * * @param filter a filter in this pipeline * @return the filter before the supplied filter in this pipeline, * or null, if there aren't any filters before the supplied filter */ Filter previous(Filter filter) { if (filter.equals(sorter)) return filters.length > 0 ? filters[filters.length - 1] : null; return first().equals(filter) ? null : filters[filter.order - 1]; } /** * Called when the specified filter has changed. * Cascades <b><code>filterChanged</code></b> notifications to the next * filter in the pipeline after the specified filter. If the specified filter * is the last filter in the pipeline, this method broadcasts a * <b><code>filterChanged</code></b> notification to all * <b><code>PipelineListener</code></b> objects registered with this pipeline. * * @param filter a filter in this pipeline that has changed in any way */ protected void filterChanged(Filter filter) { Filter next = next(filter); if (next == null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -