Add multithreading to avoid freezing the UI while loading

This commit is contained in:
Tanguy Herbron 2018-10-14 22:41:47 +02:00
parent ca7608ecab
commit 7ed991435c
2 changed files with 52 additions and 19 deletions

View File

@ -3,9 +3,11 @@ package fr.ensim.xml.ui;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import com.sun.media.jfxmedia.events.PlayerTimeListener;
import fr.ensim.xml.deezer.Runner; import fr.ensim.xml.deezer.Runner;
import javafx.application.Application; import javafx.application.Application;
import javafx.collections.ObservableList; import javafx.application.Platform;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
@ -44,6 +46,7 @@ public class MainWindow extends Application {
final MenuItem xmlOption = menuButton.getItems().get(0); final MenuItem xmlOption = menuButton.getItems().get(0);
final MenuItem jsonOption = menuButton.getItems().get(1); final MenuItem jsonOption = menuButton.getItems().get(1);
final WebView webView = (WebView) scene.lookup("#webView"); final WebView webView = (WebView) scene.lookup("#webView");
final WebEngine engine = webView.getEngine();
final MenuButton formatButton = (MenuButton) scene.lookup("#xmlFormatSelector"); final MenuButton formatButton = (MenuButton) scene.lookup("#xmlFormatSelector");
final MenuItem domOption = formatButton.getItems().get(0); final MenuItem domOption = formatButton.getItems().get(0);
final MenuItem saxOption = formatButton.getItems().get(1); final MenuItem saxOption = formatButton.getItems().get(1);
@ -51,22 +54,52 @@ public class MainWindow extends Application {
final MenuItem customOption = htmlButton.getItems().get(0); final MenuItem customOption = htmlButton.getItems().get(0);
final MenuItem staxOption = htmlButton.getItems().get(1); final MenuItem staxOption = htmlButton.getItems().get(1);
final Thread uiThread = Thread.currentThread();
searchButton.setOnAction(new EventHandler<ActionEvent>() { searchButton.setOnAction(new EventHandler<ActionEvent>() {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
if(menuButton.getText().equals(jsonOption.getText())) if(menuButton.getText().equals(jsonOption.getText()))
{ {
Runner runner = new Runner(); Thread thread = new Thread()
File htmlFile = runner.start(searchTextField.getText().replaceAll("\\s", "%20"), useCustom); {
WebEngine engine = webView.getEngine(); public void run()
engine.load(htmlFile.toURI().toString()); {
Runner runner = new Runner();
final File htmlFile = runner.start(searchTextField.getText().replaceAll("\\s", "%20"), useCustom);
Platform.runLater(new Runnable(){
@Override
public void run() {
engine.load(htmlFile.toURI().toString());
}
});
}
};
thread.start();
} }
else else
{ {
Runner runner = new Runner(); Thread thread = new Thread()
File htmlFile = runner.start(searchTextField.getText().replaceAll("\\s", "%20"), useDom, useCustom); {
WebEngine engine = webView.getEngine(); public void run()
engine.load(htmlFile.toURI().toString()); {
Runner runner = new Runner();
final File htmlFile = runner.start(searchTextField.getText().replaceAll("\\s", "%20"), useDom, useCustom);
Platform.runLater(new Runnable(){
@Override
public void run() {
engine.load(htmlFile.toURI().toString());
}
});
}
};
thread.start();
} }
} }

View File

@ -1,10 +1,10 @@
22:21:21,043 DEBUG fr.ensim.xml.deezer.AbstractSearchAlbum - >>find author=Shaka%20ponk 22:41:09,806 DEBUG fr.ensim.xml.deezer.AbstractSearchAlbum - >>find author=Shaka%20ponk
22:21:21,046 DEBUG fr.ensim.xml.deezer.AbstractSearchAlbum - http://api.deezer.com/2.0/search/album?q=Shaka%20ponk 22:41:09,808 DEBUG fr.ensim.xml.deezer.AbstractSearchAlbum - http://api.deezer.com/2.0/search/album?q=Shaka%20ponk&output=xml
22:21:26,435 DEBUG fr.ensim.json.JSONSearchAlbum - >>readAlbums 22:41:15,186 DEBUG fr.ensim.xml.deezer.dom.DOMSearchAlbums - >>readAlbums
22:21:26,445 DEBUG fr.ensim.json.JSONSearchAlbum - <<readAlbums 22:41:15,223 DEBUG fr.ensim.xml.deezer.dom.DOMSearchAlbums - <<readAlbums
22:21:26,446 DEBUG fr.ensim.xml.deezer.AbstractSearchAlbum - <<find 22:41:15,223 DEBUG fr.ensim.xml.deezer.AbstractSearchAlbum - <<find
22:21:26,446 DEBUG fr.ensim.json.JSONSearchAlbumTracks - >>albums 22:41:15,225 DEBUG fr.ensim.xml.deezer.dom.DOMSearchAlbumTracks - >>albums
22:21:26,447 DEBUG fr.ensim.json.JSONSearchAlbumTracks - http://api.deezer.com/2.0/album/47685332 22:41:15,225 DEBUG fr.ensim.xml.deezer.dom.DOMSearchAlbumTracks - http://api.deezer.com/2.0/album/47685332?output=xml
22:21:31,570 DEBUG fr.ensim.json.JSONSearchAlbumTracks - >>find 22:41:20,340 DEBUG fr.ensim.xml.deezer.dom.DOMSearchAlbumTracks - >>find
22:21:31,579 DEBUG fr.ensim.json.JSONSearchAlbumTracks - >>find 22:41:20,369 DEBUG fr.ensim.xml.deezer.dom.DOMSearchAlbumTracks - >>find
22:21:31,580 DEBUG fr.ensim.xml.deezer.stax.StAXHtmlAlbum - >>write 22:41:20,369 DEBUG fr.ensim.xml.deezer.stax.StAXHtmlAlbum - >>write