OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 112 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.openconcerto.modules.operation;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JList;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;

import org.openconcerto.ui.list.CheckListItem;
import org.openconcerto.ui.list.CheckListRenderer;

public class CheckList<T> extends JList<CheckListItem> {

    public CheckList(CheckListModel<T> statesModel) {
        super(statesModel);
        setCellRenderer(new CheckListRenderer());
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent event) {
                final JList<?> list = (JList<?>) event.getSource();
                final CheckListModel<?> model = (CheckListModel<?>) list.getModel();
                final int index = list.locationToIndex(event.getPoint());
                if (index >= 0 && index < model.getSize()) {
                    if (event.getClickCount() == 1) {
                        model.invertCheckAt(index);
                    } else {
                        model.checkAllAs(index);
                    }
                }
            }

        });
    }

    @Override
    public void setModel(ListModel<CheckListItem> model) {
        if (!(model instanceof CheckListModel))
            throw new IllegalArgumentException("Not a " + CheckListModel.class + " : " + model);
        if (((CheckListModel<?>) model).getItemClass() != this.getModel().getItemClass())
            throw new IllegalArgumentException("Not same item class");
        super.setModel(model);
    }

    @SuppressWarnings("unchecked")
    @Override
    public CheckListModel<T> getModel() {
        return (CheckListModel<T>) super.getModel();
    }

    public boolean isAllSelected() {
        final CheckListModel model = getModel();
        final int size = model.getSize();
        for (int i = 0; i < size; i++) {
            final CheckListItem item = model.getElementAt(i);
            if (!item.isSelected()) {
                return false;
            }
        }
        return true;
    }

    @Deprecated
    public List<T> getSelectedObjects() {
        return getModel().getCheckedObjects();
    }
}