Tutorial Tutorial Step 12

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

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

  1. Click on the following File|New event-handling method in the Structure pane:

  2. Add the following code to the end of this method to clear the dirty and currFileName variables. Place it immediately after the line textArea1.setText("");.
     // clear the current filename and set the file as clean:
     currFileName = null;
     dirty = false;
    
  3. Switch to the Design tab and add a Message dialog to your design from the JBCL Containers page of the Component Palette. You'll use this dialog 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.

  4. Select message1 in the Component Tree, then set its frame property to this in the Inspector.

  5. Click the Source tab to go back to the editor. Add the following okToAbandon() method (explained below) 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.
     * @return true if user saved here (Yes), or didn't care (No)
     * @return false if user hits cancel.
     */
    boolean okToAbandon() {
      message1.setButtonSet(Message.YES_NO_CANCEL);
      message1.setTitle("Text Edit");
      message1.setMessage("Save changes?");
      message1.show();
      switch (message1.getResult()) {
         case Message.YES:
           // Yes, please save changes.
           return saveFile();
         case Message.NO:
           // No, abandon edits.
           // i.e. return true without saving
           return true;
         case Message.CANCEL:
         default:
           // Cancel.
           return false;
      }
    }
    
    The above method, which we will be finishing in a later step, 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 boolean 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, and if false, means the user clicked cancel. The code that will actually check to see if the file is dirty 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.

  6. 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 on them in the Structure pane.

    The following are the modified event handlers:

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

  7. 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) and so it now always acts like the file is dirty. The result is that for now the Message box always comes up when you choose File|New, File|Open, or File|Exit, even if the text hasn't been changed.

    Warning: Be careful, this is now a functioning text editor. Don't damage your source files or other important files while testing it.

Tutorial Tutorial Step 12