// EzApp, text-files, FileChooser Dialog
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import java.io.*;

 public class Editor extends EasyIO       // EasyIO contains IBIO and
 public static void main(String[] args// standard constructor stuff
   {  new Editor();
   }
   //--- Create Components --------------------------
   Label     lFileName  = new Label("File Name");
   TextField tFileName  = new TextField(30);
   Button    bOpenFile  = new Button("Open");
   Button    bSaveFile  = new Button("Save");
   Button    bViewFile  = new Button("View");
   TextArea  aEditor    = new TextArea(20,65);

   //--- Constructor --------------------------------
   public Editor()
   {
      setSize(500,400);
      setTitle("TestEzApp");

      add(lFileName);
      add(tFileName);
      add(bOpenFile);
      add(bSaveFile);
      add(bViewFile);
      add(aEditor);

      bOpenFile.addActionListener(this);
      bSaveFile.addActionListener(this);
      bViewFile.addActionListener(this);

      aEditor.setFont(new Font("Monospaced",0,12));  // fixed-pitch font

      setVisible(true);
   }

   //--- Handle Button click events ------------------
   public void actionPerformed(ActionEvent evt)
   {  Object source = evt.getSource();

      if (source == bOpenFile)
      {  String fileName = chooseFile();
         if (!fileName.equals(""))
         {  openFile(fileName);
            tFileName.setText(fileName);
         }
      }
      else if (source == bSaveFile)
      {
         saveFile(tFileName.getText());
      }
      else if (source == bViewFile)
      {
         viewFile(tFileName.getText());               // view file
      }
   }

   //--- Choose File -------------------------------
   // Uses standard JFileChooser to pick a file
   // from the disk drive.  If the user presses [OK],
   // then returns the full path and filename.
   // If user presses [esc] or [cancel], returns ""
   //-----------------------------------------------
   public String chooseFile()
   {
       JFileChooser fileChooser = new JFileChooser();
      int clicked = fileChooser.showOpenDialog(this);
      if (clicked == fileChooser.APPROVE_OPTION)
      {  return fileChooser.getSelectedFile().getPath()}
      else
      {  return ""}
   }

   //--- Open File ---------------------------------
   // Opens the file specified by fileName.
   // If this is not a text file, it the edit box
   // will display lots of strange characters.
   // DON'T save such files - they will be ruined.
   //
   // If the file does not exist, an IOException
   // will occur.  This is trapped by try..catch..
   //-----------------------------------------------
   public void openFile(String fileName)
   {
       try
      {
         aEditor.setText("");               // clear editor box

         BufferedReader textFile =
                new BufferedReader(new FileReader(fileName));
                                            // open file for reading
         while (textFile.ready())           // repeat until End of File
         {
             String info = textFile.readLine();  // read one line of text

             String allText = aEditor.getText() + info + "\n";
                                             // appends new text to editor text
             aEditor.setText(allText);       // then puts it all back in editor
         }
         textFile.close();                  // close the file
      }
      catch (IOException e)
      {  output("Open failed \n" + e.toString());}
   }

   //--- Save File --------------------------------
   // Opens fileName for writing.
   // Gets all the text from the editor as a
   // single String, then writes the entire String
   // into the file with a single .write() command.
   //----------------------------------------------
   public void saveFile(String fileName)
   {
       try
      {                                      // open for writing
         PrintWriter textFile =
             new PrintWriter(new FileWriter(fileName));

         String info = aEditor.getText();    // get entire text from editor

         textFile.write(info);               // write entire text into file

         textFile.close();                   // close file

         output("Save was successful");
      }
      catch (IOException e)
      {  output("Save failed \n" + e.toString());}
   }

   //--- View File --------------------------------------------
   // Uses explorer.exe to open a file - only works in Windows.
   // This will choose the OS default viewer according to
   // the file-name extension and display the file.
   // For a document, this might open it in a word-processor.
   // For an HTML file, it will use a browser.
   //----------------------------------------------------------
   public void viewFile(String fileName)
   {
         tryRuntime.getRuntime().exec("explorer.exe " + fileName)}
        catch(Exception e){ output(e.toString())}
   }
 }