Created
September 2, 2015 08:10
-
-
Save samie/bb1f494428e9aa7c28cc to your computer and use it in GitHub Desktop.
Realtime updates from Spring Data REST repository to Vaadin UI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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