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