/**

 *

 * Build and use of I/O frame

 * @author Ahmad Mobasseri

 */

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.io.*;

import java.util.*;

 

class IOFrame extends JFrame implements ActionListener

{

      private static final int FRAME_WIDTH  = 600;

      private static final int FRAME_HEIGHT = 400;

      private static final int TXT_FLD_COLOMNS = 35;

      private static final String NEWLINE = System.getProperty("line.separator");

     

      private JPanel topPanel;

      private JLabel textLabel;

      private JTextField textField;

      private JButton saveButton;

      private JTextArea textArea;

      private JPanel bottomPanel;

      private JButton displayButton;

      private JButton clearButton;

      private File currentFile;

      private boolean append = true;

      public static final long serialVersionUID = 1;

 

/**

 * Constructor

 */

   public IOFrame()    

   {

        // JFrame

        setTitle("Do some I/O");

        setSize(FRAME_WIDTH, FRAME_HEIGHT);

 

        // Top Panel

      topPanel = new JPanel(new FlowLayout());

      textLabel = new JLabel("Enter Text");

//      Font textLabelFont = new Font("serif", Font.BOLD, 20);

//      textLabel.setFont(textLabelFont);

      topPanel.add(textLabel);

      textField = new JTextField(TXT_FLD_COLOMNS);

      topPanel.add(textField);

      saveButton = new JButton("Save");

      topPanel.add(saveButton);                

      add(topPanel, BorderLayout.NORTH);        // add Top Panel to frame layout     

       

        // Mid Panel

      textArea = new JTextArea();

//    textArea.setColumns(22);            // NO effect because the scroll pane is added directly

//    textArea.setRows(8);                //   to the center of a frame with BorderLayout.

      textArea.setEditable(false);

      textArea.setLineWrap(true);   // NOTE: this prevents Horizontal_Scroll

      JScrollPane scrollText = new JScrollPane(textArea);

//      scrollText.setSize(200,135); // No effect. see above.

      add(scrollText, BorderLayout.CENTER);           // add Mid Panel to frame layout

     

      // Bottom Panel

      bottomPanel = new JPanel(new FlowLayout());     

      displayButton = new JButton("Display");

      bottomPanel.add(displayButton);

      clearButton = new JButton("Clear");

      bottomPanel.add(clearButton);

      add(bottomPanel, BorderLayout.SOUTH);

     

      // Events

      saveButton.addActionListener(this);                   // on clicking Save button  

      displayButton.addActionListener(this);                // on clicking Display button

      clearButton.addActionListener(this);                        // on clicking Clear button

   }

  

/**

 * Event Action Listener for Save & Display & Clear buttons 

 */

   public void actionPerformed(ActionEvent event)

   { 

         JButton clickedButton = (JButton) event.getSource();

         if (clickedButton == saveButton)                         // Save button

         {

//             if (currentFile.exists())              // NOTE: can't use this, as currentFile = Null pntr

               if (currentFile == null)                    

               {

//                   JFileChooser chooser = new JFileChooser("C:/Users/Ahmad/Documents");

                     String currUserDir = System.getProperty("user.dir");           // Display

                     JFileChooser chooser = new JFileChooser(currUserDir);    //  file chooser

                     int status = chooser.showSaveDialog(null);                     //   with 'save' option

                     if (status == JFileChooser.APPROVE_OPTION)                     // if 'save' is clicked

                     {

                           currentFile = chooser.getSelectedFile();                       // get selected file

                           // NOTE: chooser.getSelectedFile() has reference to created object of File.

                           // And as we set it to currentFile, then it references to object of File.

                           //  and no need to create the object of File by 'new'

                     }

                     else                                                                       // must be 'Cancel' button

                     {

                           // No action.  NOTE: currentFile remains null                                 

                     }

               }

               if (currentFile != null)

               {          

                     try

                     {

                           FileOutputStream outFileStream = new FileOutputStream(currentFile, append);

                           PrintWriter outStream = new PrintWriter(outFileStream);

                           outStream.println(textField.getText());  //.append(textField.getText());

                           outStream.close();

                           textField.setText("");

                           textField.requestFocus();

                           // NOTE: currentFile.length() with 'println' is 2 more due to /r/n

                     }

                     catch (FileNotFoundException e)

                     {

                           System.out.println("File Not Found Exception on Save: " + e.getMessage());

                     }

               }

         }

         else if (clickedButton == displayButton)                 // Display button

         {

               if (currentFile == null)                    

               {

//                   JFileChooser chooser = new JFileChooser("C:/Users/Ahmad/Documents");

                     String currUserDir = System.getProperty("user.dir");           // Display

                     JFileChooser chooser = new JFileChooser(currUserDir);    //  file chooser

                     int status = chooser.showOpenDialog(null);                     //   with 'open' option

                     if (status == JFileChooser.APPROVE_OPTION)                     // if 'open' is clicked

                     {

                           currentFile = chooser.getSelectedFile();                       // get selected file

                     }

                     else                                                                       // must be 'Cancel' button

                     {

                           // No action.  NOTE: currentFile remains null                           

                     }

               }

               if (currentFile != null)

               {

                     try

                     {

                           Scanner scanner = new Scanner(currentFile).useDelimiter("\\s*\n\\s*");

                           while (scanner.hasNext())

                           {

                                 String newLine = scanner.next();

                                 textArea.append(newLine + NEWLINE);

                           }

                           scanner.close(); 

                     }

                     catch (FileNotFoundException e)

                     {

                           System.out.println("File Not Found Exception on Display: " + e.getMessage());

                     }

               }

         }

         else                                                                       // Clear button

               textArea.setText("");

   }    

}