// Oefening ReadSequentialFile, D&D 5, blz 858,..

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

import be.hogent.h17.H17_BankUI;
import be.hogent.h17.H17_AccountRecord;

public class H17_ReadSequentialFile extends JFrame {
	
	private ObjectInputStream input;
	private H17_BankUI userInterface;
	private JButton nextButton, openButton;
	
	public H17_ReadSequentialFile() {
		super ("Uitlezen van een sequentieel bestand van Objects");
		
		userInterface = new H17_BankUI(4);
		getContentPane().add(userInterface, BorderLayout.CENTER);
		
		openButton = userInterface.getDoTask1Button();
		openButton.setText("Open Bestand");
		
		openButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					openFile();
				}
			}
		);
		
		addWindowListener(
			new WindowAdapter() {
				public void windowClosing(WindowEvent event) {
					if (input != null)
						closeFile();
					
					System.exit(0);
				}
			}
		);
		
		nextButton = userInterface.getDoTask2Button();
		nextButton.setText("Volgend Record");
		nextButton.setEnabled(false);
		
		nextButton.addActionListener(
			new ActionListener() {
				public void actionPerformed( ActionEvent event) {
					readRecord();
				}
			}
		);
		
		pack();
		setSize(300,200);
		setVisible(true);
	}
	
	public void openFile() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode (JFileChooser.FILES_ONLY);
		
		int result = fileChooser.showOpenDialog(this);
		
		if (result == JFileChooser.CANCEL_OPTION)
			return;
		
		File fileName = fileChooser.getSelectedFile();
		
		if (fileName == null || fileName.getName().equals(""))
			JOptionPane.showMessageDialog (this, "Ongeldige bestandsnaam", "Ongeldige bestandsnaam",JOptionPane.ERROR_MESSAGE);
		else {
			
			try {
				input = new ObjectInputStream (new FileInputStream(fileName));
				
				openButton.setEnabled(false);
				nextButton.setEnabled(true);
			}
			catch (IOException ioException) {
				JOptionPane.showMessageDialog (this, "Fout bij het openen", "Error",JOptionPane.ERROR_MESSAGE);
			}
		}
		
	}
	
	public void readRecord() {
		
		H17_AccountRecord record;
		
		try {
			record = (H17_AccountRecord) input.readObject();
			
			String values[] = { String.valueOf(record.getAccount()),record.getFirstName(),record.getLastName(),String.valueOf(record.getBalance())};
			
			userInterface.setFieldValues(values);
		}
		catch (EOFException endOfFileException) {
			nextButton.setEnabled(false);
			
			JOptionPane.showMessageDialog(this, "Geen records meer in het bestand","Einde van bestand",JOptionPane.ERROR_MESSAGE);
		}
		catch (ClassNotFoundException classNotFoundException) {
			JOptionPane.showMessageDialog(this, "Fout bij het maken van object","Class niet gevonden",JOptionPane.ERROR_MESSAGE);
		}
		catch (IOException ioException) {
			JOptionPane.showMessageDialog(this, "Fout bij het lezen van bestand","Leesfout",JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void closeFile() {
		try {
			input.close();
			System.exit(0);
		}
		catch (IOException ioException) {
			JOptionPane.showMessageDialog(this, "Fout bij het sluiten", "Error", JOptionPane.ERROR_MESSAGE);
			System.exit(1);
		}
	}
	
	public static void main (String args[]) {
		new H17_ReadSequentialFile();
	}
	
}
