001package ball.swing.table;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: ListTableModel.java 6118 2020-06-04 19:31:45Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/swing/table/ListTableModel.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.List;
024import javax.swing.event.TableModelEvent;
025import lombok.ToString;
026
027/**
028 * {@link List} {@link javax.swing.table.TableModel} implementation.
029 *
030 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
031 * @version $Revision: 6118 $
032 */
033@ToString
034public class ListTableModel extends ArrayListTableModel<Object> {
035    private static final long serialVersionUID = -7448852717174575332L;
036
037    /** @serial */ private final List<?> list;
038
039    /**
040     * @see AbstractTableModelImpl#AbstractTableModelImpl(String...)
041     *
042     * @param   list            The underlying {@link List}.
043     * @param   name            The column name.
044     */
045    public ListTableModel(List<?> list, String name) {
046        super(list, new String[] { name });
047
048        this.list = list;
049    }
050
051    /**
052     * @see AbstractTableModelImpl#AbstractTableModelImpl(int)
053     *
054     * @param   list             The underlying {@link List}.
055     */
056    public ListTableModel(List<?> list) { this(list, null); }
057
058    @Override
059    protected Object getValueAt(Object row, int x) {
060        Object value = null;
061
062        switch (x) {
063        case 0:
064        default:
065            value = row;
066            break;
067        }
068
069        return value;
070    }
071
072    @Override
073    public void tableChanged(TableModelEvent event) {
074        list().clear();
075        list().addAll(list);
076
077        super.tableChanged(event);
078    }
079}