package todolist;

// 2001-05-14  	changes trying to force display of the buttons on start up (had to re-size to get them to appear).
//		
//		resolved by moving frame.show() stmt after init and start in main routine. See 2001-05-14 as marked.
//
//		M.Cook 2001-05-14 		


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
/**
 * Insert the type's description here.
 * Creation date: (9/16/2000 12:44:40 AM)
 * @author: 
 */
public class ToDoList extends JApplet implements KeyListener, MouseListener, MouseMotionListener, WindowListener {
	private JPanel ivjJAppletContentPane = null;
	private JButton ivjJButton1 = null;
	private JButton ivjJButton2 = null;
	private JButton ivjJButton3 = null;
	private JButton ivjJButton4 = null;
	private JLabel ivjJLabel1 = null;
	private JList ivjJList1 = null;
	private JScrollPane ivjJScrollPane1 = null;
	private JTextField ivjJTextField1 = null;
	private DefaultListModel ivjDefaultListModel1 = null;
	IvjEventHandler ivjEventHandler = new IvjEventHandler();
	static String FILE_NAME = "ToDoList.txt";

class IvjEventHandler implements java.awt.event.ActionListener, javax.swing.event.ListSelectionListener {
		public void actionPerformed(java.awt.event.ActionEvent e) {
			if (e.getSource() == ToDoList.this.getJButton1()) 
				connEtoM1(e);
			if (e.getSource() == ToDoList.this.getJButton2()) 
				connEtoM2(e);
			if (e.getSource() == ToDoList.this.getJButton1()) 
				connEtoC1();
			if (e.getSource() == ToDoList.this.getJButton3()) 
				connEtoC2(e);
			if (e.getSource() == ToDoList.this.getJButton4()) 
				connEtoC3();
			if (e.getSource() == ToDoList.this.getJButton2()) 
				connEtoC5();
		};
		public void valueChanged(javax.swing.event.ListSelectionEvent e) {
			if (e.getSource() == ToDoList.this.getJList1()) 
				connEtoC4();
		};
	};
/**
 * connEtoC1:  (JButton1.action. --> ToDoList.jButton1_ActionEvents()V)
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void connEtoC1() {
	try {
		// user code begin {1}
		// user code end
		this.jButton1_ActionEvents();
		// user code begin {2}
		// user code end
	} catch (java.lang.Throwable ivjExc) {
		// user code begin {3}
		// user code end
		handleException(ivjExc);
	}
}
/**
 * connEtoC2:  (JButton3.action.actionPerformed(java.awt.event.ActionEvent) --> ToDoList.readToDoFile()V)
 * @param arg1 java.awt.event.ActionEvent
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void connEtoC2(java.awt.event.ActionEvent arg1) {
	try {
		// user code begin {1}
		// user code end
		this.readToDoFile();
		// user code begin {2}
		// user code end
	} catch (java.lang.Throwable ivjExc) {
		// user code begin {3}
		// user code end
		handleException(ivjExc);
	}
}
/**
 * connEtoC3:  (JButton4.action. --> ToDoList.writeToDoFile()V)
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void connEtoC3() {
	try {
		// user code begin {1}
		// user code end
		this.writeToDoFile();
		// user code begin {2}
		// user code end
	} catch (java.lang.Throwable ivjExc) {
		// user code begin {3}
		// user code end
		handleException(ivjExc);
	}
}
/**
 * connEtoC4:  (JList1.listSelection. --> ToDoList.jList1_ListSelectionEvents()V)
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void connEtoC4() {
	try {
		// user code begin {1}
		// user code end
		this.jList1_ListSelectionEvents();
		// user code begin {2}
		// user code end
	} catch (java.lang.Throwable ivjExc) {
		// user code begin {3}
		// user code end
		handleException(ivjExc);
	}
}
/**
 * connEtoC5:  (JButton2.action. --> ToDoList.jButton2_ActionEvents()V)
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void connEtoC5() {
	try {
		// user code begin {1}
		// user code end
		this.jButton2_ActionEvents();
		// user code begin {2}
		// user code end
	} catch (java.lang.Throwable ivjExc) {
		// user code begin {3}
		// user code end
		handleException(ivjExc);
	}
}
/**
 * connEtoM1:  (JButton1.action.actionPerformed(java.awt.event.ActionEvent) --> DefaultListModel1.addElement(Ljava.lang.Object;)V)
 * @param arg1 java.awt.event.ActionEvent
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void connEtoM1(java.awt.event.ActionEvent arg1) {
	try {
		// user code begin {1}
		// user code end
		getDefaultListModel1().addElement(getJTextField1().getText());
		// user code begin {2}
		// user code end
	} catch (java.lang.Throwable ivjExc) {
		// user code begin {3}
		// user code end
		handleException(ivjExc);
	}
}
/**
 * connEtoM2:  (JButton2.action.actionPerformed(java.awt.event.ActionEvent) --> DefaultListModel1.removeElementAt(I)V)
 * @param arg1 java.awt.event.ActionEvent
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void connEtoM2(java.awt.event.ActionEvent arg1) {
	try {
		// user code begin {1}
		// user code end
		getDefaultListModel1().removeElementAt(getJList1().getSelectedIndex());
		// user code begin {2}
		// user code end
	} catch (java.lang.Throwable ivjExc) {
		// user code begin {3}
		// user code end
		handleException(ivjExc);
	}
}
/**
 * connPtoP1SetTarget:  (DefaultListModel1.this <--> JList1.model)
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void connPtoP1SetTarget() {
	/* Set the target from the source */
	try {
		getJList1().setModel(getDefaultListModel1());
		// user code begin {1}
		// user code end
	} catch (java.lang.Throwable ivjExc) {
		// user code begin {3}
		// user code end
		handleException(ivjExc);
	}
}
/**
 * Cleans up whatever resources are being held. If the applet is active
 * it is stopped.
 * 
 * @see #init
 * @see #start
 * @see #stop
 */
