package _VisualDVM.Visual.Menus.MainMenuBar;
import Common.Visual.Menus.VisualiserMenu;
import Common.Visual.Menus.VisualiserMenuItem;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.DBLastProject.DBLastProject;
import _VisualDVM.Passes.PassCode;

import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Vector;
public class LastOpenedProjectsMenu extends VisualiserMenu {
    public LastOpenedProjectsMenu() {
        super("Недавние проекты", "/Common/icons/Search.png");
        addMenuListener(new MenuListener() {
            @Override
            public void menuSelected(MenuEvent e) {
                removeAll();
                Vector<DBLastProject> projects = (Global.mainModule.getDb()).lastProjects.getOrdered();
                int k = 1;
                for (DBLastProject p : projects) {
                    if (new File(p.HomePath).exists() && p.lastOpened != 0) {
                        VisualiserMenuItem i = new VisualiserMenuItem(p.HomePath);
                        i.addActionListener(new AbstractAction() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                Global.mainModule.getPass(PassCode.OpenCurrentProject).Do(new File(p.HomePath));
                            }
                        });
                        add(i);
                        ++k;
                        if (k > Global.normalProperties.LastOpenedProjectsCount)
                            break;
                    }
                }
            }
            @Override
            public void menuDeselected(MenuEvent e) {
            }
            @Override
            public void menuCanceled(MenuEvent e) {
            }
        });
    }
}
