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