public void destroy() {
	super.destroy();

	// insert code to release resources here
}
/**
 * Returns information about this applet.
 * @return a string of information about this applet
 */
public String getAppletInfo() {
	return "ToDoList application. Modified 2001-05-15 to change sizes.";   // 2001-05-14
}
/**
 * Return the DefaultListModel1 property value.
 * @return javax.swing.DefaultListModel
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.DefaultListModel getDefaultListModel1() {
	if (ivjDefaultListModel1 == null) {
		try {
			ivjDefaultListModel1 = new javax.swing.DefaultListModel();
			// user code begin {1}
			// user code end
		} catch (java.lang.Throwable ivjExc) {
			// user code begin {2}
			// user code end
			handleException(ivjExc);
		}
	}
	return ivjDefaultListModel1;
}
/**
 * Return the JAppletContentPane property value.
 * @return javax.swing.JPanel
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JPanel getJAppletContentPane() {
	if (ivjJAppletContentPane == null) {
		try {
			ivjJAppletContentPane = new javax.swing.JPanel();
			ivjJAppletContentPane.setName("JAppletContentPane");
			ivjJAppletContentPane.setLayout(null);
			getJAppletContentPane().add(getJTextField1(), getJTextField1().getName());
			getJAppletContentPane().add(getJLabel1(), getJLabel1().getName());
			getJAppletContentPane().add(getJScrollPane1(), getJScrollPane1().getName());
			getJAppletContentPane().add(getJButton1(), getJButton1().getName());
			getJAppletContentPane().add(getJButton2(), getJButton2().getName());
			getJAppletContentPane().add(getJButton3(), getJButton3().getName());
			getJAppletContentPane().add(getJButton4(), getJButton4().getName());
			// user code begin {1}
			// user code end
		} catch (java.lang.Throwable ivjExc) {
			// user code begin {2}
			// user code end
			handleException(ivjExc);
		}
	}
	return ivjJAppletContentPane;
}
/**
 * Return the JButton1 property value.
 * @return javax.swing.JButton
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JButton getJButton1() {
	if (ivjJButton1 == null) {
		try {
			ivjJButton1 = new javax.swing.JButton();
			ivjJButton1.setName("JButton1");
			ivjJButton1.setText("ADD");
			ivjJButton1.setBounds(292, 26, 85, 27);
			// user code begin {1}
			// user code end
		} catch (java.lang.Throwable ivjExc) {
			// user code begin {2}
			// user code end
			handleException(ivjExc);
		}
	}
	return ivjJButton1;
}
/**
 * Return the JButton2 property value.
 * @return javax.swing.JButton
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JButton getJButton2() {
	if (ivjJButton2 == null) {
		try {
			ivjJButton2 = new javax.swing.JButton();
			ivjJButton2.setName("JButton2");
			ivjJButton2.setText("REMOVE");
			ivjJButton2.setBounds(292, 79, 85, 27);
			ivjJButton2.setEnabled(false);
			// user code begin {1}
			// user code end
		} catch (java.lang.Throwable ivjExc) {
			// user code begin {2}
			// user code end
			handleException(ivjExc);
		}
	}
	return ivjJButton2;
}
/**
 * Return the JButton3 property value.
 * @return javax.swing.JButton
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JButton getJButton3() {
	if (ivjJButton3 == null) {
		try {
			ivjJButton3 = new javax.swing.JButton();
			ivjJButton3.setName("JButton3");
			ivjJButton3.setText("OPEN");
			ivjJButton3.setBounds(292, 132, 85, 27);
			// user code begin {1}
			// user code end
		} catch (java.lang.Throwable ivjExc) {
			// user code begin {2}
			// user code end
			handleException(ivjExc);
		}
	}
	return ivjJButton3;
}
/**
 * Return the JButton4 property value.
 * @return javax.swing.JButton
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JButton getJButton4() {
	if (ivjJButton4 == null) {
		try {
			ivjJButton4 = new javax.swing.JButton();
			ivjJButton4.setName("JButton4");
			ivjJButton4.setText("SAVE");
			ivjJButton4.setBounds(292, 185, 85, 27);
			// user code begin {1}
			// user code end
		} catch (java.lang.Throwable ivjExc) {
			// user code begin {2}
			// user code end
			handleException(ivjExc);
		}
	}
	return ivjJButton4;
}
/**
 * Return the JLabel1 property value.
 * @return javax.swing.JLabel
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JLabel getJLabel1() {
	if (ivjJLabel1 == null) {
		try {
			ivjJLabel1 = new javax.swing.JLabel();
			ivjJLabel1.setName("JLabel1");
			ivjJLabel1.setText("ToDoList Item");
			ivjJLabel1.setBounds(22, 10, 85, 17);
			// user code begin {1}
			// user code end
		} catch (java.lang.Throwable ivjExc) {
			// user code begin {2}
			// user code end
			handleException(ivjExc);
		}
	}
	return ivjJLabel1;
}
/**
 * Return the JList1 property value.
 * @return javax.swing.JList
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JList getJList1() {
	if (ivjJList1 == null) {
		try {
			ivjJList1 = new javax.swing.JList();
			ivjJList1.setName("JList1");
			ivjJList1.setBounds(0, 0, 160, 120);
			ivjJList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
			// user code begin {1}
			// user code end
		} catch (java.lang.Throwable ivjExc) {
			// user code begin {2}
			// user code end
			handleException(ivjExc);
		}
	}
	return ivjJList1;
}
/**
 * Return the JScrollPane1 property value.
 * @return javax.swing.JScrollPane
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JScrollPane getJScrollPane1() {
	if (ivjJScrollPane1 == null) {
		try {
			ivjJScrollPane1 = new javax.swing.JScrollPane();
			ivjJScrollPane1.setName("JScrollPane1");
			ivjJScrollPane1.setBounds(23, 68, 217, 154);
			getJScrollPane1().setViewportView(getJList1());
			// user code begin {1}
			// user code end
		} catch (java.lang.Throwable ivjExc) {
			// user code begin {2}
			// user code end
			handleException(ivjExc);
		}
	}
	return ivjJScrollPane1;
}
/**
 * Return the JTextField1 property value.
 * @return javax.swing.JTextField
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JTextField getJTextField1() {
	if (ivjJTextField1 == null) {
		try {
			ivjJTextField1 = new javax.swing.JTextField();
			ivjJTextField1.setName("JTextField1");
			ivjJTextField1.setBounds(23, 30, 217, 21);
			// user code begin {1}
			// user code end
		} catch (java.lang.Throwable ivjExc) {
			// user code begin {2}
			// user code end
			handleException(ivjExc);
		}
	}
	return ivjJTextField1;
}
/**
 * Returns parameters defined by this applet.
 * @return an array of descriptions of the receiver's parameters
 */
