/*
 * @(#)Lab2-5.java 1.0 03/02/05
 *
 * Driver file with GUI
 *
 */

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

class Lab2_5 extends JFrame {
	
	private JButton doit = new JButton("Do It");
	private JTextField index = new JTextField(8);
	private JTextField status = new JTextField();
	
	public Lab2_5() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
				System.exit(0);
			}
		});
		
		Container c = getContentPane();
		JPanel stuff = new JPanel();
		
		c.add(status, BorderLayout.SOUTH);
		c.add(stuff, BorderLayout.CENTER);
		
		stuff.add(doit);
		stuff.add(index);
		
		stuff.setBorder(BorderFactory.createTitledBorder("Enter an index:"));
		status.setBorder(BorderFactory.createTitledBorder("Status:"));
		
		ActionListener action = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				status.setText("");
				try {
					int someIndex = Integer.parseInt(index.getText());
					testArrayIndex2(someIndex);
				}
				catch(NumberFormatException ex) {
					status.setText("bad input for int");	
				}
				catch(Exception ex) {
					status.setText("unknown exception");	
				}
			}
		};
		
		/*ActionListener selectText = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				index.selectAll();	
				index.requestFocus();
			}
		};
		*/
		
		ActionListener selectText = new SelectText(index);
		
		doit.addActionListener(action);
		doit.addActionListener(selectText);
		index.addActionListener(action);
		index.addActionListener(selectText);
	}
	
	private class SelectText implements ActionListener {
		private JTextField txtFld;
		
		public SelectText(JTextField txt) {
			txtFld = txt;
		}
		
		public void actionPerformed(ActionEvent e) {
			txtFld.selectAll();	
			txtFld.requestFocus();
		}
	}

	public static void main(String args[]) {
		
		Lab2_5 mainFrame = new Lab2_5();
		mainFrame.setSize(400, 400);
		mainFrame.setTitle("Lab2_5");
		mainFrame.setVisible(true);
	}
	
	public void testArrayIndex(int index) {
		int[] n = new int[10];
		
		try {
			n[index] = 1;
		}
		catch(ArrayIndexOutOfBoundsException e) {
			status.setText("index out of bounds: " + index);
		}
		finally {
			status.setText(status.getText() + " finally");	
		}	
	}
	
	public void testArrayIndex2(int index) {
		MyArray ma = new MyArray();
		
		try {
			ma.set(index, 1);
		}
		catch(ArrayIndexOutOfBoundsException e) {
			status.setText("index out of bounds: " + index);
		}
		catch(ArithmeticException e) {
			status.setText("arithmetic exception");	
		}
		finally {
			status.setText(status.getText() + " finally");	
		}	
	}
}

// Cheesy Array Class

public class MyArray {
	
	private final int SIZE = 10;
	private int[] nums = new int[SIZE];
	
	public void set(int index, int value) throws Exception {
		try {
			nums[index] = value;
		}
		catch(Exception e) {
			throw e;
			//throw (ArrayIndexOutOfBoundsException)e;
			//throw new ArithmeticException();
		}
		System.out.println("ouch");	
	}	
	
	public int get(int index) {
		return nums[index];	
	}
	
	public int length() {
		return SIZE;	
	}
	
}