001/* 002 * $Id$ 003 * 004 * Copyright 2009 Sun Microsystems, Inc., 4150 Network Circle, 005 * Santa Clara, California 95054, U.S.A. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 020 * 021 */ 022package org.jdesktop.swingx.sort; 023 024import javax.swing.ListModel; 025 026/** 027 * A SortController to use with JXList. 028 * 029 * @author Jeanette Winzenburg 030 */ 031public class ListSortController<M extends ListModel> extends DefaultSortController<M> { 032 033 /** underlying model */ 034 private M listModel; 035 /** 036 * @param model 037 */ 038 public ListSortController(M model) { 039 setModel(model); 040 } 041 042 /** 043 * Sets the <code>TableModel</code> to use as the underlying model 044 * for this <code>TableRowSorter</code>. A value of <code>null</code> 045 * can be used to set an empty model. 046 * 047 * @param model the underlying model to use, or <code>null</code> 048 */ 049 public void setModel(M model) { 050 listModel = model; 051 if (model != null) 052 cachedModelRowCount = model.getSize(); 053 setModelWrapper(new ListRowSorterModelWrapper()); 054 } 055 056 /** 057 * Implementation of DefaultRowSorter.ModelWrapper that delegates to a 058 * TableModel. 059 */ 060 private class ListRowSorterModelWrapper extends ModelWrapper<M,Integer> { 061 @Override 062 public M getModel() { 063 return listModel; 064 } 065 066 @Override 067 public int getColumnCount() { 068 return (listModel == null) ? 0 : 1; 069 } 070 071 @Override 072 public int getRowCount() { 073 return (listModel == null) ? 0 : listModel.getSize(); 074 } 075 076 @Override 077 public Object getValueAt(int row, int column) { 078 return listModel.getElementAt(row); 079 } 080 081 @Override 082 public String getStringValueAt(int row, int column) { 083 return getStringValueProvider().getStringValue(row, column) 084 .getString(getValueAt(row, column)); 085 } 086 087 @Override 088 public Integer getIdentifier(int index) { 089 return index; 090 } 091 } 092 093}