public java.lang.String[][] getParameterInfo() {
	String[][] info = {
		{"ToDoListName", "String", "Filename.ext"}
	};
	return info;
}
/**
 * Called whenever the part throws an exception.
 * @param exception java.lang.Throwable
 */
private void handleException(java.lang.Throwable exception) {

	/* Uncomment the following lines to print uncaught exceptions to stdout */
	System.out.println("--------- UNCAUGHT EXCEPTION ---------");
	exception.printStackTrace(System.out);
}
/**
 * Initializes the applet.
 */
public void init() {
	try {
		setName("ToDoList");
		//setSize(426, 240);                      -- try this 2001-05-14 --- see next line
		setSize(600,375);
		setContentPane(getJAppletContentPane());
		initConnections();
		//getJAppletContentPane();		// -- try this 2001-05-14  
		//repaint();				// -- try this 2001-05-14  -- no effect	
		//readToDoFile();			// -- try this 2001-05-14  -- no use
		// user code begin {1}
		// user code end
	} catch (java.lang.Throwable ivjExc) {
		// user code begin {2}
		// user code end
		handleException(ivjExc);
	}
}
/**
 * Initializes connections
 * @exception java.lang.Exception The exception description.
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void initConnections() throws java.lang.Exception {
	// user code begin {1}
	// user code end
	getJButton1().addActionListener(ivjEventHandler);
	getJButton2().addActionListener(ivjEventHandler);
	getJButton3().addActionListener(ivjEventHandler);
	getJButton4().addActionListener(ivjEventHandler);
	getJList1().addListSelectionListener(ivjEventHandler);
	connPtoP1SetTarget();
	//repaint();				// -- try this 2001-05-14 -- no effect	
}
/**
 * Comment
 */
