import java.io.*;
import java.awt.*; 

class ClearAllDialog extends Dialog { 
  Frame parent;
  TextArea edit_text;

  public ClearAllDialog(Frame f, TextArea t) {
    super(f, "ClearAll Confirm", true);
    parent = f;
    edit_text = t;

    setLayout(new FlowLayout());
    Panel back_panel = new Panel();
    back_panel.setLayout(new GridLayout(2,1));
    Panel button_panel = new Panel();
    Label mesg = new Label("Clear All OK?", Label.CENTER);
    Button ok = new Button("OK");
    Button cancel = new Button("Cancel");

    button_panel.add(ok);
    button_panel.add(cancel);
    back_panel.add(mesg);
    back_panel.add(button_panel);
    add(back_panel);
  }

  public void start() {
    resize(200,100);
     show() ;
  }

  public boolean action(Event evt, Object obj) {
    if (evt.target instanceof Button) {
      if ("OK".equals(obj)) {
        edit_text.setText("");
      }
       dispose(); 
    }
    return true;
  }
}


public class Practice12B extends Frame {
  TextArea edit_text;

  public Practice12B() {
    super("MyEdit");

    edit_text = new TextArea(40,20);
    edit_text.setFont(new Font("TimesRoman", Font.PLAIN, 16));

    setLayout(new BorderLayout()); 
    add("Center", edit_text); 

    Menu winMenu = new Menu("Window"); 
    winMenu.add(new MenuItem("Open...")); 
    winMenu.add(new MenuItem("Save...")); 
    winMenu.add(new MenuItem("-"));
    winMenu.add(new MenuItem("Quit"));

    Menu editMenu = new Menu("Edit");
    editMenu.add(new MenuItem("ClearAll"));

    MenuBar menu = new MenuBar(); 
    menu.add(winMenu); 
    menu.add(editMenu);
    setMenuBar(menu); 
  }

  public void start() {
    resize(800,400);
     show(); 
  }


  private boolean loadFile(String directory, String filename) {
    FileInputStream file;
    int c;

    edit_text.setText("");

    try {
      file = new FileInputStream(directory + filename);
      try {
         while ((c = file.read()) != -1) {
           edit_text.appendText(String.valueOf((char)c));
	 }
      } catch (EOFException e) {
        System.err.println("IO Error");
        return false;
      } finally {
        file.close();
      }
    } catch (FileNotFoundException e) {
      System.err.println("Not Found File");
      return false;
    } catch (IOException e) {
      System.err.println("Closing Error");
      return false;
    }

    return true;
  }


  private boolean saveFile(String directory, String filename) {
    FileOutputStream file;
    String buffer;
    int i;

    buffer = edit_text.getText();

    try {
      file = new FileOutputStream(directory + filename);
      try {
        for(i = 0; i < buffer.length(); i++)
          file.write(buffer.charAt(i));
      } catch (EOFException e) {
        System.err.println("IO Error");
        return false;
      } finally {
        file.close();
      }
    } catch (FileNotFoundException e) {
      System.err.println("Not Found File");
      return false;
    } catch (IOException e) {
      System.err.println("Closing Error");
      return false;
    }

    return true;
  }


   public boolean handleEvent(Event evt) { 
    if (evt.id == Event.WINDOW_DESTROY) {
      dispose();
      System.exit(1);
      return true;
    } else {
      return super.handleEvent(evt);
    }
  }

  public boolean action(Event evt, Object obj) { 
    if (evt.target instanceof MenuItem) { 
      MenuItem select_item  = (MenuItem)evt.target;
      if ("Quit".equals(select_item.getLabel())) {
        dispose();
        System.exit(0);
        return true;
      } else if ("Open...".equals(select_item.getLabel())) {
        FileDialog open_file_dialog = new FileDialog(this, "Open", FileDialog.LOAD); 
        open_file_dialog.show();
        System.out.println("Open directory: " + open_file_dialog.getDirectory()); 
        System.out.println("Open file     : " + open_file_dialog.getFile()); 
        loadFile(open_file_dialog.getDirectory(), open_file_dialog.getFile());
        return true;
      } else if ("Save...".equals(select_item.getLabel())) {
        FileDialog save_file_dialog = new FileDialog(this, "Save", FileDialog.SAVE);
        save_file_dialog.show();
        System.out.println("Save directory: " + save_file_dialog.getDirectory());
        System.out.println("Save file:      " + save_file_dialog.getFile());
        saveFile(save_file_dialog.getDirectory(), save_file_dialog.getFile());
        return true;
      } else if ("ClearAll".equals(select_item.getLabel())) {
        ClearAllDialog dlg = new ClearAllDialog(this, edit_text);
        dlg.start();
        return true;
      }
    }
    return false;
  }

  public static void main(String args[]) { 
    Practice12B app = new Practice12B();
    app.start();
  }
}