Disabling Save Button when Validation Errors Occur In An Eclipse-RAP Application

StackOverflow https://stackoverflow.com/questions/4839083

  •  27-10-2019
  •  | 
  •  

Pergunta

We are using Eclipse API in our RAP application.This uses Eclipse Modeling Frame Work. When a page gets edited, Model Becomes Dirty and as a Result,Save Button gets Enabled.

In our editor pages, when ever there is an error in the Page, we set the Validation flag of the Editor page to false, which would in turn display red colored marks on the page.Then usually save button is also becoming enabled.

But, I want to change this behavior.When some error mark appears on the page, I don't want to get the save button enabled,.It shouldn't allow the user to save the model in the error stage. The save button should be disabled, How can I achieve this. Kindly clear my doubt.

Foi útil?

Solução

The editor generated by EMF uses a commandstacklistener to fire a PROP_DIRTY to the editor. If this property is fired, the underlying framework will ask the editors #isDirty Method for the dirty state. This is the place where you can implement your logic.

@Override
public boolean isDirty() {
    Diagnostic diagnostic = validateMyModel();
    return ((BasicCommandStack)editingDomain.getCommandStack()).isSaveNeeded() && diagnostic.getSeverity() == Diagnostic.OK;
}

This case doesn't cover the usecase, that the editor could have been already dirty when the user makes a not-valid edit on the model.

But that is not the best way IMHO. Because if the user closes the editor all changes of the model are lost, without any notification (because of the missing dirty-flag). So he probably did 100 valid modifications, 1 invalid and loses his changed model.

A better way is to show a warning-message if the user wants to save the dialog. If there are errors in the dialog the editor cannot change its state from dirty to not-dirty and the user has to

  • correct all erros or
  • close the editor and looses all his changens

To achieve that you have to implement in your doSave(IProgressMonitor progressMonitor) method a dialog to show the errors. The more tricky part is to override the default-behavior of closing a dirty editor. The workbench will show a dialog with, Yes, No and Cancel To override this behavior you have to implement the interface org.eclipse.ui.ISaveablePart2 in your editor to override the promptToSaveOnClose() method. In this method there must be again your logic that checks for errors in the model. If there are errors, this method has to return ISaveablePart2.CANCEL so that editor is not closable as long as there are errors in the dirty model.

HTH Tom

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top