1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package Launchers;
21
22 import Utilities.*;
23 import javafx.application.Platform;
24 import javafx.geometry.Insets;
25 import javafx.scene.control.*;
26 import javafx.scene.layout.GridPane;
27
28 import java.sql.Connection;
29 import java.sql.PreparedStatement;
30 import java.sql.ResultSet;
31 import java.sql.SQLException;
32 import java.util.Optional;
33
34 public class AddGroup {
35 public static Group addGroup(String year, callBack successCallback) {
36 Optional<Group> returnGroup = Optional.empty();
37 Dialog<String> dialog = new Dialog<>();
38 dialog.setTitle("Add new group");
39
40
41 ButtonType addGrp = new ButtonType("Add", ButtonBar.ButtonData.OK_DONE);
42 dialog.getDialogPane().getButtonTypes().addAll(addGrp, ButtonType.CANCEL);
43
44
45 GridPane grid = new GridPane();
46 grid.setHgap(10);
47 grid.setVgap(10);
48 grid.setPadding(new Insets(20, 150, 10, 10));
49
50 TextField groupName = new TextField();
51 groupName.setPromptText("Group Name");
52
53 grid.add(new Label("Group Name:"), 0, 0);
54 grid.add(groupName, 1, 0);
55
56
57
58 javafx.scene.Node addGroupButton = dialog.getDialogPane().lookupButton(addGrp);
59 addGroupButton.setDisable(true);
60
61
62 groupName.textProperty().addListener((observable, oldValue, newValue) -> addGroupButton.setDisable(newValue.trim().isEmpty()));
63
64 dialog.getDialogPane().setContent(grid);
65
66
67 Platform.runLater(() -> groupName.requestFocus());
68
69
70 dialog.setResultConverter(dialogButton -> {
71 if (dialogButton == addGrp) {
72 return groupName.getText();
73 }
74 return null;
75 });
76
77 Optional<String> result = dialog.showAndWait();
78 if (result.isPresent()) {
79 try (Connection con = DbInt.getConnection(year);
80 PreparedStatement prep = con.prepareStatement("INSERT INTO groups(Name) VALUES(?)", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
81
82 prep.setString(1, result.get());
83
84 prep.execute();
85 } catch (SQLException e) {
86 LogToFile.log(e, Severity.SEVERE, CommonErrors.returnSqlMessage(e));
87 }
88 returnGroup = Optional.of(new Group(result.get(), year));
89 successCallback.doAction(returnGroup.get());
90
91 }
92 return returnGroup.orElse(null);
93 }
94
95 public static Group addGroup(String year, String groupName, callBack successCallback) {
96 Optional<Group> returnGroup = Optional.empty();
97 Dialog<String> dialog = new Dialog<>();
98 dialog.setTitle("Edit group " + groupName);
99
100
101 ButtonType addGrp = new ButtonType("Save", ButtonBar.ButtonData.OK_DONE);
102 dialog.getDialogPane().getButtonTypes().addAll(addGrp, ButtonType.CANCEL);
103
104
105 GridPane grid = new GridPane();
106 grid.setHgap(10);
107 grid.setVgap(10);
108 grid.setPadding(new Insets(20, 150, 10, 10));
109
110 TextField groupNameTextField = new TextField();
111 groupNameTextField.setText(groupName);
112
113 grid.add(new Label("Group Name:"), 0, 0);
114 grid.add(groupNameTextField, 1, 0);
115
116
117
118 javafx.scene.Node addGroupButton = dialog.getDialogPane().lookupButton(addGrp);
119 addGroupButton.setDisable(true);
120
121
122 groupNameTextField.textProperty().addListener((observable, oldValue, newValue) -> addGroupButton.setDisable(newValue.trim().isEmpty()));
123
124 dialog.getDialogPane().setContent(grid);
125
126
127 Platform.runLater(() -> groupNameTextField.requestFocus());
128
129
130 dialog.setResultConverter(dialogButton -> {
131 if (dialogButton == addGrp) {
132 return groupNameTextField.getText();
133 }
134 return null;
135 });
136
137 Optional<String> result = dialog.showAndWait();
138 if (result.isPresent()) {
139 try (Connection con = DbInt.getConnection(year);
140 PreparedStatement prep = con.prepareStatement("UPDATE groups SET Name=? WHERE Name=?", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
141 prep.setString(1, result.get());
142 prep.setString(2, groupName);
143
144 prep.execute();
145 } catch (SQLException e) {
146 LogToFile.log(e, Severity.SEVERE, CommonErrors.returnSqlMessage(e));
147 }
148 returnGroup = Optional.of(new Group(result.get(), year));
149 successCallback.doAction(returnGroup.get());
150 }
151 return returnGroup.orElse(null);
152 }
153
154 public interface callBack {
155
156 void doAction(Group grp);
157 }
158 }