package _VisualDVM.ProjectData.Project.UI;
import Common.Visual.Menus.GraphMenu;
import Common.Visual.Selectable;
import Common.Visual.Trees.DataTree;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Visual.Menus.VersionsMenu;

import javax.swing.tree.TreePath;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class VersionsTree extends DataTree {
    public VersionsTree() {
        super(Global.mainModule.getRoot().node);
        SelectCurrentProject();
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_DELETE:
                        Global.mainModule.getPass(PassCode.DeleteVersion).Do();
                        break;
                    case KeyEvent.VK_ENTER:
                        if (Global.mainModule.HasVersion())
                            Global.mainModule.getPass(PassCode.OpenCurrentProject).Do(Global.mainModule.getVersion());
                        break;
                }
            }
        });
    }
    @Override
    public Class getRendererClass() {
        return RendererProjectVersion.class;
    }
    @Override
    public Current currentName() {
        return Current.Version;
    }
    public void SelectCurrentProject() {
        setSelectionPath(new TreePath(Global.mainModule.getProject().node.getPath()));
    }
    @Override
    protected GraphMenu createMenu() {
        return new VersionsMenu(this);
    }
    @Override
    public void LeftMouseAction1() {
        if (Global.versions_multiselection) {
            // только если есть режим выбора версий.
            Object element = Global.mainModule.get(currentName());
            if ((element instanceof Selectable)) {
                ((Selectable) element).SwitchSelection();
                updateUI();
            }
        }
    }
    @Override
    public void LeftMouseAction2() {
        Global.mainModule.getPass(PassCode.OpenCurrentProject).Do(Global.mainModule.getVersion());
    }
    @Override
    protected int getStartLine() {
        return 1;
    }
}