View Javadoc

1   /*
2    * $Id: TableColumnSingleInputCellRenderer.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.ISortableFilterableTableListener;
28  import info.rolandkrueger.roklib.util.tables.filtertable.ITableDataColumnHeader;
29  import info.rolandkrueger.roklib.util.tables.filtertable.SortableFilterableTableDataModel;
30  import info.rolandkrueger.roklib.util.tables.filtertable.SortableFilterableTableDataModel.SearchMode;
31  
32  import java.awt.Component;
33  import java.awt.GridBagConstraints;
34  import java.awt.GridBagLayout;
35  import java.awt.Insets;
36  import java.awt.event.MouseEvent;
37  import java.awt.event.MouseListener;
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  import javax.swing.BorderFactory;
42  import javax.swing.JLabel;
43  import javax.swing.JPanel;
44  import javax.swing.JTable;
45  import javax.swing.JTextField;
46  import javax.swing.border.EtchedBorder;
47  import javax.swing.event.CaretEvent;
48  import javax.swing.event.CaretListener;
49  
50  public class TableColumnSingleInputCellRenderer<T, H extends ITableDataColumnHeader> extends
51      AbstractTableColumnInputCellRenderer<T, H> implements ISortableFilterableTableListener
52  {
53    private List<TableColumnSingleInputComponent> mComponentList;
54  
55    public TableColumnSingleInputCellRenderer (SearchMode mode,
56        SortableFilterableTableDataModel<T, H> dataModel)
57    {
58      super (mode, dataModel);
59      dataModel.addSortableFilterableTableListener (this);
60      mComponentList = new ArrayList<TableColumnSingleInputComponent> (dataModel.getColumnCount ());
61      for (int i = 0; i < dataModel.getColumnCount (); ++i)
62      {
63        mComponentList.add (null);
64      }
65    }
66  
67    public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected,
68        boolean hasFocus, int row, int column)
69    {
70      TableColumnSingleInputComponent component = mComponentList.get (column);
71      if (component == null)
72      {
73        component = new TableColumnSingleInputComponent (column);
74        mComponentList.set (column, component);
75      }
76      return component;
77    }
78  
79    private class TableColumnSingleInputComponent extends JPanel implements MouseListener
80    {
81      private static final long serialVersionUID = 6266420577278257939L;
82      private int mColumnIndex;
83      private JTextField mTextField;
84      private JLabel mHeaderLabel;
85  
86      public TableColumnSingleInputComponent (int columnIndex)
87      {
88        mColumnIndex = columnIndex;
89        initialize ();
90      }
91  
92      private void initialize ()
93      {
94        GridBagConstraints gridBagConstraints1 = new GridBagConstraints ();
95        gridBagConstraints1.fill = GridBagConstraints.BOTH;
96        gridBagConstraints1.gridy = 1;
97        gridBagConstraints1.weightx = 1.0;
98        gridBagConstraints1.anchor = GridBagConstraints.CENTER;
99        gridBagConstraints1.insets = new Insets (2, 5, 2, 5);
100       gridBagConstraints1.gridx = 0;
101       GridBagConstraints gridBagConstraints = new GridBagConstraints ();
102       gridBagConstraints.gridx = 0;
103       gridBagConstraints.gridy = 0;
104       mHeaderLabel = new JLabel ();
105       this.setSize (300, 80);
106       this.setLayout (new GridBagLayout ());
107       this.add (mHeaderLabel, gridBagConstraints);
108       this.add (getTextField (), gridBagConstraints1);
109       this.setBorder (BorderFactory.createEtchedBorder (EtchedBorder.LOWERED));
110     }
111 
112     public void reset ()
113     {
114       mTextField.setText ("");
115     }
116 
117     private JTextField getTextField ()
118     {
119       if (mTextField == null)
120       {
121         mTextField = new JTextField ();
122         mTextField.addCaretListener (new CaretListener ()
123         {
124           public void caretUpdate (CaretEvent e)
125           {
126             TableColumnSingleInputCellRenderer.this.getDataModel ().setPrefixFilter (
127                 mTextField.getText (), mColumnIndex, true);
128           }
129         });
130       }
131       return mTextField;
132     }
133 
134     public void mouseClicked (MouseEvent e)
135     {
136       System.out.println ("Click!");
137     }
138 
139     public void mouseEntered (MouseEvent e)
140     {
141       System.out.println ("Entered!");
142     }
143 
144     public void mouseExited (MouseEvent e)
145     {
146       System.out.println ("Exited!");
147     }
148 
149     public void mousePressed (MouseEvent e)
150     {
151       // TODO Auto-generated method stub
152 
153     }
154 
155     public void mouseReleased (MouseEvent e)
156     {
157       // TODO Auto-generated method stub
158 
159     }
160   }
161 
162   public void resetAllFilters ()
163   {
164     for (TableColumnSingleInputComponent c : mComponentList)
165     {
166       c.reset ();
167     }
168   }
169 }