Реализация UI
This commit is contained in:
82
src/main/java/ru/cft/task/restClient/ConfigController.java
Normal file
82
src/main/java/ru/cft/task/restClient/ConfigController.java
Normal file
@ -0,0 +1,82 @@
|
||||
package ru.cft.task.restClient;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Properties;
|
||||
|
||||
public class ConfigController {
|
||||
private Properties props;
|
||||
private InputStream conf_file = null;
|
||||
private OutputStream save_file = null;
|
||||
private static final String CONF_FILE = "config.properties";
|
||||
|
||||
@FXML
|
||||
private TextField url;
|
||||
|
||||
@FXML
|
||||
private TextField port;
|
||||
|
||||
@FXML
|
||||
private Button cancel;
|
||||
|
||||
@FXML
|
||||
private Button save;
|
||||
|
||||
@FXML
|
||||
public void initialize() {
|
||||
props = new Properties();
|
||||
try {
|
||||
File cf = new File(CONF_FILE);
|
||||
if (!cf.exists()) {
|
||||
cf.createNewFile();
|
||||
Utils.showAlert("warn", "Нет сохраненного файла настроек.\n" +
|
||||
"Будут установлены умолчательные параметры.\n" +
|
||||
"Необходимо изменить и сохранить.");
|
||||
}
|
||||
conf_file = new FileInputStream(CONF_FILE);
|
||||
props.load(conf_file);
|
||||
url.setText(props.getProperty("url", "http://localhost"));
|
||||
port.setText(props.getProperty("port", "8080"));
|
||||
} catch (IOException ex) {
|
||||
Utils.showAlert("error", ex.getMessage());
|
||||
} finally {
|
||||
if (conf_file != null) {
|
||||
try {
|
||||
conf_file.close();
|
||||
} catch (IOException ex) {
|
||||
Utils.showAlert("error", ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void saveAction() {
|
||||
try {
|
||||
save_file = new FileOutputStream(CONF_FILE);
|
||||
props.setProperty("url", url.getText());
|
||||
props.setProperty("port", port.getText());
|
||||
props.store(save_file, "Rest server config");
|
||||
Stage stage = (Stage) save.getScene().getWindow();
|
||||
stage.close();
|
||||
} catch (IOException ex) {
|
||||
Utils.showAlert("error", ex.getMessage());
|
||||
} finally {
|
||||
if (save_file != null) {
|
||||
try {
|
||||
save_file.close();
|
||||
} catch (IOException ex) {
|
||||
System.err.println(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void cancelAction() {
|
||||
Stage stage = (Stage) cancel.getScene().getWindow();
|
||||
stage.close();
|
||||
}
|
||||
}
|
@ -16,6 +16,9 @@ public class Main extends Application {
|
||||
Scene scene = new Scene(root);
|
||||
stage.setTitle("Rest Client");
|
||||
stage.setScene(scene);
|
||||
stage.centerOnScreen();
|
||||
MainController controller = new MainController();
|
||||
controller.setMainApp(stage);
|
||||
stage.show();
|
||||
}
|
||||
}
|
||||
|
82
src/main/java/ru/cft/task/restClient/MainController.java
Normal file
82
src/main/java/ru/cft/task/restClient/MainController.java
Normal file
@ -0,0 +1,82 @@
|
||||
package ru.cft.task.restClient;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.stage.Modality;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MainController {
|
||||
|
||||
private Stage mainStage;
|
||||
|
||||
public void setMainApp(Stage mainStage) {
|
||||
this.mainStage = mainStage;
|
||||
}
|
||||
|
||||
public Stage getMainApp() {
|
||||
return this.mainStage;
|
||||
}
|
||||
|
||||
@FXML
|
||||
private MenuItem srv_config;
|
||||
@FXML
|
||||
private MenuItem create;
|
||||
@FXML
|
||||
private MenuItem read;
|
||||
@FXML
|
||||
private MenuItem update;
|
||||
@FXML
|
||||
private MenuItem delete;
|
||||
|
||||
public void quitApp() {
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private Stage showModal(String fxml, String title) {
|
||||
Stage stage = null;
|
||||
try {
|
||||
Scene scene = new Scene(FXMLLoader.load(getClass().getResource(fxml)));
|
||||
stage = new Stage();
|
||||
stage.setScene(scene);
|
||||
stage.setResizable(false);
|
||||
stage.centerOnScreen();
|
||||
stage.setTitle(title);
|
||||
stage.initOwner(mainStage);
|
||||
stage.initModality(Modality.APPLICATION_MODAL);
|
||||
return stage;
|
||||
} catch (IOException e) {
|
||||
Utils.showAlert("error", e.getMessage());
|
||||
} finally {
|
||||
return stage;
|
||||
}
|
||||
}
|
||||
|
||||
public void showServerConfig() {
|
||||
Stage config = showModal("/config.fxml", srv_config.getText());
|
||||
config.showAndWait();
|
||||
}
|
||||
|
||||
public void createAction() {
|
||||
Stage cr = showModal("/form.fxml", create.getText());
|
||||
cr.showAndWait();
|
||||
}
|
||||
|
||||
public void readAction() {
|
||||
Stage rd = showModal("/form.fxml", read.getText());
|
||||
rd.showAndWait();
|
||||
}
|
||||
|
||||
public void updateAction() {
|
||||
Stage upd = showModal("/form.fxml", update.getText());
|
||||
upd.showAndWait();
|
||||
}
|
||||
|
||||
public void deleteAction() {
|
||||
Stage dlt = showModal("/form.fxml", delete.getText());
|
||||
dlt.showAndWait();
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package ru.cft.task.restClient;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class RestActionsController {
|
||||
@FXML
|
||||
private TextField id;
|
||||
|
||||
@FXML
|
||||
private TextField name;
|
||||
|
||||
@FXML
|
||||
private TextField email;
|
||||
|
||||
@FXML
|
||||
private Button cancel;
|
||||
|
||||
@FXML
|
||||
private Button doAction;
|
||||
|
||||
public void cancelAction() {
|
||||
Stage stage = (Stage) cancel.getScene().getWindow();
|
||||
stage.close();
|
||||
}
|
||||
}
|
38
src/main/java/ru/cft/task/restClient/Utils.java
Normal file
38
src/main/java/ru/cft/task/restClient/Utils.java
Normal file
@ -0,0 +1,38 @@
|
||||
package ru.cft.task.restClient;
|
||||
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.stage.Modality;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Utils {
|
||||
public static void showAlert(String type, String message) {
|
||||
Alert.AlertType alertType;
|
||||
|
||||
switch (type) {
|
||||
case "info":
|
||||
alertType = Alert.AlertType.INFORMATION;
|
||||
break;
|
||||
case "error":
|
||||
alertType = Alert.AlertType.ERROR;
|
||||
break;
|
||||
case "warn":
|
||||
alertType = Alert.AlertType.INFORMATION;
|
||||
break;
|
||||
case "confirm":
|
||||
alertType = Alert.AlertType.CONFIRMATION;
|
||||
break;
|
||||
default:
|
||||
alertType = Alert.AlertType.NONE;
|
||||
break;
|
||||
}
|
||||
Alert alert = new Alert(alertType);
|
||||
alert.setHeaderText(null);
|
||||
alert.setTitle(type.toUpperCase());
|
||||
alert.setContentText(message);
|
||||
alert.showAndWait();
|
||||
}
|
||||
}
|
17
src/main/resources/config.fxml
Normal file
17
src/main/resources/config.fxml
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.TextField?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
|
||||
<AnchorPane prefHeight="161.0" prefWidth="283.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.cft.task.restClient.ConfigController">
|
||||
<children>
|
||||
<Label layoutX="16.0" layoutY="14.0" text="URL сервера" />
|
||||
<TextField fx:id="url" layoutX="16.0" layoutY="31.0" prefHeight="25.0" prefWidth="252.0" />
|
||||
<Label layoutX="17.0" layoutY="61.0" text="Порт сервера" />
|
||||
<TextField fx:id="port" layoutX="19.0" layoutY="78.0" />
|
||||
<Button fx:id="save" layoutX="194.0" layoutY="123.0" mnemonicParsing="false" onAction="#saveAction" text="Сохранить" />
|
||||
<Button fx:id="cancel" layoutX="16.0" layoutY="123.0" mnemonicParsing="false" onAction="#cancelAction" text="Отмена" />
|
||||
</children>
|
||||
</AnchorPane>
|
19
src/main/resources/form.fxml
Normal file
19
src/main/resources/form.fxml
Normal file
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.TextField?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
|
||||
<AnchorPane prefHeight="147.0" prefWidth="295.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.cft.task.restClient.RestActionsController">
|
||||
<children>
|
||||
<Label layoutX="15.0" layoutY="14.0" text="Идентификатор" />
|
||||
<TextField layoutX="111.0" layoutY="10.0" prefHeight="25.0" prefWidth="181.0" />
|
||||
<Label layoutX="33.0" layoutY="49.0" text="Имя клиента" />
|
||||
<Label layoutX="27.0" layoutY="83.0" text="Email клиента" />
|
||||
<TextField layoutX="111.0" layoutY="45.0" prefHeight="25.0" prefWidth="181.0" />
|
||||
<TextField layoutX="111.0" layoutY="79.0" prefHeight="25.0" prefWidth="181.0" />
|
||||
<Button fx:id="cancel" layoutX="14.0" layoutY="118.0" mnemonicParsing="false" onAction="#cancelAction" text="Отмена" />
|
||||
<Button fx:id="doAction" layoutX="240.0" layoutY="118.0" mnemonicParsing="false" text="Button" />
|
||||
</children>
|
||||
</AnchorPane>
|
@ -1,12 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import java.lang.*?>
|
||||
<?import java.util.*?>
|
||||
<?import javafx.scene.*?>
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
<?import javafx.scene.control.Menu?>
|
||||
<?import javafx.scene.control.MenuBar?>
|
||||
<?import javafx.scene.control.MenuItem?>
|
||||
<?import javafx.scene.control.SeparatorMenuItem?>
|
||||
<?import javafx.scene.input.KeyCodeCombination?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
|
||||
<AnchorPane xmlns="http://javafx.com/javafx"
|
||||
xmlns:fx="http://javafx.com/fxml"
|
||||
fx:controller="ru.cft.task.restClient.Main"
|
||||
prefHeight="400.0" prefWidth="600.0"/>
|
||||
<VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.cft.task.restClient.MainController">
|
||||
<children>
|
||||
<MenuBar>
|
||||
<menus>
|
||||
<Menu mnemonicParsing="false" text="Клиент">
|
||||
<items>
|
||||
<MenuItem fx:id="create" mnemonicParsing="false" onAction="#createAction" text="Добавить данные" />
|
||||
<MenuItem fx:id="read" mnemonicParsing="false" onAction="#readAction" text="Получить данные" />
|
||||
<MenuItem fx:id="update" mnemonicParsing="false" onAction="#updateAction" text="Изменить данные" />
|
||||
<MenuItem fx:id="delete" mnemonicParsing="false" onAction="#deleteAction" text="Удалить" />
|
||||
<SeparatorMenuItem mnemonicParsing="false" />
|
||||
<MenuItem fx:id="quit" mnemonicParsing="false" onAction="#quitApp" text="Выход">
|
||||
<accelerator>
|
||||
<KeyCodeCombination alt="DOWN" code="F4" control="UP" meta="UP" shift="UP" shortcut="UP" />
|
||||
</accelerator></MenuItem>
|
||||
</items>
|
||||
</Menu>
|
||||
<Menu mnemonicParsing="false" text="Сервер">
|
||||
<items>
|
||||
<MenuItem fx:id="srv_config" mnemonicParsing="false" onAction="#showServerConfig" text="Настройка" />
|
||||
</items>
|
||||
</Menu>
|
||||
</menus>
|
||||
</MenuBar>
|
||||
</children>
|
||||
</VBox>
|
||||
|
Reference in New Issue
Block a user