package action;

import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class DownloadPicturesAction extends AbstractAction {
/*
 * download displayed pictures in a folder of your choice
 */
	byte[] image;
	JPanel jp;
	
	public DownloadPicturesAction(byte[] image, JPanel jp){
		this.image = image;
		this.jp = jp;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		//convert byte into pictures and save them		  
		 
		try {	
			InputStream in = new ByteArrayInputStream(image);
			BufferedImage bImageFromConvert = ImageIO.read(in);
			JFileChooser chooser = new JFileChooser();
			int returnValue = chooser.showOpenDialog(null);
			if (returnValue == JFileChooser.APPROVE_OPTION) {
				File selectedFile = chooser.getSelectedFile();
				ImageIO.write(bImageFromConvert, "png", selectedFile);
			}
		}
		catch (IOException e) {
			JOptionPane.showMessageDialog(jp, "error during pictures download", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
}
