Avoiding loss of data in the close process

The process of closing one or more documents technically is easy to achieve. However, making sure that changes to a document are not lost when it is closed is a more complex task.

Step 1: Intercept all close actions

Because a close operation can be caused by different actions, it is important to take into account all occasions that would cause a document to close. So the first step to take is to ensure that each and every possible close action is intercepted by a check wheter or not it is ok to close that document or what requirements are bound to closing it.

In the design of SimplyHTML proper handling of close requests is ensured by having all methods to call the same action for closing a document: SHTMLFileCloseAction. Having all related functionality in a central place and having all other related methods to call that central functionality makes it easier to implement exactly the correct functionality and makes sure it is implemented only once.

The close actions to intercept are

- closing the main frame (method processWindowEvent)
- selecting 'Exit' from menu 'File' ( SHTMLFileExitAction)
- selecting 'Close' from menu 'File' ( SHTMLFileCloseAction)
- selecting 'Close all' from menu 'File' ( SHTMLFileCloseAllAction)

Step 2: Ensure documents are closed only when conditions allow it

The second step is to ensure that a document is only closed when conditions allow to close it. Before it closes a document, SHTMLFileCloseAction tests in a central place, if changes are to be saved for that document first or if a save process currently is going on which finalization has to be waited for.

In this functionality the logic is placed to notify the user, that he is about to close a document which contains unsaved changes and to ask the user for a decision whether or not the changes should be saved before closing.

Step 3: Testing the result of the close action

In cases where an action has to follow the close action, such as when the application shall be terminated, the exit action needs to test if a document has been actually closed after it requested to close it. Otherwise, an application would terminate even if the user opted to cancel the operation during the close action (e.g. when asked to save the document first).