View Javadoc
1   /*
2    * Copyright (c) 2009-2011, EzWare
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    * Redistributions of source code must retain the above copyright notice,
10   * this list of conditions and the following disclaimer.Redistributions
11   * in binary form must reproduce the above copyright notice, this list of
12   * conditions and the following disclaimer in the documentation and/or
13   * other materials provided with the distribution.Neither the name of the
14   * EzWare nor the names of its contributors may be used to endorse or
15   * promote products derived from this software without specific prior
16   * written permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28   * POSSIBILITY OF SUCH DAMAGE.
29   *
30   * %%Ignore-License%%
31   */
32  
33  package jaxx.runtime.swing.table.filter;
34  
35  import java.io.Serializable;
36  import java.util.Collection;
37  import java.util.Collections;
38  import java.util.Set;
39  
40  import javax.swing.DefaultRowSorter;
41  import javax.swing.JTable;
42  import javax.swing.RowFilter;
43  import javax.swing.RowSorter;
44  import javax.swing.table.TableModel;
45  import javax.swing.table.TableRowSorter;
46  
47  public class JTableFilter extends AbstractTableFilter<JTable> {
48  
49  	private static final long serialVersionUID = 1L;
50  
51  	private final TableRowFilter filter = new TableRowFilter();
52  
53  	public JTableFilter( JTable table) {
54  		super(table);
55  	}
56  
57  	@Override
58  	protected boolean execute(int col, Collection<Object> items) {
59  
60  		RowSorter<?> rs = getTable().getRowSorter();
61  
62  		if (!( rs instanceof DefaultRowSorter )) return false;
63  
64  		DefaultRowSorter<?, ?> drs = (DefaultRowSorter<?, ?>) rs;
65  
66  		@SuppressWarnings("unchecked")
67  		RowFilter<Object,Object> prevFilter = (RowFilter<Object, Object>) drs.getRowFilter();
68  		if ( !(prevFilter instanceof TableRowFilter)) {
69  			filter.setParentFilter(prevFilter);
70  		}
71  
72  		drs.setRowFilter(filter);
73  		return true;
74  
75  	}
76  
77  	class TableRowFilter extends RowFilter<Object,Object> implements Serializable {
78  
79  		private static final long serialVersionUID = 1L;
80  
81  		private RowFilter<Object, Object> parentFilter;
82  
83  		public RowFilter<Object, Object> getParentFilter() {
84  			return parentFilter;
85  		}
86  
87  		public void setParentFilter( RowFilter<Object, Object> parentFilter ) {
88  			this.parentFilter = (parentFilter == null ||  parentFilter == this )? null: parentFilter;
89  		}
90  
91  		@Override
92  		public boolean include( final RowFilter.Entry<? extends Object, ? extends Object> entry) {
93  
94  			// use parent filter condition
95  			if ( parentFilter != null && !parentFilter.include(entry)) return false;
96  
97  			return includeRow( new TableFilter.Row() {
98  
99  				@Override
100 				public Object getValue(int column) { return entry.getValue(column); }
101 
102 				@Override
103 				public int getValueCount() { return entry.getValueCount(); }
104 
105 			});
106 
107 		}
108 
109 	}
110 
111 	public void modelChanged( TableModel model ) {
112 		getTable().setRowSorter(  new TableRowSorter<TableModel>( model ));
113 	}
114 
115     @Override
116     public Set<Object> distinctValuesForColumn(int i) {
117         //TODO
118         return null;
119     }
120 
121     @Override
122     public String toString(Object o) {
123         //TODO
124         return null;
125     }
126 
127     public void clear() {
128         super.clear();
129 		Collection<Object> items = Collections.emptyList();
130 		for( int column=0; column<getTable().getModel().getColumnCount(); column++) {
131 			execute(column, items);
132 		}
133     }
134 }