little clean up in the code

This commit is contained in:
Mateusz Kowalczyk 2019-11-24 11:50:30 +01:00
parent 461738d25e
commit a692141db6
5 changed files with 25 additions and 12 deletions

View File

@ -1,5 +1,6 @@
package application; package application;
import application.client.view.ResourceLoader;
import javafx.application.Application; import javafx.application.Application;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;

View File

@ -1,6 +1,8 @@
package application; package application;
import application.client.view.ResourceLoader;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -8,8 +10,11 @@ import java.io.OutputStream;
import java.util.Properties; import java.util.Properties;
public class PropertiesLoader { public class PropertiesLoader {
public static final String CONFIG_NAME_FILE = "config.properties";
private static final PropertiesLoader instance = new PropertiesLoader(); private static final PropertiesLoader instance = new PropertiesLoader();
private ResourceLoader resourceLoader = ResourceLoader.getInstance(); private ResourceLoader resourceLoader = ResourceLoader.getInstance();
private Properties properties = null;
public PropertiesLoader () {} public PropertiesLoader () {}
@ -18,23 +23,21 @@ public class PropertiesLoader {
} }
public Properties load() { public Properties load() {
Properties properties = new Properties(); this.properties = new Properties();
try { try {
InputStream inputStream = resourceLoader.getResourceAsStream("config.properties"); InputStream inputStream = resourceLoader.getResourceAsStream(CONFIG_NAME_FILE);
properties.load(inputStream); this.properties.load(inputStream);
inputStream.close(); inputStream.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
return this.properties;
return properties;
} }
public void save(Properties prop) { public void save(Properties prop) {
try { try {
OutputStream output = new FileOutputStream(this.resourceLoader.getPath("config.properties")); OutputStream output = new FileOutputStream(this.resourceLoader.getPath(CONFIG_NAME_FILE));
prop.store(output, null); prop.store(output, null);
output.close(); output.close();
@ -43,4 +46,12 @@ public class PropertiesLoader {
e.printStackTrace(); e.printStackTrace();
} }
} }
public String get(String name) {
if (this.properties == null) {
this.load();
}
return this.properties.getProperty(name);
}
} }

View File

@ -1,18 +1,19 @@
package application; package application.client.view;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
public class ResourceLoader { public class ResourceLoader {
private static final ResourceLoader instance = new ResourceLoader(); private static final ResourceLoader instance = new ResourceLoader();
public static final String RESOURCE_FOLDER = "resource/";
String name; String name;
public URL getResource(String name) { public URL getResource(String name) {
return getClass().getClassLoader().getResource("resource/" + name); return getClass().getClassLoader().getResource(RESOURCE_FOLDER + name);
} }
public InputStream getResourceAsStream(String name) { public InputStream getResourceAsStream(String name) {
return getClass().getClassLoader().getResourceAsStream("resource/" + name); return getClass().getClassLoader().getResourceAsStream(RESOURCE_FOLDER + name);
} }
public String getPath(String name) { public String getPath(String name) {

View File

@ -1,4 +1,4 @@
package application; package application.client.view.mainPage;
import application.client.Connector; import application.client.Connector;
import application.client.exception.CannotConnectException; import application.client.exception.CannotConnectException;

View File

@ -7,7 +7,7 @@
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?> <?import javafx.scene.text.Font?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainPageController"> <VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.client.view.mainPage.MainPageController">
<children> <children>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS"> <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children> <children>