/*
 * Copyright (C) 2025 ctecinf.com.br
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.com.hobbyslotcar.racedirector;

import java.awt.Component;
import java.awt.Dimension;
import java.util.Collection;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Cássio Conceição
 * @param <T>
 * @since 07/03/2025
 * @version 2503
 * @see http://hobbyslotcar.com.br
 */
public class List<T> extends JList<T> {

    public List() {
        this(null);
    }

    public List(Collection<T> list) {
        super(new DefaultListModel<T>());
        if (list != null) {
            getModel().addAll(list);
        }
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        setPreferredSize(new Dimension(300, 0));
        setCellRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                setBorder(BorderFactory.createEmptyBorder());
                return this;
            }
        });
    }

    public void addElement(T element) {
        getModel().addElement(element);
    }

    public void removeElement(T element) {
        getModel().removeElement(element);
    }

    @Override
    public final DefaultListModel<T> getModel() {
        return (DefaultListModel<T>) super.getModel();
    }

}
