View Javadoc

1   /*
2    * $Id: SortableFilterableTable.java 178 2010-10-31 18:01:20Z roland $
3    * Copyright (C) 2007-2010 Roland Krueger
4    * Created on 08.02.2010
5    * 
6    * Author: Roland Krueger (www.rolandkrueger.info)
7    *
8    * This file is part of RoKlib.
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public License
12   * as published by the Free Software Foundation; either version 2.1 of
13   * the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful, but
16   * WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23   * USA
24   */
25  package info.rolandkrueger.roklib.ui.swing.table;
26  
27  import info.rolandkrueger.roklib.util.tables.filtertable.ITableDataColumnHeader;
28  import info.rolandkrueger.roklib.util.tables.filtertable.SortableFilterableTableDataModel;
29  import info.rolandkrueger.roklib.util.tables.filtertable.SortableFilterableTableDataModel.SearchMode;
30  
31  import java.util.Vector;
32  
33  import javax.swing.JTable;
34  import javax.swing.ListSelectionModel;
35  import javax.swing.table.TableColumnModel;
36  import javax.swing.table.TableModel;
37  
38  public class SortableFilterableTable<T, H extends ITableDataColumnHeader> extends JTable
39  {
40    private static final long serialVersionUID = - 4117999556318274032L;
41  
42    private SortableFilterableTableDataModel<T, H> mTableModel;
43    private SearchMode mSearchCapability;
44  
45    public SortableFilterableTable (SearchMode searchCapability)
46    {
47      super ();
48      mSearchCapability = searchCapability;
49    }
50  
51    public SortableFilterableTable (SortableFilterableTableDataModel<T, H> dm,
52        SearchMode searchCapability)
53    {
54      this (searchCapability);
55      setModel (dm);
56    }
57  
58    public SortableFilterableTable (int numRows, int numColumns, SearchMode searchCapability)
59    {
60      this (searchCapability);
61      mTableModel = new SortableFilterableTableDataModel<T, H> (numColumns, searchCapability);
62      for (int i = 0; i < numColumns * numRows; ++i)
63        mTableModel.addStringData ("");
64    }
65  
66    public SortableFilterableTable (Object[][] rowData, Object[] columnNames,
67        SearchMode searchCapability)
68    {
69      this (searchCapability);
70      mTableModel = new SortableFilterableTableDataModel<T, H> (columnNames.length, searchCapability);
71      // TODO:...
72    }
73  
74    public SortableFilterableTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm,
75        SearchMode searchCapability)
76    {
77      this (dm, searchCapability);
78      setColumnModel (cm);
79      setSelectionModel (sm);
80    }
81  
82    public SortableFilterableTable (TableModel dm, TableColumnModel cm, SearchMode searchCapability)
83    {
84      this (dm, searchCapability);
85      setColumnModel (cm);
86    }
87  
88    public SortableFilterableTable (TableModel dm, SearchMode searchCapability)
89    {
90      this (searchCapability);
91      setModel (dm);
92    }
93  
94    @SuppressWarnings ("unchecked")
95    public SortableFilterableTable (Vector rowData, Vector columnNames, SearchMode searchCapability)
96    {
97      // TODO:...
98    }
99  
100   @Override
101   public void setModel (TableModel dataModel)
102   {
103     super.setModel (dataModel);
104     mTableModel = new SortableFilterableTableDataModel<T, H> (dataModel, mSearchCapability);
105     TableColumnSingleInputCellRenderer<T, H> renderer = new TableColumnSingleInputCellRenderer<T, H> (
106         mSearchCapability, mTableModel);
107     for (int i = 0; i < mTableModel.getColumnCount (); ++i)
108     {
109 
110       getColumnModel ().getColumn (i).setHeaderRenderer (renderer);
111     }
112 
113     repaint ();
114   }
115 }