<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
/*
    This program lets the user edit short text files in a window.  A "File"
    menu provides the following commands:
    
            New  -- Clears all text from the window.
            Open -- Let's the user select a file and loads up to 100
                    lines of text form that file into the window.  The
                    previous contents of the window are lost.
            Save -- Let's the user specify an ouput file and saves
                    the contents of the window in that file.
            Quit -- Closes the window and ends the program.
            
    This class uses the non-standard classes TextReader and MessageDialog.
*/

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

public class TrivialEdit extends Frame implements ActionListener {

   public static void main(String[] args) {
         // The main program just opens a window belonging to this
         // TrivialEdit class.  Then the window takes care of itself
         // until the program is ended with the Quit command.
      new TrivialEdit();
   }
   

   private TextArea text;   // Holds that text that is displayed in the window.
   
   
   public TrivialEdit() {
          // Add a menu bar and a TextArea to the window, and show it
          // on the screen.  The first line of this routine calls the
          // constructor from the superclass to specify a title for the
          // window.  The pack() command sets the size of the window to
          // be just large enough to hold its contents.
      super("A Trivial Editor");
      setMenuBar(makeMenus());
      text = new TextArea(20,60);
      add(text, BorderLayout.CENTER);
      pack();
      show();
   }
   

   private MenuBar makeMenus() {
          // Create and return a menu bar containing a single menu, the
          // File menu.  This menu contains four commands, and each 
          // command has a keyboard equivalent.  The Frame will listen
          // for action events from the menu.
      Menu fileMenu = new Menu("File");
      fileMenu.addActionListener(this);
      fileMenu.add( new MenuItem("New", new MenuShortcut(KeyEvent.VK_N)) );
      fileMenu.add( new MenuItem("Open...", new MenuShortcut(KeyEvent.VK_O)) );
      fileMenu.add( new MenuItem("Save...", new MenuShortcut(KeyEvent.VK_S)) );
      fileMenu.add( new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q)) );
      MenuBar bar = new MenuBar();
      bar.add(fileMenu);
      return bar;
   }


   public void actionPerformed(ActionEvent evt) {
          // This will be called when the user makes a selection from
          // the File menu.  This routine just checks which command was
          // selected and calls another routine to carry out the command.
      String cmd = evt.getActionCommand();
      if (cmd.equals("New"))
         doNew();
      else if (cmd.equals("Open..."))
         doOpen();
      else if (cmd.equals("Save..."))
         doSave();
      else if (cmd.equals("Quit"))
         doQuit();
   }
   

   private void doNew() {
          // Carry out the "New" command from the File menu by
          // by clearing all the text from the TextArea.
      text.setText("");
   }


   private void doSave() {
          // Carry out the Save command by letting the user specify
          // an output file and writing the text from the TextArea
          // to that file.
      FileDialog fd;     // A file dialog that let's the user specify the file.
      String fileName;   // Name of file specified by the user.
      String directory;  // Directory that contains the file.
      fd = new FileDialog(this,"Save Text to File",FileDialog.SAVE);
      fd.show();
      fileName = fd.getFile();
      if (fileName == null) {
            // The fileName is null if the user has canceled the file 
            // dialog.  In this case, there is nothing to do, so quit.
         return;
      }
      directory = fd.getDirectory();
      try {
            // Create a PrintStream for writing to the specified
            // file and write the text from the window to that stream.
         File file = new File(directory, fileName);
         PrintWriter out = new PrintWriter(new FileWriter(file));
         String contents = text.getText();
         out.print(contents);
         out.close();
      }
      catch (IOException e) {
            // Some error has occured while opening or closing the file.
            // Show an error message.
         new MessageDialog(this, "Error:  " + e.toString());
      }
   }


   private void doOpen() {
          // Carry out the Open command by letting the user specify
          // the file to be opened and reading up to 100 lines from 
          // that file.  The text from the file replaces the text
          // in the TextArea.
      FileDialog fd;     // A file dialog that let's the user specify the file.
      String fileName;   // Name of file specified by the user.
      String directory;  // Directory that contains the file.
      fd = new FileDialog(this,"Load File",FileDialog.LOAD);
      fd.show();
      fileName = fd.getFile();
      if (fileName == null) {
            // The fileName is null if the user has canceled the file 
            // dialog.  In this case, there is nothing to do, so quit.
         return;
      }
      directory = fd.getDirectory();
      try {
             // Read lines from the file until end-of-file is detected,
             // or until 100 lines have been read.  The lines are appended
             // the the TextArea, with a line feed after each line.
         File file = new File(directory, fileName);
         TextReader in = new TextReader(new FileReader(file));
         String line;
         text.setText("");
         int lineCt = 0;
         while (lineCt &lt; 100 &amp;&amp; in.peek() != '\0') {
            line = in.getln();
            text.appendText(line + '\n');
            lineCt++;
         }
         if (in.eof() == false)
            text.appendText("\n\n********** Text truncated to 100 lines! ***********\n");
         in.close();
      }
      catch (Exception e) {
            // Some error has occured while opening or closing the file.
            // Show an error message.
         new MessageDialog(this, "Error:  " + e.toString());
      }
   }


   private void doQuit() {
         // Carry out the Quit command by exiting the program.
      System.exit(0);
   }


} // end class TrivialEdit
</pre></body></html>