Fixed issue where partial number of settings weren't loaded and improved ThemeManager

This commit is contained in:
Rohit Awate 2018-04-24 22:11:18 +05:30
parent defd8924ae
commit 9e040c6d7a
5 changed files with 97 additions and 39 deletions

View file

@ -79,7 +79,7 @@ public class DashboardController implements Initializable {
@FXML @FXML
Tab paramsTab, authTab, headersTab, bodyTab; Tab paramsTab, authTab, headersTab, bodyTab;
private JFXSnackbar snackBar = new JFXSnackbar(dashboard); private JFXSnackbar snackbar;
private final String[] httpMethods = {"GET", "POST", "PUT", "DELETE", "PATCH"}; private final String[] httpMethods = {"GET", "POST", "PUT", "DELETE", "PATCH"};
private List<StringKeyValueFieldController> paramsControllers; private List<StringKeyValueFieldController> paramsControllers;
private List<String> appendedParams; private List<String> appendedParams;
@ -110,6 +110,8 @@ public class DashboardController implements Initializable {
Services.loggingService.logSevere("Could not load headers/body tabs.", e, LocalDateTime.now()); Services.loggingService.logSevere("Could not load headers/body tabs.", e, LocalDateTime.now());
} }
snackbar = new JFXSnackbar(dashboard);
responseBox.getChildren().remove(0); responseBox.getChildren().remove(0);
promptLayer.setVisible(true); promptLayer.setVisible(true);
httpMethodBox.getItems().addAll(httpMethods); httpMethodBox.getItems().addAll(httpMethods);
@ -150,7 +152,7 @@ public class DashboardController implements Initializable {
String address = addressField.getText(); String address = addressField.getText();
if (address.equals("")) { if (address.equals("")) {
promptLayer.setVisible(true); promptLayer.setVisible(true);
snackBar.show("Please enter an address.", 3000); snackbar.show("Please enter an address.", 3000);
return; return;
} }
switch (httpMethodBox.getValue()) { switch (httpMethodBox.getValue()) {
@ -165,7 +167,7 @@ public class DashboardController implements Initializable {
if (requestManager == null || requestManager.getClass() != GETRequestManager.class) if (requestManager == null || requestManager.getClass() != GETRequestManager.class)
requestManager = new GETRequestManager(getRequest); requestManager = new GETRequestManager(getRequest);
else if (requestManager.isRunning()) { 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; return;
} else { } else {
requestManager.setRequest(getRequest); requestManager.setRequest(getRequest);
@ -187,7 +189,7 @@ public class DashboardController implements Initializable {
if (requestManager == null || requestManager.getClass() != DataDispatchRequestManager.class) if (requestManager == null || requestManager.getClass() != DataDispatchRequestManager.class)
requestManager = new DataDispatchRequestManager(dataDispatchRequest); requestManager = new DataDispatchRequestManager(dataDispatchRequest);
else if (requestManager.isRunning()) { 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; return;
} else { } else {
requestManager.setRequest(dataDispatchRequest); requestManager.setRequest(dataDispatchRequest);
@ -204,7 +206,7 @@ public class DashboardController implements Initializable {
if (requestManager == null || requestManager.getClass() != DELETERequestManager.class) if (requestManager == null || requestManager.getClass() != DELETERequestManager.class)
requestManager = new DELETERequestManager(deleteRequest); requestManager = new DELETERequestManager(deleteRequest);
else if (requestManager.isRunning()) { 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; return;
} else { } else {
requestManager.setRequest(deleteRequest); requestManager.setRequest(deleteRequest);
@ -221,7 +223,7 @@ public class DashboardController implements Initializable {
Services.historyManager.saveHistory(getState()); Services.historyManager.saveHistory(getState());
} catch (MalformedURLException MURLE) { } catch (MalformedURLException MURLE) {
promptLayer.setVisible(true); 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) { } catch (Exception E) {
Services.loggingService.logSevere("Request execution failed.", E, LocalDateTime.now()); Services.loggingService.logSevere("Request execution failed.", E, LocalDateTime.now());
errorLayer.setVisible(true); errorLayer.setVisible(true);
@ -254,7 +256,7 @@ public class DashboardController implements Initializable {
} else if (throwable.getClass() == RedirectException.class) { } else if (throwable.getClass() == RedirectException.class) {
RedirectException redirect = (RedirectException) throwable; RedirectException redirect = (RedirectException) throwable;
addressField.setText(redirect.getNewLocation()); addressField.setText(redirect.getNewLocation());
snackBar.show("Resource moved permanently. Redirecting...", 3000); snackbar.show("Resource moved permanently. Redirecting...", 3000);
requestManager = null; requestManager = null;
sendRequest(); sendRequest();
return; return;
@ -277,7 +279,7 @@ public class DashboardController implements Initializable {
private void onCancelled() { private void onCancelled() {
loadingLayer.setVisible(false); loadingLayer.setVisible(false);
promptLayer.setVisible(true); promptLayer.setVisible(true);
snackBar.show("Request canceled.", 2000); snackbar.show("Request canceled.", 2000);
requestManager.reset(); requestManager.reset();
} }
@ -341,7 +343,7 @@ public class DashboardController implements Initializable {
responseArea.setText("No body found in the response."); responseArea.setText("No body found in the response.");
} }
} catch (Exception e) { } 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()); Services.loggingService.logSevere("Response could not be parsed.", e, LocalDateTime.now());
errorLayer.setVisible(true); errorLayer.setVisible(true);
errorTitle.setText("Parsing Error"); errorTitle.setText("Parsing Error");

View file

@ -19,6 +19,7 @@ package com.rohitawate.everest.controllers;
import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXButton;
import com.rohitawate.everest.models.DashboardState; import com.rohitawate.everest.models.DashboardState;
import com.rohitawate.everest.util.Services; import com.rohitawate.everest.util.Services;
import com.rohitawate.everest.util.themes.ThemeManager;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.binding.Bindings; import javafx.beans.binding.Bindings;
import javafx.beans.property.StringProperty; 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 focusAuth = new KeyCodeCombination(KeyCode.A, KeyCombination.ALT_DOWN);
private final KeyCombination focusHeaders = new KeyCodeCombination(KeyCode.H, 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 focusBody = new KeyCodeCombination(KeyCode.B, KeyCombination.ALT_DOWN);
private final KeyCombination refreshTheme = new KeyCodeCombination(KeyCode.T, KeyCombination.SHIFT_DOWN);
private HashMap<Tab, DashboardController> tabControllerMap; private HashMap<Tab, DashboardController> tabControllerMap;
private List<HistoryItemController> historyItemControllers; private List<HistoryItemController> historyItemControllers;
@ -188,6 +190,8 @@ public class HomeWindowController implements Initializable {
if (!httpMethod.equals("GET") && !httpMethod.equals("DELETE")) { if (!httpMethod.equals("GET") && !httpMethod.equals("DELETE")) {
controller.requestOptionsTab.getSelectionModel().select(controller.bodyTab); controller.requestOptionsTab.getSelectionModel().select(controller.bodyTab);
} }
} else if (refreshTheme.match(e)) {
ThemeManager.refreshTheme();
} }
}); });
} }

View file

@ -38,17 +38,17 @@ public class Main extends Application {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/homewindow/HomeWindow.fxml")); FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/homewindow/HomeWindow.fxml"));
Parent homeWindow = loader.load(); Parent homeWindow = loader.load();
Services.homeWindowController = loader.getController(); Services.homeWindowController = loader.getController();
Stage dashboardStage = new Stage(); Stage dashboardStage = new Stage();
ThemeManager.setTheme(homeWindow); ThemeManager.setTheme(homeWindow);
dashboardStage.getIcons().add(new Image(getClass().getResource("/assets/Logo.png").toExternalForm())); dashboardStage.getIcons().add(new Image(getClass().getResource("/assets/Logo.png").toExternalForm()));
dashboardStage.setScene(new Scene(homeWindow)); dashboardStage.setScene(new Scene(homeWindow));
dashboardStage.setTitle("Everest"); dashboardStage.setTitle("Everest");
dashboardStage.show(); dashboardStage.show();
EverestUtilities.createBugReporter(); EverestUtilities.createBugReporter();
} }
public static void main(String args[]) { public static void main(String args[]) {
launch(args); launch(args);
} }

