Create connector

This commit is contained in:
Mateusz Kowalczyk 2019-11-23 22:02:12 +01:00
parent b31762e410
commit 461738d25e
8 changed files with 144 additions and 47 deletions

View File

@ -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();

View File

@ -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");
}
}
}

View File

@ -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();
}
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}
}

View File

@ -0,0 +1,6 @@
package application.client.exception;
import java.io.IOException;
public class CannotConnectException extends IOException {
}

View File

@ -0,0 +1 @@
port=7332

View File

@ -1,62 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?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">
<children>
<MenuBar VBox.vgrow="NEVER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="New" />
<MenuItem mnemonicParsing="false" text="Open…" />
<Menu mnemonicParsing="false" text="Open Recent" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem mnemonicParsing="false" text="Save" />
<MenuItem mnemonicParsing="false" text="Save As…" />
<MenuItem mnemonicParsing="false" text="Revert" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Preferences…" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Quit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Undo" />
<MenuItem mnemonicParsing="false" text="Redo" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Cut" />
<MenuItem mnemonicParsing="false" text="Copy" />
<MenuItem mnemonicParsing="false" text="Paste" />
<MenuItem mnemonicParsing="false" text="Delete" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Select All" />
<MenuItem mnemonicParsing="false" text="Unselect All" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About MyHelloApp" />
</items>
</Menu>
</menus>
</MenuBar>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<Label alignment="CENTER" layoutX="197.0" layoutY="173.0" style="&#10;" text="FTP SAR - Sender And Reciver" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false">
<Label alignment="CENTER" layoutX="197.0" layoutY="45.0" style="&#10;" text="FTP SAR - Sender And Reciver" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false">
<font>
<Font size="18.0" />
</font>
</Label>
<TextField fx:id="rawFieldWithIp" layoutX="239.0" layoutY="82.0" promptText="Insert ip" />
<Button layoutX="290.0" layoutY="122.0" mnemonicParsing="false" onAction="#connectButton" text="Connect" />
<Label fx:id="errorLabel" alignment="CENTER" contentDisplay="CENTER" layoutX="15.0" layoutY="14.0" prefHeight="18.0" prefWidth="608.0" textFill="RED" />
</children>
</AnchorPane>
</children>