From 9e040c6d7ac22ee26a1245cfe97f2339410ba4dd Mon Sep 17 00:00:00 2001 From: Rohit Awate Date: Tue, 24 Apr 2018 22:11:18 +0530 Subject: [PATCH] Fixed issue where partial number of settings weren't loaded and improved ThemeManager --- .../controllers/DashboardController.java | 20 +++--- .../controllers/HomeWindowController.java | 4 ++ .../com/rohitawate/everest/main/Main.java | 8 +-- .../everest/util/settings/SettingsLoader.java | 67 +++++++++++++++---- .../everest/util/themes/ThemeManager.java | 37 ++++++---- 5 files changed, 97 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/rohitawate/everest/controllers/DashboardController.java b/src/main/java/com/rohitawate/everest/controllers/DashboardController.java index ece09fb..1bb824e 100644 --- a/src/main/java/com/rohitawate/everest/controllers/DashboardController.java +++ b/src/main/java/com/rohitawate/everest/controllers/DashboardController.java @@ -79,7 +79,7 @@ public class DashboardController implements Initializable { @FXML Tab paramsTab, authTab, headersTab, bodyTab; - private JFXSnackbar snackBar = new JFXSnackbar(dashboard); + private JFXSnackbar snackbar; private final String[] httpMethods = {"GET", "POST", "PUT", "DELETE", "PATCH"}; private List paramsControllers; private List appendedParams; @@ -110,6 +110,8 @@ public class DashboardController implements Initializable { Services.loggingService.logSevere("Could not load headers/body tabs.", e, LocalDateTime.now()); } + snackbar = new JFXSnackbar(dashboard); + responseBox.getChildren().remove(0); promptLayer.setVisible(true); httpMethodBox.getItems().addAll(httpMethods); @@ -150,7 +152,7 @@ public class DashboardController implements Initializable { String address = addressField.getText(); if (address.equals("")) { promptLayer.setVisible(true); - snackBar.show("Please enter an address.", 3000); + snackbar.show("Please enter an address.", 3000); return; } switch (httpMethodBox.getValue()) { @@ -165,7 +167,7 @@ public class DashboardController implements Initializable { if (requestManager == null || requestManager.getClass() != GETRequestManager.class) requestManager = new GETRequestManager(getRequest); else if (requestManager.isRunning()) { - snackBar.show("Please wait while the current request is processed.", 3000); + snackbar.show("Please wait while the current request is processed.", 3000); return; } else { requestManager.setRequest(getRequest); @@ -187,7 +189,7 @@ public class DashboardController implements Initializable { if (requestManager == null || requestManager.getClass() != DataDispatchRequestManager.class) requestManager = new DataDispatchRequestManager(dataDispatchRequest); else if (requestManager.isRunning()) { - snackBar.show("Please wait while the current request is processed.", 3000); + snackbar.show("Please wait while the current request is processed.", 3000); return; } else { requestManager.setRequest(dataDispatchRequest); @@ -204,7 +206,7 @@ public class DashboardController implements Initializable { if (requestManager == null || requestManager.getClass() != DELETERequestManager.class) requestManager = new DELETERequestManager(deleteRequest); else if (requestManager.isRunning()) { - snackBar.show("Please wait while the current request is processed.", 3000); + snackbar.show("Please wait while the current request is processed.", 3000); return; } else { requestManager.setRequest(deleteRequest); @@ -221,7 +223,7 @@ public class DashboardController implements Initializable { Services.historyManager.saveHistory(getState()); } catch (MalformedURLException MURLE) { promptLayer.setVisible(true); - snackBar.show("Invalid address. Please verify and try again.", 3000); + snackbar.show("Invalid address. Please verify and try again.", 3000); } catch (Exception E) { Services.loggingService.logSevere("Request execution failed.", E, LocalDateTime.now()); errorLayer.setVisible(true); @@ -254,7 +256,7 @@ public class DashboardController implements Initializable { } else if (throwable.getClass() == RedirectException.class) { RedirectException redirect = (RedirectException) throwable; addressField.setText(redirect.getNewLocation()); - snackBar.show("Resource moved permanently. Redirecting...", 3000); + snackbar.show("Resource moved permanently. Redirecting...", 3000); requestManager = null; sendRequest(); return; @@ -277,7 +279,7 @@ public class DashboardController implements Initializable { private void onCancelled() { loadingLayer.setVisible(false); promptLayer.setVisible(true); - snackBar.show("Request canceled.", 2000); + snackbar.show("Request canceled.", 2000); requestManager.reset(); } @@ -341,7 +343,7 @@ public class DashboardController implements Initializable { responseArea.setText("No body found in the response."); } } catch (Exception e) { - snackBar.show("Response could not be parsed.", 5000); + snackbar.show("Response could not be parsed.", 5000); Services.loggingService.logSevere("Response could not be parsed.", e, LocalDateTime.now()); errorLayer.setVisible(true); errorTitle.setText("Parsing Error"); diff --git a/src/main/java/com/rohitawate/everest/controllers/HomeWindowController.java b/src/main/java/com/rohitawate/everest/controllers/HomeWindowController.java index 5cca360..1c42539 100644 --- a/src/main/java/com/rohitawate/everest/controllers/HomeWindowController.java +++ b/src/main/java/com/rohitawate/everest/controllers/HomeWindowController.java @@ -19,6 +19,7 @@ package com.rohitawate.everest.controllers; import com.jfoenix.controls.JFXButton; import com.rohitawate.everest.models.DashboardState; import com.rohitawate.everest.util.Services; +import com.rohitawate.everest.util.themes.ThemeManager; import javafx.application.Platform; import javafx.beans.binding.Bindings; import javafx.beans.property.StringProperty; @@ -71,6 +72,7 @@ public class HomeWindowController implements Initializable { private final KeyCombination focusAuth = new KeyCodeCombination(KeyCode.A, KeyCombination.ALT_DOWN); private final KeyCombination focusHeaders = new KeyCodeCombination(KeyCode.H, KeyCombination.ALT_DOWN); private final KeyCombination focusBody = new KeyCodeCombination(KeyCode.B, KeyCombination.ALT_DOWN); + private final KeyCombination refreshTheme = new KeyCodeCombination(KeyCode.T, KeyCombination.SHIFT_DOWN); private HashMap tabControllerMap; private List historyItemControllers; @@ -188,6 +190,8 @@ public class HomeWindowController implements Initializable { if (!httpMethod.equals("GET") && !httpMethod.equals("DELETE")) { controller.requestOptionsTab.getSelectionModel().select(controller.bodyTab); } + } else if (refreshTheme.match(e)) { + ThemeManager.refreshTheme(); } }); } diff --git a/src/main/java/com/rohitawate/everest/main/Main.java b/src/main/java/com/rohitawate/everest/main/Main.java index a5f7eef..eaf7870 100644 --- a/src/main/java/com/rohitawate/everest/main/Main.java +++ b/src/main/java/com/rohitawate/everest/main/Main.java @@ -38,17 +38,17 @@ public class Main extends Application { FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/homewindow/HomeWindow.fxml")); Parent homeWindow = loader.load(); Services.homeWindowController = loader.getController(); - Stage dashboardStage = new Stage(); + Stage dashboardStage = new Stage(); ThemeManager.setTheme(homeWindow); dashboardStage.getIcons().add(new Image(getClass().getResource("/assets/Logo.png").toExternalForm())); dashboardStage.setScene(new Scene(homeWindow)); dashboardStage.setTitle("Everest"); - dashboardStage.show(); + dashboardStage.show(); EverestUtilities.createBugReporter(); - } - + } + public static void main(String args[]) { launch(args); } diff --git a/src/main/java/com/rohitawate/everest/util/settings/SettingsLoader.java b/src/main/java/com/rohitawate/everest/util/settings/SettingsLoader.java index e3cecb5..5414913 100644 --- a/src/main/java/com/rohitawate/everest/util/settings/SettingsLoader.java +++ b/src/main/java/com/rohitawate/everest/util/settings/SettingsLoader.java @@ -22,13 +22,15 @@ import com.rohitawate.everest.util.EverestUtilities; import com.rohitawate.everest.util.Services; import java.io.File; +import java.io.IOException; import java.time.LocalDateTime; /** - * Loads up custom values into Settings from settings.json. + * Loads up custom values into Settings from Everest/config/settings.json. */ public class SettingsLoader implements Runnable { public Thread settingsLoaderThread; + private JsonNode nodes; public SettingsLoader() { settingsLoaderThread = new Thread(this, "Settings loader thread"); @@ -40,27 +42,64 @@ public class SettingsLoader implements Runnable { try { File settingsFile = new File("Everest/config/settings.json"); - System.out.print("Settings file found. Loading settings... "); + System.out.println("Settings file found. Loading settings... "); ObjectMapper mapper = new ObjectMapper(); - JsonNode nodes = mapper.readTree(settingsFile); + nodes = mapper.readTree(settingsFile); - Settings.responseAreaFont = nodes.get("responseAreaFont").toString(); - Settings.responseAreaFontSize = nodes.get("responseAreaFontSize").asInt(); + Settings.responseAreaFont = setStringSetting(Settings.responseAreaFont, "responseAreaFont"); + Settings.responseAreaFontSize = setIntegerSetting(Settings.responseAreaFontSize, "responseAreaFontSize"); - Settings.connectionTimeOutEnable = nodes.get("connectionTimeOutEnable").asBoolean(); + Settings.connectionTimeOutEnable = setBooleanSetting(Settings.connectionTimeOutEnable, "connectionTimeOutEnable"); if (Settings.connectionTimeOutEnable) - Settings.connectionTimeOut = nodes.get("connectionTimeOut").asInt(); + Settings.connectionTimeOut = setIntegerSetting(Settings.connectionTimeOut, "connectionTimeOut"); - Settings.connectionReadTimeOutEnable = nodes.get("connectionReadTimeOutEnable").asBoolean(); + Settings.connectionReadTimeOutEnable = setBooleanSetting(Settings.connectionReadTimeOutEnable, "connectionReadTimeOutEnable"); if (Settings.connectionReadTimeOutEnable) - Settings.connectionReadTimeOut = nodes.get("connectionReadTimeOut").asInt(); + Settings.connectionReadTimeOut = setIntegerSetting(Settings.connectionReadTimeOut, "connectionReadTimeOut"); - Settings.theme = EverestUtilities.trimString(nodes.get("theme").toString()); - } catch (Exception E) { - Services.loggingService.logInfo("Default settings will be used.", LocalDateTime.now()); - } finally { - Services.loggingService.logInfo("Settings loaded.", LocalDateTime.now()); + Settings.theme = EverestUtilities.trimString(setStringSetting(Settings.theme, "theme")); + } catch (IOException IOE) { + Services.loggingService.logInfo("Settings file not found. Using defaults", LocalDateTime.now()); } } + + private String setStringSetting(String defaultValue, String identifier) { + JsonNode value = nodes.get(identifier); + + if (value != null) { + defaultValue = value.toString(); + Services.loggingService.logInfo("[" + identifier + "]: Loaded: " + defaultValue, LocalDateTime.now()); + } else { + Services.loggingService.logInfo("[" + identifier + "]: Not found. Using default value.", LocalDateTime.now()); + } + + return defaultValue; + } + + private int setIntegerSetting(int defaultValue, String identifier) { + JsonNode value = nodes.get(identifier); + + if (value != null) { + defaultValue = value.asInt(); + Services.loggingService.logInfo("[" + identifier + "]: Loaded: " + defaultValue, LocalDateTime.now()); + } else { + Services.loggingService.logInfo("[" + identifier + "]: Not found. Using default value.", LocalDateTime.now()); + } + + return defaultValue; + } + + private boolean setBooleanSetting(boolean defaultValue, String identifier) { + JsonNode value = nodes.get(identifier); + + if (value != null) { + defaultValue = value.asBoolean(); + Services.loggingService.logInfo("[" + identifier + "]: Loaded: " + defaultValue, LocalDateTime.now()); + } else { + Services.loggingService.logInfo("[" + identifier + "]: Not found. Using default value.", LocalDateTime.now()); + } + + return defaultValue; + } } diff --git a/src/main/java/com/rohitawate/everest/util/themes/ThemeManager.java b/src/main/java/com/rohitawate/everest/util/themes/ThemeManager.java index 2781754..7a6a0d2 100644 --- a/src/main/java/com/rohitawate/everest/util/themes/ThemeManager.java +++ b/src/main/java/com/rohitawate/everest/util/themes/ThemeManager.java @@ -28,25 +28,38 @@ import java.util.List; public class ThemeManager { private static List parentNodes = new ArrayList<>(); + /** + * Refreshes the theme of all the registered parents by replacing + * the old external one with the new one. The fallback theme ie "Adreana" + * is always retained. + */ public static void refreshTheme() { - File themeFile = new File("Everest/themes/" + Settings.theme + ".css"); - if (themeFile.exists()) { - String themePath = themeFile.toURI().toString(); + if (!Settings.theme.equals("Adreana")) { + File themeFile = new File("Everest/themes/" + Settings.theme + ".css"); + if (themeFile.exists()) { + String themePath = themeFile.toURI().toString(); - for (Parent parent : parentNodes) { - parent.getStylesheets().clear(); - parent.getStylesheets().add(themePath); + for (Parent parent : parentNodes) { + parent.getStylesheets().remove(1); + parent.getStylesheets().add(1, themePath); + } + + Services.loggingService.logInfo("Theme changed to " + Settings.theme + ".", LocalDateTime.now()); + } else { + Services.loggingService.logInfo(Settings.theme + ": No such theme file found.", LocalDateTime.now()); } - - Services.loggingService.logInfo("Theme changed to " + Settings.theme, LocalDateTime.now()); } } public static void setTheme(Parent parent) { - File themeFile = new File("Everest/themes/" + Settings.theme + ".css"); - if (themeFile.exists()) { - parent.getStylesheets().add(themeFile.toURI().toString()); - parentNodes.add(parent); + if (!Settings.theme.equals("Adreana")) { + File themeFile = new File("Everest/themes/" + Settings.theme + ".css"); + if (themeFile.exists()) { + parent.getStylesheets().add(themeFile.toURI().toString()); + parentNodes.add(parent); + } else { + Services.loggingService.logInfo(Settings.theme + ": No such theme file found.", LocalDateTime.now()); + } } } }