View file

@ -22,13 +22,15 @@ import com.rohitawate.everest.util.EverestUtilities;
import com.rohitawate.everest.util.Services; import com.rohitawate.everest.util.Services;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime; 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 class SettingsLoader implements Runnable {
public Thread settingsLoaderThread; public Thread settingsLoaderThread;
private JsonNode nodes;
public SettingsLoader() { public SettingsLoader() {
settingsLoaderThread = new Thread(this, "Settings loader thread"); settingsLoaderThread = new Thread(this, "Settings loader thread");
@ -40,27 +42,64 @@ public class SettingsLoader implements Runnable {
try { try {
File settingsFile = new File("Everest/config/settings.json"); 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(); ObjectMapper mapper = new ObjectMapper();
JsonNode nodes = mapper.readTree(settingsFile); nodes = mapper.readTree(settingsFile);
Settings.responseAreaFont = nodes.get("responseAreaFont").toString(); Settings.responseAreaFont = setStringSetting(Settings.responseAreaFont, "responseAreaFont");
Settings.responseAreaFontSize = nodes.get("responseAreaFontSize").asInt(); Settings.responseAreaFontSize = setIntegerSetting(Settings.responseAreaFontSize, "responseAreaFontSize");
Settings.connectionTimeOutEnable = nodes.get("connectionTimeOutEnable").asBoolean(); Settings.connectionTimeOutEnable = setBooleanSetting(Settings.connectionTimeOutEnable, "connectionTimeOutEnable");
if (Settings.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) if (Settings.connectionReadTimeOutEnable)
Settings.connectionReadTimeOut = nodes.get("connectionReadTimeOut").asInt(); Settings.connectionReadTimeOut = setIntegerSetting(Settings.connectionReadTimeOut, "connectionReadTimeOut");
Settings.theme = EverestUtilities.trimString(nodes.get("theme").toString()); Settings.theme = EverestUtilities.trimString(setStringSetting(Settings.theme, "theme"));
} catch (Exception E) { } catch (IOException IOE) {
Services.loggingService.logInfo("Default settings will be used.", LocalDateTime.now()); Services.loggingService.logInfo("Settings file not found. Using defaults", LocalDateTime.now());
} finally {
Services.loggingService.logInfo("Settings loaded.", 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;
}
} }

View file

@ -28,25 +28,38 @@ import java.util.List;
public class ThemeManager { public class ThemeManager {
private static List<Parent> parentNodes = new ArrayList<>(); private static List<Parent> 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() { public static void refreshTheme() {
File themeFile = new File("Everest/themes/" + Settings.theme + ".css"); if (!Settings.theme.equals("Adreana")) {
if (themeFile.exists()) { File themeFile = new File("Everest/themes/" + Settings.theme + ".css");
String themePath = themeFile.toURI().toString(); if (themeFile.exists()) {
String themePath = themeFile.toURI().toString();
for (Parent parent : parentNodes) { for (Parent parent : parentNodes) {
parent.getStylesheets().clear(); parent.getStylesheets().remove(1);
parent.getStylesheets().add(themePath); 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) { public static void setTheme(Parent parent) {
File themeFile = new File("Everest/themes/" + Settings.theme + ".css"); if (!Settings.theme.equals("Adreana")) {
if (themeFile.exists()) { File themeFile = new File("Everest/themes/" + Settings.theme + ".css");
parent.getStylesheets().add(themeFile.toURI().toString()); if (themeFile.exists()) {
parentNodes.add(parent); parent.getStylesheets().add(themeFile.toURI().toString());
parentNodes.add(parent);
} else {
Services.loggingService.logInfo(Settings.theme + ": No such theme file found.", LocalDateTime.now());
}
} }
} }
} }