public void jButton1_ActionEvents() {
	// testing event-to-code.
	return;
}
/**
 * Comment
 */
public void jButton2_ActionEvents() {
	return;
}
/**
 * Comment
 */

public void jList1_ListSelectionEvents() {
	if (getJList1().getSelectedIndex() < 0 )
		getJButton2().setEnabled(false);
	else
		getJButton2().setEnabled(true);
	return;
}
/**
 * Called when a key has been pressed.
 * @param e the received event
 */
public void keyPressed(KeyEvent e) {
	System.out.println("keyPressed");
}
/**
 * Called when a key has been released.
 * @param e the received event
 */
public void keyReleased(KeyEvent e) {
	System.out.println("keyReleased");
}
/**
 * Called when a key has been typed.
 * @param e the received event
 */
public void keyTyped(KeyEvent e) {
	System.out.println("keyTyped");
}
/**
 * Starts the applet when it is run as an application
 * @param args an array of command-line arguments
 */
public static void main(java.lang.String[] args) {
	ToDoList applet = new ToDoList();
	java.awt.Frame frame = new java.awt.Frame("ToDoList");

	frame.addWindowListener(applet);
	frame.add("Center", applet);
	//frame.setSize(560, 375);
	frame.setSize(600, 375);  			// try this 2001-05-14
	//frame.show();					// try this 2001-05-14 -- moved down after init and start below

	applet.init();
	applet.start();
	//applet.paint();  				// try this 2001-05-14 -- illegal

	frame.show();					// try this -- moving this here 2001-05-14  -- it works now!	
}
/**
 * Called when the mouse has been clicked.
 * @param e the received event
 */
public void mouseClicked(MouseEvent e) {
	System.out.println("mouseClicked");
}
/**
 * Called when the mouse has been dragged.
 * @param e the received event
 */
public void mouseDragged(MouseEvent e) {
	System.out.println("mouseDragged");
}
/**
 * Called when the mouse has entered a window.
 * @param e the received event
 */
public void mouseEntered(MouseEvent e) {
	System.out.println("mouseEntered");
}
/**
 * Called when the mouse has exited a window.
 * @param e the received event
 */
public void mouseExited(MouseEvent e) {
	System.out.println("mouseExited");
}
/**
 * Called when the mouse has been moved.
 * @param e the received event
 */
public void mouseMoved(MouseEvent e) {
	System.out.println("mouseMoved");
}
/**
 * Called when a mouse button has been pressed.
 * @param e the received event
 */
public void mousePressed(MouseEvent e) {
	System.out.println("mousePressed");
}
/**
 * Called when a mouse button has been released.
 * @param e the received event
 */
public void mouseReleased(MouseEvent e) {
	System.out.println("mouseReleased");
}
/**
 * Paints the applet.
 * If the applet does not need to be painted (e.g. if it is only a container for other
 * awt components) then this method can be safely removed.
 * 
 * @param g  the specified Graphics window
 * @see #update
 */
