Friday 14 September 2007

Prevent that a RCP Editor is closed

Using editors in a RCP application is quite useful, but have you ever tried to prevent the closing of an editor? I use an editor with several text fields and only if all fields are filled with valid content the editor can be closed. During a normal save event the content can be validated and if no save is possible some error messages are shown. The only problem is the editor close event; there is no direct way to interrupt the editor close (and save) if the content is not ready to be saved.

Fortunately eclipse can provides an easy solution. Your editor just has to implement the ISaveablePart2 interface. This interface requires the implementation of a single method promptToSaveOnClose. This method is only called when the part is closed or when the RCP application is shutting down. The implementation of this method now just needs to show a custom dialog and the user can decide what to do with unsaved data. Easy, isn’t it? Let’s have look at a short example.

Assume that you have an editor there the isDirty method returns true (so eclipse would show the save-message).

In the diagram you can see the short application flow. First a message is shown and the user can decide what to do. If the user selects NO, NO is also the result of the promptToSaveOnClose. If NO is returned nothing is saved and the editor is closed. On CANCEL nothing should happen and just CANCEL is returned. On YES the content of the editor is validated. If the validation fails CANCEL is returned, otherwise the result is YES.


This method can also return DEFAULT. In that case the standard eclipse dialog is shown.That’s all if the user wants to close the editor and save the content this is only allowed if editor content as valid.

1 comment:

Dimitri said...

A very nice blog entry. Thanks.