package action;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class LoadFileAction extends AbstractAction {
	/*
	 * button to load glycan sequences into input area from a file
	 */
	JFileChooser chooser ;
	JPanel jp;
	JTextArea input;
	JTextField jf;
	
	public LoadFileAction(JPanel jp, JTextField jf, JTextArea input){
		chooser = new JFileChooser();
		this.input = input;
		this.jp = jp;
		this.jf = jf;
	}
	
	public void actionPerformed(ActionEvent arg0) {
//		select file containing glycan sequences, extract it and put all in the text area
		
		int returnValue = chooser.showOpenDialog(null);
		if (returnValue == JFileChooser.APPROVE_OPTION) {
			File selectedFile = chooser.getSelectedFile();
			System.out.println(selectedFile.getPath());
			jf.setText(selectedFile.getPath());
			
			String str ="";
			Scanner scanfile;
			try {
				scanfile = new Scanner (new FileReader(selectedFile));
				while (scanfile.hasNextLine())
					str = str + "\n" + scanfile.nextLine();
			}
			catch (FileNotFoundException e) {
				e.printStackTrace();
			}		
			input.setText(str);
		}
	}
}