public void paint(Graphics g) {
	super.paint(g);

	// insert code to paint the applet here
}
/**
 * Method readToDoFile creates a fileInStream for FILE_NAME and reads
 * using dataInStream.
 * Creation date: (8/28/2000 12:20:48 AM)
 */
public void readToDoFile()
{
   FileReader fileInStream;
   BufferedReader dataInStream;
   String result; 
   try {
	  // read the file and fill the list
	  fileInStream = new FileReader(FILE_NAME);
	  dataInStream = new BufferedReader(fileInStream);
 
	  // clear the existing entries from the list
	  getDefaultListModel1().removeAllElements();
 
	  // for each line in the file create an item in the list
	  while ((result = dataInStream.readLine()) != null) {
		 if (result.length() != 0)
			getDefaultListModel1().addElement(result);
	  }
	  fileInStream.close();
	  dataInStream.close();
	  repaint();
   } 
	catch (Throwable exc) 
	{
			handleException(exc);
   	}
   return;
}
/**
 * Called to start the applet. You never need to call this method
 * directly, it is called when the applet's document is visited.
 * @see #init
 * @see #stop
 * @see #destroy
 */
public void start() {
	super.start();
	//paint();	// try this to force display on startup 2001-05-14 -- no effect	
	// insert any code to be run when the applet starts here
}
/**
 * Called to stop the applet. It is called when the applet's document is
 * no longer on the screen. It is guaranteed to be called before destroy()
 * is called. You never need to call this method directly.
 * @see #init
 * @see #start
 * @see #destroy
 */
public void stop() {
	super.stop();

	// insert any code to be run when the applet is stopped here
}
/**
 * Invoked when a window is activated.
 * @param e the received event
 */
public void windowActivated(WindowEvent e) {
	// Do nothing.
	// This method is required to comply with the WindowListener interface.
}
/**
 * Invoked when a window has been closed.
 * @param e the received event
 */
public void windowClosed(WindowEvent e) {
	// Do nothing.
	// This method is required to comply with the WindowListener interface.
}
/**
 * Invoked when a window is in the process of being closed.
 * The close operation can be overridden at this point.
 * @param e the received event
 */
public void windowClosing(WindowEvent e) {
	// The window is being closed.  Shut down the system.
	System.exit(0);
}
/**
 * Invoked when a window is deactivated.
 * @param e the received event
 */
public void windowDeactivated(WindowEvent e) {
	// Do nothing.
	// This method is required to comply with the WindowListener interface.
}
/**
 * Invoked when a window is de-iconified.
 * @param e the received event
 */
public void windowDeiconified(WindowEvent e) {
	// Do nothing.
	// This method is required to comply with the WindowListener interface.
}
/**
 * Invoked when a window is iconified.
 * @param e the received event
 */
public void windowIconified(WindowEvent e) {
	// Do nothing.
	// This method is required to comply with the WindowListener interface.
}
/**
 * Invoked when a window has been opened.
 * @param e the received event
 */
public void windowOpened(WindowEvent e) {
	// Do nothing.
	// This method is required to comply with the WindowListener interface.

	//repaint();				// try this 2001-05-14 -- no effect
	connEtoC1();				// try this 2001-05-14 
}
/**
 * Method writeToDoFile creates a fileOutStream for FILE_NAME and uses a 
 * PrintWriter dataOutStream to write the file.
 * Creation date: (8/28/2000 12:31:22 AM)
 */
public void writeToDoFile() 
{ 
FileWriter fileOutStream; 
PrintWriter dataOutStream;


// carriage return and line feed constant 
String crlf = System.getProperties().getProperty("line.separator"); 

// write the file from the list 
try { 
	fileOutStream = new FileWriter(FILE_NAME); 
	dataOutStream = new PrintWriter(fileOutStream); 

// for every item in the list, write a line to the output file 
	for (int i = 0; i < getDefaultListModel1().size(); i++) 
	  dataOutStream.write(getDefaultListModel1().getElementAt(i) + crlf);
	
	fileOutStream.close();
	dataOutStream.close();
}
catch (Throwable exc) 
{
System.err.println("WriteToDoFile threw an exception");
handleException(exc);
}
return;
} 
}

