My saveCommand is hold by a HandledMenuItem and a HandledToolItem.
The command must be disabled when no change occurs in the data model.
I'd like to disable the ToolItem dynamically. So the SaveHandler is an observer of my model. The data model notifies its dependants when the dirty property of the datamodel changes.
Here is the code:
public class SaveHandler implements Observer{
@Inject
MApplication application;
@Inject
private EModelService modelService;
@CanExecute
public boolean canExecute() {
return ((MyProject.getFilename() != null) && MyProject.isModelDirty());
}
@Execute
public Boolean execute() {
if (canExecute() == true) // test en principe inutile
{
// ici le processus de sauvegarde du fichier
MyProject.setDirty(false);
}
return Boolean.TRUE;
}
@PostConstruct
public void register()
{
ModelProvider.INSTANCE.addObserver(this);
}
@Override
public void update(Observable o, Object arg) {
// here i would like to change the enable state of my command
MUIElement item = modelService.find("MyProject.handleditem.trimbar.top.save", application);
ToolItem toolItem= (ToolItem)(item.getWidget());
toolItem.setEnabled(((ModelProvider)o).isModified());;
}
@PreDestroy
public void unregister(MApplication application, EModelService service)
{
ModelProvider.INSTANCE.deleteObserver(this);
}
}
I am not satisfied with this solution. In fact as it is the the command that becomes enabled/disabled it would be logic to ask the command to notify the gui elements to refresh. I'am not satisfied to have to search the swt component in order to refresh the tool item.
I'm new with Eclipse E4. Any suggestion to a proper solution? Thanks a lot.