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