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

public class HandleFile extends HandleEvent{
    public HandleFile(CalculatorWindow window) {
        super(window);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==window.copyBtn){
            window.computerProcess.copy();
        }
        if(e.getSource()==window.clearTextBtn){
            window.computerProcess.setText(null);
        }
        if(e.getSource()==window.saveBtn){
            JFileChooser chooser=new JFileChooser();
            int state=chooser.showSaveDialog(null);
            File file =chooser.getSelectedFile();
            if(file!=null&&state==JFileChooser.APPROVE_OPTION){
                try {
                    String content=window.computerProcess.getText();
                    StringReader reader=new StringReader(content);
                    BufferedReader in=new BufferedReader(reader);
                    FileWriter fw=new FileWriter(file);
                    BufferedWriter out=new BufferedWriter(fw);
                    String s=null;
                    while ((s=in.readLine())!=null){
                        out.write(s);
                        out.newLine();
                    }
                    in.close();
                    out.close();
                }catch (IOException exception){

                }
            }
        }
    }
}
