package _VisualDVM.Visual.Menus;
import Common.Utils.Utils_;
import Common.Visual.Controls.StableMenuItem;
import Common.Visual.Menus.GraphMenu;
import Common.Visual.Menus.VisualiserMenuItem;
import Common.Visual.Selectable;
import Common.Visual.Trees.DataTree;
import Common.Visual.Trees.SelectableTree;
import _VisualDVM.Global;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public abstract class SelectionTreeMenu extends GraphMenu<DataTree> {
    VisualiserMenuItem m_select_for_current;
    VisualiserMenuItem m_unselect_for_current;
    VisualiserMenuItem m_select_all;
    VisualiserMenuItem m_unselect_all;
    public SelectionTreeMenu(SelectableTree tree_in) {
        super(tree_in, "");
        addSeparator();
        //-
        m_select_all = new StableMenuItem("Выбрать всё", "/Common/icons/SelectAll.png");
        m_select_all.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SelectAll(true);
                tree.updateUI();
            }
        });
        add(m_select_all);
        m_unselect_all = new StableMenuItem("Отменить всё", "/Common/icons/UnselectAll.png");
        m_unselect_all.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SelectAll(false);
                tree.updateUI();
            }
        });
        add(m_unselect_all);
        addSeparator();
        m_select_for_current = new VisualiserMenuItem("");
        m_select_for_current.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object o = Global.mainModule.get(tree.currentName());
                if (o instanceof Selectable) {
                    ((Selectable) o).SelectAllChildren(true);
                }
                tree.updateUI();
            }
        });
        add(m_select_for_current);
        //--------
        m_unselect_for_current = new VisualiserMenuItem("");
        m_unselect_for_current.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object o = Global.mainModule.get(tree.currentName());
                if (o instanceof Selectable) {
                    ((Selectable) o).SelectAllChildren(false);
                }
                tree.updateUI();
            }
        });
        //-------
        add(m_unselect_for_current);
    }
    public abstract Class getTargetClass(); //для кого позволяется выбирать всех потомков.
    public abstract void SelectAll(boolean select);
    @Override
    public void CheckElementsVisibility() {
        Object current = Global.mainModule.get(tree.currentName());
        if ((current != null) && (current.getClass().equals(getTargetClass()))) {
            String name = ((Selectable) current).getSelectionText();
            m_select_for_current.setText("Выбрать всё для " +
                    Utils_.Brackets(name));
            m_unselect_for_current.setText("Отменить выбор всех для " +
                    Utils_.Brackets(name));
            //-
            m_select_for_current.setVisible(true);
            m_unselect_for_current.setVisible(true);
        } else {
            m_select_for_current.setVisible(false);
            m_unselect_for_current.setVisible(false);
        }
    }
}
