View Javadoc

1   /***
2    * Copyright (c) 2003 held jointly by the individual authors.            
3    *                                                                          
4    * This library is free software; you can redistribute it and/or modify it    
5    * under the terms of the GNU Lesser General Public License as published      
6    * by the Free Software Foundation; either version 2.1 of the License, or 
7    * (at your option) any later version.                                            
8    *                                                                            
9    * This library is distributed in the hope that it will be useful, but 
10   * WITHOUT ANY WARRANTY; with out even the implied warranty of 
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
12   * GNU Lesser General Public License for more details.                                                  
13   *                                                                           
14   * You should have received a copy of the GNU Lesser General Public License   
15   * along with this library;  if not, write to the Free Software Foundation,   
16   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.              
17   *                                                                            
18   * > http://www.gnu.org/copyleft/lesser.html                                  
19   * > http://www.opensource.org/licenses/lgpl-license.php
20   */
21  package net.mlw.vlh.swing.support;
22  
23  import javax.swing.JTable;
24  
25  /***
26   * @author Matthew L. Wilson
27   * @version $Revision: 1.1 $ $Date: 2005/08/30 18:54:25 $
28   */
29  public class SelectionSupport
30  {
31     public static void maintainSelectedRows(KeyRetriever keyRetriever, JTable table, AbstractValueListTableModel model, TableSorter sorter,
32           Runnable runnable)
33     {
34        String key = null;
35        int row = table.getSelectedRow();
36  
37        if (table.getSelectedRow() != -1)
38        {
39           int selectedRow = table.getSelectedRow();
40           int sortedRow = sorter.modelIndex(selectedRow);
41           key = keyRetriever.getKey(model.getBean(sortedRow));
42        }
43  
44        runnable.run();
45  
46        if (key != null)
47        {
48           int topIndex = 0;
49           int bottomIndex = 0;
50           int length = model.getRowCount();
51           for (int i = 1;; i++)
52           {
53              if (((topIndex = row + i) < length) || ((bottomIndex = row - i) > -1))
54              {
55                 if (topIndex < length)
56                 {
57                    Object bean = model.getBean(sorter.modelIndex(topIndex));
58                    if (key.equals(keyRetriever.getKey(bean)))
59                    {
60                       table.getSelectionModel().clearSelection();
61                       table.getSelectionModel().setLeadSelectionIndex(topIndex);
62                       break;
63                    }
64                 }
65  
66                 if (bottomIndex > -1)
67                 {
68                    Object bean = model.getBean(sorter.modelIndex(bottomIndex));
69                    if (key.equals(keyRetriever.getKey(bean)))
70                    {
71                       table.getSelectionModel().clearSelection();
72                       table.getSelectionModel().setLeadSelectionIndex(bottomIndex);
73                       break;
74                    }
75                 }
76              }
77              else
78              {
79                 break;
80              }
81           }
82        }
83     }
84  
85     public interface KeyRetriever
86     {
87        String getKey(Object bean);
88     }
89  }