001package ball.swing.table;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: MapsTableModel.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/MapsTableModel.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.ArrayList;
024import java.util.Iterator;
025import java.util.Map;
026import lombok.ToString;
027
028/**
029 * {@link Map}s {@link javax.swing.table.TableModel} implementation.
030 *
031 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
032 * @version $Revision: 6118 $
033 */
034@ToString
035public class MapsTableModel extends MapTableModel {
036    private static final long serialVersionUID = -8967894056049317574L;
037
038    /** @serial */
039    private final ArrayList<Map<?,?>> list = new ArrayList<>();
040
041    /**
042     * @see MapTableModel#MapTableModel(Map,String...)
043     *
044     * @param   iterable        The {@link Iterable} of underlying
045     *                          {@link Map}s.
046     * @param   names           The column names.
047     */
048    public MapsTableModel(Iterable<? extends Map<?,?>> iterable,
049                          String... names) {
050        this(iterable.iterator(), names);
051    }
052
053    /**
054     * @see MapTableModel#MapTableModel(Map,int)
055     *
056     * @param   iterable        The {@link Iterable} of underlying
057     *                          {@link Map}s.
058     * @param   columns         The number of columns.
059     */
060    public MapsTableModel(Iterable<? extends Map<?,?>> iterable, int columns) {
061        this(iterable, new String[columns]);
062    }
063
064    /**
065     * @see MapsTableModel#MapsTableModel(Iterable,String...)
066     *
067     * @param   iterator        The {@link Iterator} of underlying
068     *                          {@link Map}s.
069     * @param   names           The column names.
070     */
071    protected MapsTableModel(Iterator<? extends Map<?,?>> iterator,
072                             String... names) {
073        super(iterator.next(), names);
074
075        list.add(super.map());
076
077        while (iterator.hasNext()) {
078            list.add(iterator.next());
079        }
080    }
081
082    @Override
083    protected Object getValueAt(Object row, int x) {
084        Object value = null;
085
086        switch (x) {
087        case 0:
088        case 1:
089            value = super.getValueAt(row, x);
090            break;
091
092        default:
093            value = list.get(x - 1).get(row);
094            break;
095        }
096
097        return value;
098    }
099}