package Action;

import IHM.WorkingImage;
import Window.Interface;

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

public class ActionSaveProject extends AbstractAction {
    public ActionSaveProject(String text, String desc, Integer mnemonic) {
        super(text);
        putValue(SHORT_DESCRIPTION, desc);
        putValue(Action.MNEMONIC_KEY, mnemonic);
        if (WorkingImage.getInstance().getImages().size() == 0)
            setEnabled(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (WorkingImage.getInstance().getImages().size() == 0)
            return;
        JFileChooser jc = new JFileChooser();
        if (jc.showSaveDialog(Interface.getInstance()) == JFileChooser.APPROVE_OPTION) {
            File file = jc.getSelectedFile();
            String path = file.getPath();
            if (!path.endsWith(".myPSD"))
                path = path + ".myPSD";
            file = new File(path);
            try {
                WorkingImage.getInstance().writeToFile(file);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}