package Action;

import IHM.WorkingImage;
import Window.Interface;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.File;

public class ActionOpenProject extends AbstractAction {

    public ActionOpenProject(String text, String desc, Integer mnemonic) {
        super(text);
        putValue(SHORT_DESCRIPTION, desc);
        putValue(MNEMONIC_KEY, mnemonic);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser jc = new JFileChooser();
        if (jc.showOpenDialog(Interface.getInstance()) == JFileChooser.APPROVE_OPTION) {
            File file = jc.getSelectedFile();
            if (!file.getPath().endsWith(".myPSD"))
                return;
            Interface.getInstance().remove(WorkingImage.getInstance());
            WorkingImage.getInstance().readFromFile(file);
            Interface.getInstance().remove(Interface.getInstance().getScrollPane());
            JScrollPane scrollPane = new JScrollPane(WorkingImage.getInstance());
            Interface.getInstance().setContentPane(scrollPane);
            Interface.getInstance().setMenuBar();
            Interface.getInstance().showWindow();
        }
    }
}