|
| 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