
import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class AWT extends Frame implements ActionListener, WindowListener
{

  private BufferedReader inFile;
  private TextArea editArea;
  private TextField nameField;
  private Button loadButton, saveButton, saveAsButton, exitButton;
  private PrintWriter outFile;
  private FileDialog getNameBox;

  public static void main (String [] args)
  {

    AWT demo = new AWT();
    demo.setSize(400,300);
    demo.makeGui();
    demo.setVisible(true);


  }

  public void makeGui()
  {
    setTitle("Java TextEditor");

    Panel Top = new Panel();

    saveButton = new Button("save");
    add("North", saveButton);
    saveButton.addActionListener(this);

    editArea = new TextArea(20,50);
    add ("Center", editArea);
    addWindowListener(this);

    saveAsButton = new Button("save as");
    add("North", saveAsButton);
    saveAsButton.addActionListener(this);

    exitButton = new Button("exit");
    add("South", exitButton);
    exitButton.addActionListener(this);

    loadButton = new Button("load");
    add("North", loadButton);
    loadButton.addActionListener(this);

    add(new Label("Enter file name"));
    nameField = new TextField(20);
    nameField.setText("");
    add(nameField);

    addWindowListener(this);
  }

   public void actionPerformed(ActionEvent evt)
   {
      String fileName;
      fileName = nameField.getText();

      if (evt.getSource()==saveButton)
      {
       try
       {
        nameField.setText(fileName);
        outFile = new PrintWriter( new FileWriter("testout.txt"),true)
        outFile.print(editArea.getText());
        outFile.close();
       }
      
      catch (IOException e)
      {
         System.err.println("File Error: " +e.toString() );
         nameField.setText(" 'Save As' the file first before saving it with 'Save' ! ");
      }

    }
     
     if (evt.getSource()==loadButton)
      {
       getNameBox = new FileDialog(this,"Load",FileDialog.LOAD);
       getNameBox.show();
       fileName = getNameBox.getFile()
       nameField.setText(fileName);

       try
       {

        inFile = new BufferedReader(new FileReader(fileName));
        editArea.setText("");

        String line;
                                                                                                
        while((line = inFile.readLine() ) != null)
        {
           editArea.append(line+"\n");
        }

        inFile.close();
       }

       catch (IOException e)
       {
          System.err.println("Error in file"+fileName+ ": " +e.toString() );
          nameField.setText("Enter the file name to load: ");

       }
     }

     if (evt.getSource()==saveAsButton)
     {
      getNameBox = new FileDialog(this, "get Name", FileDialog.LOAD);
      getNameBox.show();

      fileName = getNameBox.getFile();
      nameField.setText(fileName);
     
      try
      {
         outFile = new PrintWriter(new FileWriter(nameField.getText()),true);
         outFile.print(editArea.getText());
         outFile.close();
      }

      catch(IOException e)
        {
          System.err.println("File Error:" +e.toString());
        }
      }

     if (evt.getSource()==exitButton)
      {
       System.exit(1);
      }
    }
 

   public void windowClosing(WindowEvent e)
    {
       System.exit(0);
    }

    public void windowIconified(WindowEvent e)
    {
    }

    public void windowOpened(WindowEvent e)
    {
    }

    public void windowClosed(WindowEvent e)
    {
    }

    public void windowDeiconified(WindowEvent e)
    {
    }

    public void windowActivated(WindowEvent e)
    {
    }

    public void windowDeactivated(WindowEvent e)
    {
    }
 }

     
     
       

    


