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.Arrays;
022import lombok.ToString;
023
024/**
025 * Simple {@link javax.swing.table.TableModel} implementation.
026 *
027 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
028 */
029@ToString
030public class SimpleTableModel extends ArrayListTableModel<Object[]> {
031    private static final long serialVersionUID = 4763741378592706296L;
032
033    /**
034     * @see ArrayListTableModel#ArrayListTableModel(Iterable,String...)
035     *
036     * @param   rows            The {@link javax.swing.table.TableModel}'s
037     *                          rows.
038     * @param   names           The column names.
039     */
040    public SimpleTableModel(Object[][] rows, String... names) {
041        super(Arrays.asList(rows), names);
042    }
043
044    /**
045     * @see ArrayListTableModel#ArrayListTableModel(Iterable,int)
046     *
047     * @param   rows            The TableModel's rows.
048     * @param   columns         The number of columns.
049     */
050    public SimpleTableModel(Object[][] rows, int columns) {
051        this(rows, new String[columns]);
052    }
053
054    /**
055     * Convenience method to add a new row.
056     *
057     * @param   row             The array of {@link Object}s that make up
058     *                          the row to be added.
059     *
060     * @return  {@link.this} {@link SimpleTableModel}.
061     */
062    public SimpleTableModel row(Object... row) {
063        list().add(row);
064
065        fireTableStructureChanged();
066        fireTableDataChanged();
067
068        return this;
069    }
070
071    @Override
072    protected Object getValueAt(Object[] row, int x) { return row[x]; }
073}