Added custom settings loader

This commit is contained in:
Rohit Awate 2018-01-28 16:28:17 +05:30
parent f94b1f3d18
commit 03c6b7c048
6 changed files with 99 additions and 1 deletions

4
settings/settings.json Normal file
View file

@ -0,0 +1,4 @@
{
"responseAreaFont": "Liberation Mono",
"responseAreaFontSize": "18"
}

View file

@ -21,6 +21,7 @@ import com.rohitawate.restaurant.models.requests.GETRequest;
import com.rohitawate.restaurant.models.responses.RestaurantResponse; import com.rohitawate.restaurant.models.responses.RestaurantResponse;
import com.rohitawate.restaurant.requestsmanager.GETRequestManager; import com.rohitawate.restaurant.requestsmanager.GETRequestManager;
import com.rohitawate.restaurant.requestsmanager.RequestManager; import com.rohitawate.restaurant.requestsmanager.RequestManager;
import com.rohitawate.restaurant.settings.Settings;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox; import javafx.scene.control.ComboBox;
@ -60,6 +61,7 @@ public class DashboardController implements Initializable {
@Override @Override
public void initialize(URL url, ResourceBundle rb) { public void initialize(URL url, ResourceBundle rb) {
applySettings();
responseBox.getChildren().remove(0); responseBox.getChildren().remove(0);
httpMethodBox.getItems().addAll(httpMethods); httpMethodBox.getItems().addAll(httpMethods);
httpMethodBox.setValue("GET"); httpMethodBox.setValue("GET");
@ -114,4 +116,10 @@ public class DashboardController implements Initializable {
responseTime.setText(Long.toString(response.getTime()) + " ms"); responseTime.setText(Long.toString(response.getTime()) + " ms");
responseSize.setText(Integer.toString(response.getSize()) + " B"); responseSize.setText(Integer.toString(response.getSize()) + " B");
} }
private void applySettings() {
String responseAreaCSS = "-fx-font-family: " + Settings.responseAreaFont + ";" +
"-fx-font-size: " + Settings.responseAreaFontSize;
responseArea.setStyle(responseAreaCSS);
}
} }

View file

@ -15,6 +15,7 @@
*/ */
package com.rohitawate.restaurant.main; package com.rohitawate.restaurant.main;
import com.rohitawate.restaurant.settings.SettingsLoader;
import javafx.application.Application; import javafx.application.Application;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;
@ -24,6 +25,8 @@ import javafx.stage.Stage;
public class Main extends Application { public class Main extends Application {
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
SettingsLoader settingsLoader = new SettingsLoader();
settingsLoader.SettingsLoaderThread.join();
Parent dashboard = FXMLLoader.load(getClass().getResource("/fxml/Dashboard.fxml")); Parent dashboard = FXMLLoader.load(getClass().getResource("/fxml/Dashboard.fxml"));
Stage dashboardStage = new Stage(); Stage dashboardStage = new Stage();
dashboardStage.setScene(new Scene(dashboard)); dashboardStage.setScene(new Scene(dashboard));

View file

@ -0,0 +1,26 @@
/*
* Copyright 2018 Rohit Awate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rohitawate.restaurant.settings;
/*
Holds settings from settings.json which are
loaded by SettingsLoader at startup.
*/
public class Settings {
public static String responseAreaFont;
public static int responseAreaFontSize;
}

View file

@ -0,0 +1,57 @@
/*
* Copyright 2018 Rohit Awate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rohitawate.restaurant.settings;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class SettingsLoader implements Runnable {
public Thread SettingsLoaderThread;
public SettingsLoader() {
SettingsLoaderThread = new Thread(this, "Settings loader thread");
SettingsLoaderThread.start();
}
@Override
public void run() {
try {
StringBuilder settingsJSON = new StringBuilder();
File settingsFile = new File("settings/settings.json");
BufferedReader reader = new BufferedReader(new FileReader(settingsFile));
String line;
while ((line = reader.readLine()) != null) {
settingsJSON.append(line).append('\n');
}
reader.close();
ObjectMapper mapper = new ObjectMapper();
JsonNode nodes = mapper.readTree(settingsJSON.toString());
Settings.responseAreaFont = nodes.get("responseAreaFont").toString();
Settings.responseAreaFontSize = nodes.get("responseAreaFontSize").asInt();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View file

@ -36,7 +36,7 @@
} }
#responseArea { #responseArea {
-fx-font-family: 'Liberation Mono'; -fx-font-family: "Liberation Mono";
-fx-text-fill: #a1a1a1; -fx-text-fill: #a1a1a1;
-fx-background-radius: 0; -fx-background-radius: 0;
-fx-background-insets: 0; -fx-background-insets: 0;