Hi,
UserIndicator
is located in the MainScreen
and can be accessed in it. So the task is to notify MainScreen
that a user has changed. One of the options if to create ApplicationEvent
that is fired from UserEdit
and handled in the MainScreen
, e.g.:
public class UserUpdatedEvent extends ApplicationEvent {
public UserUpdatedEvent(String username) {
super(username);
}
@Override
public String getSource() {
return (String) super.getSource();
}
}
public class UserEdit extends StandardEditor<User> {
....
@Subscribe(target = Target.DATA_CONTEXT)
public void onPostCommit(DataContext.PostCommitEvent event) {
...
getApplicationContext().getBean(UiEventPublisher.class)
.publishEvent(new UserUpdatedEvent(getEditedEntity().getUsername()));
}
}
Unfortunately, just call userIndicator.refreshUser()
is not enough, because UserIndicator
obtains user instance from currentUserSubstitution#getAuthenticatedUser
which returns an old instance, thus you need to set userIndicatorFormatter
:
public class MainScreen extends Screen implements Window.HasWorkArea {
...
@Autowired
private UserIndicator userIndicator;
@Autowired
private CurrentUserSubstitution currentUserSubstitution;
@Autowired
private DatabaseUserRepository databaseUserRepository;
@Autowired
private MetadataTools metadataTools;
...
@Install(to = "userIndicator", subject = "formatter")
private String userIndicatorFormatter(final UserDetails userDetails) {
User user = databaseUserRepository.loadUserByUsername(userDetails.getUsername());
return metadataTools.getInstanceName(user);
}
@EventListener
public void onUserUpdated(UserUpdatedEvent event) {
UserDetails authenticatedUser = currentUserSubstitution.getAuthenticatedUser();
if (Objects.equals(authenticatedUser.getUsername(), event.getSource())) {
userIndicator.refreshUser();
}
}
}
Demo project: UserIndicator-demo.zip (89.3 KB)
Gleb