Tutorial Tutorial Step 12

Building a Java text editor

Step 11: Adding code to test if a file has been modified

The program needs to keep track of whether a file has been modified (is "dirty") since being created, opened, or saved, so you can ask the user if it should be saved before closing the file or exiting the program. To do this, let's add a boolean variable called dirty.

  1. Click the following File|New event-handling method in the structure pane: jMenuItem1_actionPerformed(ActionEvent e)

  2. Add the following code to the end of this method to clear the dirty and currFileName variables. Place it immediately after the line jTextArea1.setText(""); and before the closing curly brace.

    // clear the current filename and set the file as clean:
    currFileName = null;
    dirty = false;
    

    You'll use the JOptionPane dialog to display a confirmation message box to find out from the user whether to save a dirty file before abandoning it when doing a File|Open, File|New, or File|Exit. This dialog is invoked by calling a class method on JOptionPane, so you do not need to add a JOptionPane component to your program.

  3. Add the following okToAbandon() method to the source code. You can put this new method right after the saveAsFile() method:
    // Check if file is dirty.
    // If so get user to make a "Save? yes/no/cancel" decision.
    boolean okToAbandon() {
      int value =  JOptionPane.showConfirmDialog(this, "Save changes?",
                                           "Text Edit", JOptionPane.YES_NO_CANCEL_OPTION) ;
    
      switch (value) {
         case JOptionPane.YES_OPTION:
           // yes, please save changes
           return saveFile();
         case JOptionPane.NO_OPTION:
           // no, abandon edits
           // i.e. return true without saving
           return true;
         case JOptionPane.CANCEL_OPTION:
         default:
           // cancel
           return false;
      }
    }
    
    The above method, which you'll finish later, will be called whenever the user chooses File|New, File|Open, or File|Exit. Its purpose is to test to see if the text needs to be saved (is "dirty"). If it is dirty, this method uses a Yes, No, Cancel Message dialog for asking the user whether to save.

    This method also calls saveFile() if the user clicks the Yes button. When the method returns, the <CODE>boo lean</CODE> return value, if true, indicates it is OK to abandon this file because it was clean or the user clicked the Yes or No button. If the return value is false, it means the user clicked Cancel. The code that will actually check to see if the file has changed will be added in a later step.

    For now, this method always treats the file as dirty, even if no change has been made to the text. Later you will add a method to set the dirty variable to true when the user types in the text area, and you will add code to the top of okToAbandon() to test the dirty variable.

  4. Place calls to this okToAbandon() method at the top of your File|New and File|Open event handlers, as well as in the wizard-generated File|Exit event handler. In each case, test the value returned by okToAbandon() and only perform the operation if the value returned is 'true'.

    Tip: To find these event handlers quickly, click them in the structure pane. You can also search in the structure pane by moving focus to the pane and typing.

    The following are the modified event handlers:

    Each of these menu event handling methods now only does its task if okToAbandon() returns true.

  5. Save and run the program and try opening, editing, and saving various files. Remember that okToAbandon() isn't completed yet (it will be finished in a later step). Right now, it always acts like the file is dirty. The result is that for now the confirmation message box always comes up when you choose File|New, File|Open, or File|Exit, even if the text hasn't been changed.

  6. Close the "Text Editor" application.
Tutorial Tutorial Step 12