ChangeMonitor.java

 
     1  
     2  //Title:        contour.ChangeMonitor
     3  //Version:
     4  //Copyright:    Copyright (c) 1998
     5  //Author:       Andrew P.  Black
     6  //Company:      Xerox PARC
     7  //Description:  An aspect to monitor whether the contents of the buffer are
     8  //              different from the corresponding file on disk.
     9  
    10  
    11  package contour;
    12  
    13  import java.awt.event.*;
    14  
    15  public aspect ChangeMonitor
    16  {
    17    introduce boolean TextEditFrame.changed;
    18  
    19    advise boolean TextEditFrame.openFile(String fileName), boolean TextEditFrame.saveFile(),
    20           boolean TextEditFrame.menuItem1_actionPerformed(java.awt.event.ActionEvent e)
    21    {
    22      static after
    23      {
    24        if (thisResult) changed = false;    // thisResult => operation was successfull
    25      }
    26    }
    27  
    28    advise void TextEditFrame.textArea1_textValueChanged(java.awt.event.TextEvent e)
    29    {
    30      static before
    31      {
    32        if (changed)
    33          return;
    34        else
    35          changed = true;
    36      }
    37    }
    38  
    39    advise void TextEditFrame.menuItem3_actionPerformed(java.awt.event.ActionEvent e) // File>Save
    40    {
    41      static before
    42      {
    43        if (! changed)
    44        {
    45          thisObject.getToolkit().beep();
    46          return;
    47        }
    48      }
    49    }
    50  
    51    introduce protected boolean TextEditFrame.okToAbandon()
    52    { /**
    53       * Check with the user to see if the current buffer should be abandoned or
    54       * saved to a file, or whether the operation that caused this question to
    55       * be asked should be aborted.
    56       *
    57       * @return true if it is OK to abandon the current buffer contents, either
    58       * because there is already a copy on disk (! changed), or because the
    59       * user said "No", or because she said "Yes" and a save was completed.
    60       *
    61       * @return false if the operation should be aborted.
    62       */
    63       if (! changed) return true;
    64       message1.setButtonSet(Message.YES_NO_CANCEL);
    65       message1.setTitle(thisObject.getTitle());
    66       message1.setMessage("Save changes to " + currentFileString() + "?");
    67       message1.show();
    68       switch (message1.getResult())
    69       {
    70         case Message.YES:
    71              return thisObject.saveFile();
    72         case Message.NO:
    73              return true;
    74         case Message.CANCEL:
    75              return false;
    76         default:
    77              throw new Error("Impossible result from Message.getResult");
    78       }
    79    }
    80  
    81    advise void TextEditFrame.fileOpen(),
    82           void TextEditFrame.fileExit_actionPerformed(ActionEvent e)
    83    {
    84      static before
    85      {
    86        if (! thisObject.okToAbandon())
    87           return;
    88      }
    89    }
    90  
    91    advise boolean TextEditFrame.menuItem1_actionPerformed(ActionEvent e)
    92    {
    93      static before
    94      {
    95        if (! thisObject.okToAbandon())
    96           return false;
    97      }
    98    }
    99  
   100  } // end of ChangeMonitor aspect