001package ball.swing.table; 002/*- 003 * ########################################################################## 004 * Utilities 005 * $Id: ArrayListTableModel.java 5285 2020-02-05 04:23:21Z ball $ 006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/swing/table/ArrayListTableModel.java $ 007 * %% 008 * Copyright (C) 2008 - 2020 Allen D. Ball 009 * %% 010 * Licensed under the Apache License, Version 2.0 (the "License"); 011 * you may not use this file except in compliance with the License. 012 * You may obtain a copy of the License at 013 * 014 * http://www.apache.org/licenses/LICENSE-2.0 015 * 016 * Unless required by applicable law or agreed to in writing, software 017 * distributed under the License is distributed on an "AS IS" BASIS, 018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 019 * See the License for the specific language governing permissions and 020 * limitations under the License. 021 * ########################################################################## 022 */ 023import java.util.ArrayList; 024import java.util.Collection; 025 026/** 027 * Abstract base class for {@link javax.swing.table.TableModel} 028 * implementations based on an {@link ArrayList}. 029 * 030 * @param <R> The type of table row. 031 * 032 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball} 033 * @version $Revision: 5285 $ 034 */ 035public abstract class ArrayListTableModel<R> extends AbstractTableModelImpl { 036 private static final long serialVersionUID = -7927458440773401575L; 037 038 /** @serial */ private final ArrayList<R> list = new ArrayList<>(); 039 040 /** 041 * @see AbstractTableModelImpl#AbstractTableModelImpl(String...) 042 * 043 * @param iterable The {@link Iterable} of row values. 044 * @param names The column names. 045 */ 046 protected ArrayListTableModel(Iterable<? extends R> iterable, 047 String... names) { 048 super(names); 049 050 if (iterable != null) { 051 for (R element : iterable) { 052 list.add(element); 053 } 054 } 055 } 056 057 /** 058 * @see AbstractTableModelImpl#AbstractTableModelImpl(int) 059 * 060 * @param iterable The {@link Iterable} of row values. 061 * @param columns The number of columns. 062 */ 063 protected ArrayListTableModel(Iterable<? extends R> iterable, 064 int columns) { 065 this(iterable, new String[columns]); 066 } 067 068 /** 069 * Method to access the underlying row {@link java.util.List}. 070 * 071 * @return The underlying row {@link java.util.List}. 072 */ 073 public ArrayList<R> list() { return list; } 074 075 @Override 076 public int getRowCount() { return list().size(); } 077 078 /** 079 * Implementation method to retrieve a column value from a row object. 080 * 081 * @param row The {@link Object} representing the row. 082 * @param x The column index. 083 * 084 * @return The column value from the row. 085 */ 086 protected abstract Object getValueAt(R row, int x); 087 088 @Override 089 public Object getValueAt(int y, int x) { 090 return getColumnClass(x).cast(getValueAt(list().get(y), x)); 091 } 092}