From d33c14bab8e83a79314f55727f82d234b980164a Mon Sep 17 00:00:00 2001 From: Jeddunk Date: Tue, 26 Nov 2019 11:19:32 +0100 Subject: [PATCH] save and getdlclist functions as services added state label at bottom ripple fill color are consistent now --- src/main/java/Controller.java | 179 +++++++++++++++++++---------- src/main/java/Main.java | 2 +- src/main/resources/mainWindow.fxml | 15 ++- 3 files changed, 126 insertions(+), 70 deletions(-) diff --git a/src/main/java/Controller.java b/src/main/java/Controller.java index 93ca966..4cb499e 100644 --- a/src/main/java/Controller.java +++ b/src/main/java/Controller.java @@ -1,7 +1,10 @@ import com.jfoenix.controls.*; import javafx.beans.value.ChangeListener; import javafx.collections.FXCollections; +import javafx.concurrent.Service; +import javafx.concurrent.Task; import javafx.fxml.FXML; +import javafx.scene.control.Label; import javafx.scene.control.Tooltip; import javafx.stage.FileChooser; import org.apache.commons.codec.digest.DigestUtils; @@ -35,6 +38,8 @@ public class Controller { private CreamApiConfig config = CreamApiConfig.getInstance(); private SteamAppsListCache cache = new SteamAppsListCache(); @FXML + public Label state_label; + @FXML public JFXTextField path_textfield; @FXML public JFXTextField appId_textfield; @@ -127,20 +132,40 @@ public class Controller { } public void save() { - try { - setUpCreamApi(); - config.setDlcListFromString(dlc_textarea.getText()); - config.setAppId(Integer.parseInt(appId_textfield.getText())); - config.setExtraProtection(extra_protection_checkbox.isSelected()); - config.setForceOffline(offline_checkbox.isSelected()); - config.setUnlockAll(unlock_all_checkbox.isSelected()); - config.setLanguage(language_combobox.getValue()); - config.sync(); - } catch (IOException | ConfigurationException e) { - e.printStackTrace(); - } catch (NullPointerException e) { - System.err.println("No configuration file set!"); - } + Service s = new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected Void call() { + try { + setUpCreamApi(); + config.setDlcListFromString(dlc_textarea.getText()); + config.setAppId(Integer.parseInt(appId_textfield.getText())); + config.setExtraProtection(extra_protection_checkbox.isSelected()); + config.setForceOffline(offline_checkbox.isSelected()); + config.setUnlockAll(unlock_all_checkbox.isSelected()); + config.setLanguage(language_combobox.getValue()); + config.sync(); + } catch (IOException | ConfigurationException e) { + e.printStackTrace(); + } catch (NullPointerException e) { + System.err.println("No configuration file set!"); + } + return null; + } + }; + } + }; + s.setOnRunning(event -> { + setDisableAllButtons(true); + state_label.setText("Saving..."); + }); + s.setOnSucceeded(event -> { + setDisableAllButtons(false); + state_label.setText("Saved successfully!"); + }); + s.start(); } public void getAppId() { @@ -162,56 +187,76 @@ public class Controller { * also lists DLC not available for purchase. */ public void getDlcList() { - Map steamStoreDLCs = new HashMap<>(); - Map steamDbDLCs = new HashMap<>(); - //StringBuilder sb = new StringBuilder(); - try { - // Steam Store - Document steamDoc = Jsoup - .connect("https://store.steampowered.com/app/" + appId_textfield.getText() + "/") - .get(); - Elements steamDLCs = steamDoc.getElementsByClass("game_area_dlc_row"); - for (Element dlc : steamDLCs) { - String dlc_id = dlc.attr("data-ds-appid"); - String dlc_name = dlc - .getElementsByClass("game_area_dlc_name") - .text().replace("\n", "").trim(); - steamStoreDLCs.put(Integer.parseInt(dlc_id), dlc_name); - } - // SteamDB - Document steamDbDoc = Jsoup - .connect("https://steamdb.info/app/" + appId_textfield.getText() + "/dlc/") - .get(); - Element steamDbDlcSection = steamDbDoc.getElementById("dlc"); - Elements steamDbDLCElements = steamDbDlcSection.getElementsByClass("app"); - for (Element dlc : steamDbDLCElements) { - String dlc_id = dlc.attr("data-appid"); - String dlc_name = "Unknown DLC " + dlc_id; - Elements td = dlc.getElementsByTag("td"); - if (!td.isEmpty()) { - dlc_name = td.get(1).text().replace("\n", "").trim(); - } - steamDbDLCs.put(Integer.parseInt(dlc_id), dlc_name); - } - } catch (HttpStatusException e) { - if (e.getStatusCode() == 404) { - System.err.println("App ID empty or not found! (HTTP Status Code: 404)"); - } - } catch (IOException e) { - e.printStackTrace(); - } catch (NullPointerException e) { - // ignore - } + Service s = new Service() { + @Override + protected Task createTask() { + return new Task() { + @Override + protected Void call() { + Map steamStoreDLCs = new HashMap<>(); + Map steamDbDLCs = new HashMap<>(); + //StringBuilder sb = new StringBuilder(); + try { + // Steam Store + Document steamDoc = Jsoup + .connect("https://store.steampowered.com/app/" + appId_textfield.getText() + "/") + .get(); + Elements steamDLCs = steamDoc.getElementsByClass("game_area_dlc_row"); + for (Element dlc : steamDLCs) { + String dlc_id = dlc.attr("data-ds-appid"); + String dlc_name = dlc + .getElementsByClass("game_area_dlc_name") + .text().replace("\n", "").trim(); + steamStoreDLCs.put(Integer.parseInt(dlc_id), dlc_name); + } + // SteamDB + Document steamDbDoc = Jsoup + .connect("https://steamdb.info/app/" + appId_textfield.getText() + "/dlc/") + .get(); + Element steamDbDlcSection = steamDbDoc.getElementById("dlc"); + Elements steamDbDLCElements = steamDbDlcSection.getElementsByClass("app"); + for (Element dlc : steamDbDLCElements) { + String dlc_id = dlc.attr("data-appid"); + String dlc_name = "Unknown DLC " + dlc_id; + Elements td = dlc.getElementsByTag("td"); + if (!td.isEmpty()) { + dlc_name = td.get(1).text().replace("\n", "").trim(); + } + steamDbDLCs.put(Integer.parseInt(dlc_id), dlc_name); + } + } catch (HttpStatusException e) { + if (e.getStatusCode() == 404) { + System.err.println("App ID empty or not found! (HTTP Status Code: 404)"); + } + } catch (IOException e) { + e.printStackTrace(); + } catch (NullPointerException e) { + // ignore + } - Map allDLCs = new HashMap<>(steamStoreDLCs); - steamDbDLCs.forEach(allDLCs::putIfAbsent); - StringBuilder sb = new StringBuilder(); - LinkedHashMap collect = allDLCs.entrySet().stream() - .sorted(Map.Entry.comparingByKey()) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, - (oldValue, newValue) -> oldValue, LinkedHashMap::new)); - collect.forEach((k, v) -> sb.append(k).append("=").append(v).append("\n")); - dlc_textarea.setText(sb.toString()); + Map allDLCs = new HashMap<>(steamStoreDLCs); + steamDbDLCs.forEach(allDLCs::putIfAbsent); + StringBuilder sb = new StringBuilder(); + LinkedHashMap collect = allDLCs.entrySet().stream() + .sorted(Map.Entry.comparingByKey()) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, + (oldValue, newValue) -> oldValue, LinkedHashMap::new)); + collect.forEach((k, v) -> sb.append(k).append("=").append(v).append("\n")); + dlc_textarea.setText(sb.toString()); + return null; + } + }; + } + }; + s.setOnRunning(event -> { + setDisableAllButtons(true); + state_label.setText("Getting list of DLCs..."); + }); + s.setOnSucceeded(event -> { + setDisableAllButtons(false); + state_label.setText("Got list of DLCs successfully!"); + }); + s.start(); } public void openFileChooser() { @@ -262,4 +307,12 @@ public class Controller { } } } + + private void setDisableAllButtons(boolean b) { + reset_button.setDisable(b); + save_button.setDisable(b); + getAppId_button.setDisable(b); + path_button.setDisable(b); + retrieveDlcList_button.setDisable(b); + } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 1d2a0cf..9499ce1 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -11,7 +11,7 @@ public class Main extends Application { Parent root = FXMLLoader.load(ClassLoader.getSystemResource("mainWindow.fxml"));//getClass().getResource("/mainWindow.fxml") primaryStage.setTitle("Auto CreamAPI"); primaryStage.setMinWidth(655 + 25); - primaryStage.setMinHeight(360 + 50); + primaryStage.setMinHeight(400 + 50); primaryStage.setScene(new Scene(root, 1200, 600)); primaryStage.show(); } diff --git a/src/main/resources/mainWindow.fxml b/src/main/resources/mainWindow.fxml index 92cc63f..b0de22b 100644 --- a/src/main/resources/mainWindow.fxml +++ b/src/main/resources/mainWindow.fxml @@ -7,11 +7,12 @@ + - + @@ -26,18 +27,19 @@ + - + - + @@ -48,7 +50,8 @@ - - - + + + +