From 7ed991435c51deff019eb5cd99f989106c8a5169 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Sun, 14 Oct 2018 22:41:47 +0200 Subject: [PATCH] Add multithreading to avoid freezing the UI while loading --- .../src/fr/ensim/xml/ui/MainWindow.java | 51 +++++++++++++++---- .../Java/tp-deezer3-HERBRON_Tanguy/tp-xml.log | 20 ++++---- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/xml/ui/MainWindow.java b/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/xml/ui/MainWindow.java index 6631bce..0ca58ea 100644 --- a/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/xml/ui/MainWindow.java +++ b/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/xml/ui/MainWindow.java @@ -3,9 +3,11 @@ package fr.ensim.xml.ui; import java.io.File; import java.io.IOException; +import com.sun.media.jfxmedia.events.PlayerTimeListener; + import fr.ensim.xml.deezer.Runner; import javafx.application.Application; -import javafx.collections.ObservableList; +import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXMLLoader; @@ -44,6 +46,7 @@ public class MainWindow extends Application { final MenuItem xmlOption = menuButton.getItems().get(0); final MenuItem jsonOption = menuButton.getItems().get(1); final WebView webView = (WebView) scene.lookup("#webView"); + final WebEngine engine = webView.getEngine(); final MenuButton formatButton = (MenuButton) scene.lookup("#xmlFormatSelector"); final MenuItem domOption = formatButton.getItems().get(0); 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 staxOption = htmlButton.getItems().get(1); + final Thread uiThread = Thread.currentThread(); + searchButton.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { if(menuButton.getText().equals(jsonOption.getText())) { - Runner runner = new Runner(); - File htmlFile = runner.start(searchTextField.getText().replaceAll("\\s", "%20"), useCustom); - WebEngine engine = webView.getEngine(); - engine.load(htmlFile.toURI().toString()); + Thread thread = new Thread() + { + public void run() + { + 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 { - Runner runner = new Runner(); - File htmlFile = runner.start(searchTextField.getText().replaceAll("\\s", "%20"), useDom, useCustom); - WebEngine engine = webView.getEngine(); - engine.load(htmlFile.toURI().toString()); + Thread thread = new Thread() + { + public void run() + { + 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(); } } diff --git a/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/tp-xml.log b/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/tp-xml.log index 6ee07a3..278708a 100644 --- a/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/tp-xml.log +++ b/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/tp-xml.log @@ -1,10 +1,10 @@ -22:21:21,043 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:21:26,435 DEBUG fr.ensim.json.JSONSearchAlbum - >>readAlbums -22:21:26,445 DEBUG fr.ensim.json.JSONSearchAlbum - <>albums -22:21:26,447 DEBUG fr.ensim.json.JSONSearchAlbumTracks - http://api.deezer.com/2.0/album/47685332 -22:21:31,570 DEBUG fr.ensim.json.JSONSearchAlbumTracks - >>find -22:21:31,579 DEBUG fr.ensim.json.JSONSearchAlbumTracks - >>find -22:21:31,580 DEBUG fr.ensim.xml.deezer.stax.StAXHtmlAlbum - >>write +22:41:09,806 DEBUG fr.ensim.xml.deezer.AbstractSearchAlbum - >>find author=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:41:15,186 DEBUG fr.ensim.xml.deezer.dom.DOMSearchAlbums - >>readAlbums +22:41:15,223 DEBUG fr.ensim.xml.deezer.dom.DOMSearchAlbums - <>albums +22:41:15,225 DEBUG fr.ensim.xml.deezer.dom.DOMSearchAlbumTracks - http://api.deezer.com/2.0/album/47685332?output=xml +22:41:20,340 DEBUG fr.ensim.xml.deezer.dom.DOMSearchAlbumTracks - >>find +22:41:20,369 DEBUG fr.ensim.xml.deezer.dom.DOMSearchAlbumTracks - >>find +22:41:20,369 DEBUG fr.ensim.xml.deezer.stax.StAXHtmlAlbum - >>write