package Common.Visual.Menus;
import Common.Database.Tables.DataSet;
import Common.Passes.PassCode_;
import Common.Visual.Controls.MenuBarButton;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class DataMenuBar extends VisualiserMenuBar {
    public JLabel countLabel = null;
    //-
    public ActionListener selectAllListener = null;
    public ActionListener unselectAllListener = null;
    JButton selectAllButton = null;
    JButton unselectAllButton = null;
    private DataSet dataSource = null;
    //-
    public DataMenuBar(String dataName, PassCode_... passes) {
        add(new JLabel(dataName + " : "));
        add(countLabel = new JLabel("?"));
        addPasses(passes);
    }
    public void createSelectionButtons(DataSet dataSource_in) {
        dataSource = dataSource_in;
        java.awt.Dimension d = new Dimension(25, 25);
        if (selectAllButton == null) {
            add(selectAllButton = new MenuBarButton() {
                {
                    setIcon("/Common/icons/SelectAll.png");
                    setToolTipText("Выбрать всё");
                    setPreferredSize(d);
                    setMinimumSize(d);
                    setMaximumSize(d);
                }
            }, 0);
        }
        if (unselectAllButton == null) {
            add(unselectAllButton = new MenuBarButton() {
                {
                    setIcon("/Common/icons/UnselectAll.png");
                    setToolTipText("Отменить всё");
                    setPreferredSize(d);
                    setMinimumSize(d);
                    setMaximumSize(d);
                }
            }, 1);
        }
        if (selectAllListener != null) {
            selectAllButton.removeActionListener(selectAllListener);
        }
        selectAllButton.addActionListener(selectAllListener = e -> getDataSource().getUI().SelectAll(true));
        if (unselectAllListener != null) {
            unselectAllButton.removeActionListener(unselectAllListener);
        }
        unselectAllButton.addActionListener(unselectAllListener = e -> getDataSource().getUI().SelectAll(false));
    }
    public DataSet getDataSource() {
        return dataSource;
    }
    public void setDataSource(DataSet dataSource) {
        this.dataSource = dataSource;
    }
}
