001package ball.swing.table;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: AbstractTableModelImpl.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/AbstractTableModelImpl.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 javax.swing.event.TableModelEvent;
025import javax.swing.event.TableModelListener;
026import javax.swing.table.AbstractTableModel;
027import lombok.ToString;
028
029/**
030 * {@link AbstractTableModel} implementation.
031 *
032 * {@bean.info}
033 *
034 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
035 * @version $Revision: 6118 $
036 */
037@ToString
038public abstract class AbstractTableModelImpl extends AbstractTableModel
039                                             implements TableModelListener {
040    private static final long serialVersionUID = -4459832803497493630L;
041
042    /** @serial */ private String[] names = new String[] { };
043    /** @serial */ private Class<?>[] types = new Class<?>[] { };
044
045    /**
046     * Construct a {@link javax.swing.table.TableModel} with the specified
047     * column names.
048     *
049     * @param   names           The column names.
050     */
051    protected AbstractTableModelImpl(String... names) {
052        super();
053
054        if (names.length > 0) {
055            this.names = Arrays.copyOf(names, names.length);
056            this.types = Arrays.copyOf(types, names.length);
057        } else {
058            throw new IllegalArgumentException("names="
059                                               + Arrays.asList(names));
060        }
061
062        addTableModelListener(this);
063    }
064
065    /**
066     * Construct a {@link javax.swing.table.TableModel} with the specified
067     * number of columns.
068     *
069     * @param   columns         The number of columns.
070     */
071    protected AbstractTableModelImpl(int columns) {
072        this(new String[columns]);
073    }
074
075    /**
076     * Convenience method to get the column names as an array.
077     *
078     * @return  The array of column names.
079     */
080    protected String[] header() {
081        String[] header = new String[getColumnCount()];
082
083        for (int x = 0; x < header.length; x += 1) {
084            header[x] = getColumnName(x);
085        }
086
087        return header;
088    }
089
090    /**
091     * Convenience method to get a column's values as an array.
092     *
093     * @param   x               The column index.
094     *
095     * @return  The array of column values.
096     */
097    protected Object[] column(int x) {
098        Object[] column = new Object[getRowCount()];
099
100        for (int y = 0; y < column.length; y += 1) {
101            column[y] = getColumnClass(x).cast(getValueAt(y, x));
102        }
103
104        return column;
105    }
106
107    /**
108     * Convenience method to get a row's values as an array.
109     *
110     * @param   y               The row index.
111     *
112     * @return  The array of row values.
113     */
114    protected Object[] row(int y) {
115        Object[] row = new Object[getColumnCount()];
116
117        for (int x = 0; x < row.length; x += 1) {
118            row[x] = getColumnClass(x).cast(getValueAt(y, x));
119        }
120
121        return row;
122    }
123
124    public int getColumnCount() { return names.length; }
125
126    @Override
127    public Class<?> getColumnClass(int x) {
128        return (types[x] != null) ? types[x] : super.getColumnClass(x);
129    }
130
131    public void setColumnClass(int x, Class<?> type) { types[x] = type; }
132
133    @Override
134    public String getColumnName(int x) { return names[x]; }
135
136    public void setColumnName(int x, String name) { names[x] = name; }
137
138    @Override
139    public abstract int getRowCount();
140
141    @Override
142    public boolean isCellEditable(int y, int x) { return false; }
143
144    @Override
145    public abstract Object getValueAt(int y, int x);
146
147    @Override
148    public void setValueAt(Object value, int y, int x) {
149        throw new UnsupportedOperationException("setValueAt(Object,int,int)");
150    }
151
152    @Override
153    public void tableChanged(TableModelEvent event) { }
154}