Skip to content

Commit 1bf7e75

Browse files
committed
Added multithreaded server, admin room
1 parent 536cd49 commit 1bf7e75

37 files changed

+1772
-143
lines changed

src/client/Client.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package client;
2+
3+
import java.io.IOException;
4+
import java.io.ObjectInputStream;
5+
import java.io.ObjectOutputStream;
6+
import java.net.Socket;
7+
8+
public class Client {
9+
private static Client client;
10+
private final int PORT = 1880;
11+
12+
public static void start(){
13+
if(client == null)
14+
client = new Client();
15+
}
16+
public static Client get(){
17+
return client;
18+
}
19+
20+
public synchronized Object sendMessage(String message){
21+
Object obj;
22+
try {
23+
Socket socket = new Socket("localhost", PORT);
24+
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
25+
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
26+
27+
oos.writeObject(message);
28+
obj = ois.readObject();
29+
30+
oos.close();
31+
ois.close();
32+
socket.close();
33+
34+
return obj;
35+
} catch (ClassNotFoundException | IOException e) {
36+
e.printStackTrace();
37+
}
38+
return null;
39+
}
40+
41+
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package sample;
1+
package client;
22

33
import javafx.application.Application;
44
import javafx.fxml.FXMLLoader;
@@ -8,18 +8,19 @@
88
import javafx.stage.StageStyle;
99

1010
public class Main extends Application {
11-
1211
@Override
1312
public void start(Stage primaryStage) throws Exception{
14-
Parent root = FXMLLoader.load(getClass().getResource("Views/admin.fxml"));
13+
Client.start();
14+
15+
Parent root = FXMLLoader.load(getClass().getResource("views/login.fxml"));
1516
primaryStage.initStyle(StageStyle.UNDECORATED);
1617
primaryStage.setTitle("Hello World");
1718
primaryStage.setScene(new Scene(root));
1819
primaryStage.show();
1920
}
2021

21-
2222
public static void main(String[] args) {
2323
launch(args);
2424
}
25+
2526
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
package client.controllers;
2+
3+
import client.database.SendDatabase;
4+
import javafx.collections.FXCollections;
5+
import javafx.collections.ObservableList;
6+
import javafx.fxml.FXML;
7+
import javafx.fxml.FXMLLoader;
8+
import javafx.fxml.Initializable;
9+
import javafx.scene.Parent;
10+
import javafx.scene.Scene;
11+
import javafx.scene.control.*;
12+
import javafx.scene.control.cell.PropertyValueFactory;
13+
import javafx.stage.Stage;
14+
import javafx.stage.StageStyle;
15+
16+
import client.models.AbstractTable;
17+
import client.models.ManagerTables;
18+
import server.entities.Users;
19+
import utils.Alert;
20+
21+
import java.io.IOException;
22+
import java.net.URL;
23+
import java.util.ArrayList;
24+
import java.util.ResourceBundle;
25+
26+
public class AdminController implements Initializable {
27+
public AbstractTable modelPrototype;
28+
private AbstractTable modelSelected;
29+
30+
ObservableList<AbstractTable> models = FXCollections.observableArrayList();
31+
ObservableList<String> choices = FXCollections.observableArrayList("users", "admins");
32+
33+
@FXML
34+
ChoiceBox<String> tableNameChoiceBox ;
35+
36+
@FXML
37+
TableView<AbstractTable> table;
38+
39+
@FXML
40+
Label updateLabel;
41+
42+
@Override
43+
public void initialize(URL url, ResourceBundle resourceBundle) {
44+
tableNameChoiceBox.getItems().addAll(choices);
45+
tableNameChoiceBox.setOnAction(event -> {
46+
String choice = tableNameChoiceBox.getValue();
47+
changeTable(choice);
48+
displayColumns();
49+
updateTable();
50+
});
51+
tableNameChoiceBox.setValue("users");
52+
53+
//получить выделенную запись
54+
table.getSelectionModel().selectedItemProperty().addListener(
55+
(observable, oldValue, newValue) -> setSelected((AbstractTable)newValue));
56+
57+
//двойной клик по записи открывает окно редактирования
58+
table.setRowFactory( tv -> {
59+
TableRow<AbstractTable> row = new TableRow<>();
60+
row.setOnMouseClicked(event -> {
61+
if (event.getClickCount() == 2 && (! row.isEmpty()) ) {
62+
onEdit();
63+
}
64+
});
65+
return row ;
66+
});
67+
68+
}
69+
70+
@FXML
71+
public void onAdd() {
72+
try {
73+
AdminDialogController controller = openDialog();
74+
//специфичные для добавления настройки
75+
controller.setMode("Add");
76+
controller.setModel(modelPrototype.cloneEmpty());
77+
}
78+
catch (IOException e) { e.printStackTrace(); }
79+
}
80+
81+
@FXML
82+
public void onEdit(){
83+
if(modelSelected != null) {
84+
try {
85+
AdminDialogController controller = openDialog();
86+
//специфичные для редактирования настройки
87+
controller.setMode("Edit");
88+
controller.setModel(modelSelected);
89+
}
90+
catch (IOException e) { e.printStackTrace(); }
91+
}
92+
else Alert.alert("Не выбрано поле!","Пожалуйста, выберите запись.");
93+
}
94+
95+
@FXML
96+
public void onDelete() {
97+
if(modelSelected != null) {
98+
updateTable("delete", modelSelected);
99+
updateTable();
100+
}
101+
else Alert.alert("Не выбрано поле!", "Пожалуйста, выберите запись.");
102+
}
103+
104+
private AdminDialogController openDialog() throws IOException {
105+
//загружаем adminDialog.fxml
106+
FXMLLoader loader = new FXMLLoader();
107+
loader.setLocation(getClass().getResource("../Views/adminDialog.fxml"));
108+
Parent rootLayout = loader.load();
109+
110+
//создаём новое окно и отображаем adminDialog.fxml
111+
Stage dlg = new Stage();
112+
dlg.initStyle(StageStyle.UNDECORATED);
113+
dlg.setTitle("Редактор");
114+
dlg.setScene(new Scene(rootLayout));
115+
116+
//даём контроллеру доступ к главному приложению
117+
AdminDialogController controller = loader.getController();
118+
controller.setAdminController(this);
119+
120+
// отображаем диалоговое окно и настраиваем
121+
dlg.show();
122+
controller.setStage(dlg);
123+
124+
return controller;
125+
}
126+
127+
@FXML
128+
public void onClose() {
129+
Stage stage = (Stage) table.getScene().getWindow();
130+
131+
Parent root = null;
132+
try { root = FXMLLoader.load(getClass().getResource("../views/login.fxml"));
133+
} catch (IOException e) { e.printStackTrace(); }
134+
135+
stage.setWidth(600);
136+
stage.setHeight(400);
137+
stage.setScene(new Scene(root));
138+
stage.show();
139+
}
140+
141+
@FXML
142+
public void onUpdate(){
143+
updateTable();
144+
}
145+
146+
private void displayColumns() {
147+
table.getColumns().clear();
148+
for(String f : modelPrototype.fields()) {
149+
TableColumn<AbstractTable, String> col = new TableColumn<>(f);
150+
col.setCellValueFactory(new PropertyValueFactory<>(f));
151+
table.getColumns().add(col);
152+
}
153+
}
154+
155+
public void setSelected(AbstractTable at){
156+
modelSelected = at;
157+
}
158+
159+
public AbstractTable getSelected(){
160+
return modelSelected;
161+
}
162+
163+
public void updateTable(){
164+
try{
165+
models.clear();
166+
models.addAll( SendDatabase.getArray(modelPrototype));
167+
table.setItems(models);
168+
new Thread() {
169+
public void run() {
170+
updateLabel.setVisible(true);
171+
try{
172+
sleep(2000);
173+
}catch(Exception ignored){};
174+
updateLabel.setVisible(false);
175+
}
176+
}.start();
177+
}
178+
catch(IllegalArgumentException e){
179+
e.printStackTrace();
180+
Alert.alert("Ошибка", e.getMessage());
181+
}
182+
catch(Exception e){
183+
e.printStackTrace();
184+
Alert.alert("Ошибка", "Не получилось соединиться с сервером");
185+
}
186+
}
187+
public void updateTable(String command, AbstractTable table){
188+
try{
189+
SendDatabase.updateDB(command, table);
190+
}
191+
catch(IllegalArgumentException e){
192+
e.printStackTrace();
193+
Alert.alert("Ошибка", e.getMessage());
194+
}
195+
catch(Exception e){
196+
e.printStackTrace();
197+
Alert.alert("Ошибка", "Не получилось соединиться с сервером");
198+
}
199+
}
200+
201+
public void changeTable(String tableName){
202+
models.clear();
203+
modelPrototype = ManagerTables.get(tableName);
204+
}
205+
206+
}

0 commit comments

Comments
 (0)