use of javafx.concurrent.Worker in project intellij-plugins by StepicOrg.
the class AuthDialog method makeProgressBarWithListener.
@NotNull
private ProgressBar makeProgressBarWithListener() {
final ProgressBar progress = new ProgressBar();
Worker<Void> loadWorker = engine.getLoadWorker();
progress.progressProperty().bind(loadWorker.progressProperty());
loadWorker.stateProperty().addListener(new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue<? extends Worker.State> ov, Worker.State oldState, Worker.State newState) {
if (newState == Worker.State.CANCELLED) {
return;
}
if (newState == Worker.State.FAILED) {
Map<String, Object> map = new HashMap<>();
map.put("url", engine.getLocation());
String content = Templater.processTemplate("error", map);
engine.loadContent(content);
return;
}
String location = engine.getLocation();
if (location != null) {
if (location.startsWith(Urls.STEPIK_URL + "/#")) {
String paramString = location.split("#")[1];
String[] params = paramString.split("&");
map.clear();
Arrays.stream(params).forEach(param -> {
String[] entry = param.split("=");
String value = "";
if (entry.length > 1) {
value = entry[1];
}
map.put(entry[0], value);
});
hide();
return;
} else if ((Urls.STEPIK_URL + "/?error=access_denied").equals(location)) {
map.put("error", "access_denied");
hide();
return;
}
}
progressBar.setVisible(newState == Worker.State.RUNNING);
if (newState == Worker.State.SUCCEEDED) {
AuthDialog.this.setTitle(engine.getTitle());
}
}
private void hide() {
loadWorker.cancel();
setVisible(false);
}
});
return progress;
}
Aggregations