001package ball.swing.table;
002/*-
003 * ##########################################################################
004 * Utilities
005 * %%
006 * Copyright (C) 2008 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import java.util.List;
022import javax.swing.event.TableModelEvent;
023import lombok.ToString;
024
025/**
026 * {@link List} {@link javax.swing.table.TableModel} implementation.
027 *
028 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
029 */
030@ToString
031public class ListTableModel extends ArrayListTableModel<Object> {
032    private static final long serialVersionUID = -7448852717174575332L;
033
034    /** @serial */ private final List<?> list;
035
036    /**
037     * @see AbstractTableModelImpl#AbstractTableModelImpl(String...)
038     *
039     * @param   list            The underlying {@link List}.
040     * @param   name            The column name.
041     */
042    public ListTableModel(List<?> list, String name) {
043        super(list, new String[] { name });
044
045        this.list = list;
046    }
047
048    /**
049     * @see AbstractTableModelImpl#AbstractTableModelImpl(int)
050     *
051     * @param   list             The underlying {@link List}.
052     */
053    public ListTableModel(List<?> list) { this(list, null); }
054
055    @Override
056    protected Object getValueAt(Object row, int x) {
057        Object value = null;
058
059        switch (x) {
060        case 0:
061        default:
062            value = row;
063            break;
064        }
065
066        return value;
067    }
068
069    @Override
070    public void tableChanged(TableModelEvent event) {
071        list().clear();
072        list().addAll(list);
073
074        super.tableChanged(event);
075    }
076}