Skip to content

Instantly share code, notes, and snippets.

@samie
Created September 2, 2015 08:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samie/bb1f494428e9aa7c28cc to your computer and use it in GitHub Desktop.
Save samie/bb1f494428e9aa7c28cc to your computer and use it in GitHub Desktop.
Realtime updates from Spring Data REST repository to Vaadin UI
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public WidgetSet getAddonCDNWidgetSet() {
return new WidgetSet();
}
@Theme("valo")
@SpringUI(path = "")
@Push
public static class VaadinUI extends UI {
@Override
protected void init(VaadinRequest request) {
}
@Autowired
public void configureNavigator(SpringViewProvider viewProvider, DataUpdateEventHandler updateHandler) {
Navigator navigator = new Navigator(this,this);
navigator.addProvider(viewProvider);
navigator.addViewChangeListener(updateHandler);
}
}
@Component
@RepositoryEventHandler(DataUpdate.class)
public static class DataUpdateEventHandler implements ViewChangeListener {
private View activeView;
@HandleAfterCreate
public void handleDataUpdateCreate(GPSUpdate u) {
// Send update only to the active activeView if present
if (activeView != null){
// We need to lock the UI during the access. This does it.
((CustomComponent) activeView).getUI().access(() -> {
activeView.enter(null);
});
}
}
@Override
public boolean beforeViewChange(ViewChangeEvent event) {
activeView = null;
return true;
}
@Override
public void afterViewChange(ViewChangeEvent event) {
activeView = event.getNewView();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment