From 461738d25e8aca5722e690dc94c722621b3d080e Mon Sep 17 00:00:00 2001 From: Mateusz Kowalczyk Date: Sat, 23 Nov 2019 22:02:12 +0100 Subject: [PATCH] Create connector --- src/application/Main.java | 6 +-- src/application/MainPageController.java | 22 ++++++++ src/application/PropertiesLoader.java | 46 +++++++++++++++++ src/application/ResourceLoader.java | 33 ++++++++++++ src/application/client/Connector.java | 27 ++++++++++ .../exception/CannotConnectException.java | 6 +++ src/resource/config.properties | 1 + src/resource/mainPage.fxml | 50 +++---------------- 8 files changed, 144 insertions(+), 47 deletions(-) create mode 100644 src/application/PropertiesLoader.java create mode 100644 src/application/ResourceLoader.java create mode 100644 src/application/client/Connector.java create mode 100644 src/application/client/exception/CannotConnectException.java create mode 100644 src/resource/config.properties diff --git a/src/application/Main.java b/src/application/Main.java index 0b55e91..8844289 100644 --- a/src/application/Main.java +++ b/src/application/Main.java @@ -6,14 +6,14 @@ import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; -import java.net.URL; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ - URL mainPage = ClassLoader.getSystemClassLoader().getResource("resource/mainPage.fxml"); - Parent root = FXMLLoader.load(mainPage); + ResourceLoader loader = ResourceLoader.getInstance(); + + Parent root = FXMLLoader.load(loader.getResource("mainPage.fxml")); primaryStage.setTitle("FTP SAR"); primaryStage.setScene(new Scene(root)); primaryStage.show(); diff --git a/src/application/MainPageController.java b/src/application/MainPageController.java index bb2acae..093056b 100644 --- a/src/application/MainPageController.java +++ b/src/application/MainPageController.java @@ -1,7 +1,29 @@ package application; +import application.client.Connector; +import application.client.exception.CannotConnectException; +import javafx.event.Event; +import javafx.fxml.FXML; +import javafx.scene.control.Label; +import javafx.scene.control.TextField; + public class MainPageController { + public TextField rawFieldWithIp = null; + public Label errorLabel = null; + private Connector connector = Connector.getInstance(); + public MainPageController() { System.out.println("Controller work!"); } + + @FXML + public void connectButton(Event e) { + System.out.println(rawFieldWithIp.getText());; + + try { + this.connector.connect(this.rawFieldWithIp.getText()); + } catch (CannotConnectException ex) { + this.errorLabel.setText("Cannot connect"); + } + } } diff --git a/src/application/PropertiesLoader.java b/src/application/PropertiesLoader.java new file mode 100644 index 0000000..d230bc9 --- /dev/null +++ b/src/application/PropertiesLoader.java @@ -0,0 +1,46 @@ +package application; + + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Properties; + +public class PropertiesLoader { + private static final PropertiesLoader instance = new PropertiesLoader(); + private ResourceLoader resourceLoader = ResourceLoader.getInstance(); + + public PropertiesLoader () {} + + public static PropertiesLoader getInstance() { + return instance; + } + + public Properties load() { + Properties properties = new Properties(); + + try { + InputStream inputStream = resourceLoader.getResourceAsStream("config.properties"); + properties.load(inputStream); + + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + return properties; + } + + public void save(Properties prop) { + try { + OutputStream output = new FileOutputStream(this.resourceLoader.getPath("config.properties")); + prop.store(output, null); + + output.close(); + System.out.println(prop); + } catch (IOException e){ + e.printStackTrace(); + } + } +} diff --git a/src/application/ResourceLoader.java b/src/application/ResourceLoader.java new file mode 100644 index 0000000..a08cd38 --- /dev/null +++ b/src/application/ResourceLoader.java @@ -0,0 +1,33 @@ +package application; + +import java.io.InputStream; +import java.net.URL; + +public class ResourceLoader { + private static final ResourceLoader instance = new ResourceLoader(); + String name; + + public URL getResource(String name) { + return getClass().getClassLoader().getResource("resource/" + name); + } + + public InputStream getResourceAsStream(String name) { + return getClass().getClassLoader().getResourceAsStream("resource/" + name); + } + + public String getPath(String name) { + return this.getResource(name).getPath(); + } + + public static ResourceLoader getInstance() { + return instance; + } + + public void test(String name) { + this.name = name; + } + + public String get() { + return this.name; + } +} diff --git a/src/application/client/Connector.java b/src/application/client/Connector.java new file mode 100644 index 0000000..7ec4e8b --- /dev/null +++ b/src/application/client/Connector.java @@ -0,0 +1,27 @@ +package application.client; + +import application.client.exception.CannotConnectException; + +import java.io.IOException; +import java.net.Socket; + +public class Connector { + private static final Connector instance = new Connector(); + + private Socket client = null; + + private Connector() { + } + + public static Connector getInstance() { + return instance; + } + + public void connect(String ip) throws CannotConnectException { + try { + this.client = new Socket(ip, 7332); + } catch (IOException e) { + throw new CannotConnectException(); + } + } +} diff --git a/src/application/client/exception/CannotConnectException.java b/src/application/client/exception/CannotConnectException.java new file mode 100644 index 0000000..8c38252 --- /dev/null +++ b/src/application/client/exception/CannotConnectException.java @@ -0,0 +1,6 @@ +package application.client.exception; + +import java.io.IOException; + +public class CannotConnectException extends IOException { +} diff --git a/src/resource/config.properties b/src/resource/config.properties new file mode 100644 index 0000000..48a6ca5 --- /dev/null +++ b/src/resource/config.properties @@ -0,0 +1 @@ +port=7332 \ No newline at end of file diff --git a/src/resource/mainPage.fxml b/src/resource/mainPage.fxml index 4184685..310f671 100644 --- a/src/resource/mainPage.fxml +++ b/src/resource/mainPage.fxml @@ -1,62 +1,24 @@ + - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -