SHTMLFileCloseAction

All actions implement method actionPerformed which gets called automatically by components bound to that action. Usually all functionality to be performed by an action goes here.

SHTMLFileCloseAction adds some flexibility to that approach by having an additional method closeDocument. This method is called by the action's actionPerformed method on the currently active document so that whenever the action is fired, the currently active document is safely closed.

Method closeDocument

With method closeDocument an arbitrary document shown in the main window can be closed even if it is not the currently active one. The method is declared public so that it can be called and reused from other places simply by instantiating class SHTMLFileCloseAction.

The method's main task is to ensure that a document is only closed, when all of its contents are properly saved. It does this by

1. check, whether or not the particular document needs to be saved (if it was newly created and/or contains unsaved changes, that is)
2. if not, the document is closed
3. if yes, the user is asked if the document shall be saved
4. if the document shall be saved, it is saved by calling the appropriate action class (perform a 'save' or 'save as')
5. if the save was successful or if the user chose not to save changes, the document is closed
6. if the save was not successful or the user wanted to cancel, the document is not closed

In all cases, closing the document is done simply by removing the respective DocumentPane from FrmMain's jtpDocs.

Special case: save thread in progress

As outlined in ' Using threads for lengthy operations', saving a document among other functions is set forth in an own thread. Any close operation has to consider, that a save operation on a particular document could be in progress at the time the user selects to close a document. Method closeDocument takes this into account by calling Method scheduleClose (see below) in cases where it detects a save operation being in progress or where it caused a save operation itself while attempting to close a document.

Method scheduleClose

When a save operation is in progress on a document that is to be closed, SimplyHTML has to wait for the save operation to finish because a document may only be closed when it was saved successful. Whether or not the save operation was successful can only be determined, once it completely finishes, so in this case, the application has to wait with closing until then.

To block the application from other activities, method scheduleClose creates a Timer thread and schedules a TimerTask with the Timer . The TimerTask periodically checks whether or not the save operation of the particular document has finished with the help of field saveInProgress of class DocumentPane. If it has finished, the document is closed and the Timer and TimerTask are disposed. If there was an error during the save operation, the document remains open.

Design advantage

The advantage of this design is that closing a document safely is implemented only once. Still it can be reused either as action or as method from anywhere in the application as done in SHTMLFileCloseAllAction or SHTMLFileExitAction for instance.

Especially see 'Avoiding loss of data in the close process' partly dealing with this topic too.