diff --git a/ProjectAirFighter/src/main/java/com/projectairfighter/FormAirFighter.java b/ProjectAirFighter/src/main/java/com/projectairfighter/FormAirFighter.java index f38b42c..7099bbd 100644 --- a/ProjectAirFighter/src/main/java/com/projectairfighter/FormAirFighter.java +++ b/ProjectAirFighter/src/main/java/com/projectairfighter/FormAirFighter.java @@ -7,6 +7,7 @@ import javafx.application.Application; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; +import javafx.fxml.Initializable; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; @@ -17,8 +18,10 @@ import javafx.scene.input.KeyEvent; import javafx.stage.Stage; import java.io.IOException; +import java.net.URL; +import java.util.ResourceBundle; -public class FormAirFighter extends Application { +public class FormAirFighter extends Application implements Initializable { private AbstractStrategy strategy; private DrawningWarPlane drawningWarPlane; @@ -106,7 +109,6 @@ public class FormAirFighter extends Application { Parent root = loader.load(); canvasAirFighter = (Canvas) loader.getNamespace().get("canvasAirFighter"); comboBoxStrategy = (ComboBox) loader.getNamespace().get("comboBoxStrategy"); - initComboBox(); strategy = null; Scene scene = new Scene(root, 800, 400); @@ -115,11 +117,12 @@ public class FormAirFighter extends Application { primaryStage.show(); } - public void initComboBox() { - comboBoxStrategy.getItems().addAll("К центру", "К краю"); - } - public static void main(String[] args) { launch(args); } + + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + comboBoxStrategy.getItems().addAll("К центру", "К краю"); + } } diff --git a/ProjectAirFighter/src/main/java/com/projectairfighter/FormWarPlaneCollection.java b/ProjectAirFighter/src/main/java/com/projectairfighter/FormWarPlaneCollection.java index 035240e..6229975 100644 --- a/ProjectAirFighter/src/main/java/com/projectairfighter/FormWarPlaneCollection.java +++ b/ProjectAirFighter/src/main/java/com/projectairfighter/FormWarPlaneCollection.java @@ -1,14 +1,13 @@ package com.projectairfighter; -import com.projectairfighter.collectiongenericobjects.AbstractCompany; -import com.projectairfighter.collectiongenericobjects.Hangar; -import com.projectairfighter.collectiongenericobjects.MassiveGenericObjects; +import com.projectairfighter.collectiongenericobjects.*; import com.projectairfighter.drawnings.DrawningAirFighter; import com.projectairfighter.drawnings.DrawningWarPlane; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; +import javafx.fxml.Initializable; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Parent; @@ -21,9 +20,16 @@ import javafx.scene.paint.Color; import javafx.stage.Stage; import java.io.IOException; +import java.net.URL; import java.util.Random; +import java.util.ResourceBundle; +import java.util.Stack; + +public class FormWarPlaneCollection extends Application implements Initializable { + private StorageCollection storageCollection; + + private Stack removedPlanesStack; -public class FormWarPlaneCollection extends Application { private AbstractCompany company; // Окно отрисовки @@ -36,6 +42,21 @@ public class FormWarPlaneCollection extends Application { @FXML private TextField textBox; + @FXML + private SplitPane splitPane; + + @FXML + private TextField textFieldCollectionName; + + @FXML + private RadioButton radioButtonMassive; + + @FXML + private RadioButton radioButtonList; + + @FXML + private ListView listViewCollection; + private static FormWarPlaneCollection formWarPlaneCollection; public static void setFormWarPlaneCollection(FormWarPlaneCollection controller) { @@ -46,14 +67,15 @@ public class FormWarPlaneCollection extends Application { return formWarPlaneCollection; } + @Override + public void initialize(URL location, ResourceBundle resources) { + storageCollection = new StorageCollection<>(); + removedPlanesStack = new Stack<>(); + } + @FXML private void comboBoxSelectorCompany(ActionEvent event) { - String selectedCompany = comboBox.getValue(); - switch (selectedCompany) { - case "Хранилище": - company = new Hangar((int) canvasWarPlane.getWidth(), (int) canvasWarPlane.getHeight(), new MassiveGenericObjects<>()); - break; - } + splitPane.getItems().get(1).setDisable(true); } private void createObject(String type) { @@ -108,6 +130,7 @@ public class FormWarPlaneCollection extends Application { int pos = Integer.parseInt(text); DrawningWarPlane removedPlane = company.removePlane(pos); if (removedPlane != null) { + removedPlanesStack.push(removedPlane); showAlert("Объект удален"); company.show(canvasWarPlane); } else { @@ -118,7 +141,7 @@ public class FormWarPlaneCollection extends Application { } @FXML - private void buttonRefresh(ActionEvent event){ + private void buttonRefresh(ActionEvent event) { if (company == null) return; company.show(canvasWarPlane); @@ -148,7 +171,7 @@ public class FormWarPlaneCollection extends Application { } @FXML - private void buttonGoToCheck(ActionEvent event){ + private void buttonGoToCheck(ActionEvent event) { if (company == null) return; DrawningWarPlane warPlane = null; @@ -163,22 +186,11 @@ public class FormWarPlaneCollection extends Application { if (warPlane == null) return; - try { - FXMLLoader loader = new FXMLLoader(getClass().getResource("FormAirFighter.fxml")); - Parent root = loader.load(); - FormAirFighter controller = loader.getController(); - controller.setWarPlane(warPlane); - controller.initComboBox(); - Stage stage = new Stage(); - stage.setScene(new Scene(root)); - stage.showAndWait(); - } catch (IOException e) { - e.printStackTrace(); - } + goToFormAirFighter(warPlane); } @FXML - private void buttonGoFormInfo(ActionEvent event){ + private void buttonGoFormConstructor(ActionEvent event) { if (company == null) return; try { @@ -189,13 +201,112 @@ public class FormWarPlaneCollection extends Application { stage.setScene(new Scene(root)); controller.start(stage); FormWarPlaneCollection.setFormWarPlaneCollection(this); - stage.show(); } catch (IOException e) { e.printStackTrace(); } } + @FXML + private void buttonGoToFormWithDeleteObject(ActionEvent event){ + if (!removedPlanesStack.isEmpty()) { + goToFormAirFighter(removedPlanesStack.pop()); + } else { + showAlert("Стек удаленных объектов пуст."); + } + + } + + private void goToFormAirFighter(DrawningWarPlane plane){ + try { + FXMLLoader loader = new FXMLLoader(getClass().getResource("FormAirFighter.fxml")); + Parent root = loader.load(); + FormAirFighter controller = loader.getController(); + controller.setWarPlane(plane); + Stage stage = new Stage(); + stage.setScene(new Scene(root)); + stage.showAndWait(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @FXML + void buttonCollectionAdd(ActionEvent event) { + if (textFieldCollectionName.getText().isEmpty() || (!radioButtonList.isSelected() && !radioButtonMassive.isSelected())) { + showError("Не все данные заполнены"); + return; + } + + CollectionType collectionType = CollectionType.None; + if (radioButtonMassive.isSelected()) { + collectionType = CollectionType.Massive; + } else if (radioButtonList.isSelected()) { + collectionType = CollectionType.List; + } + + storageCollection.addCollection(textFieldCollectionName.getText(), collectionType); + refreshListBoxItems(); + } + + @FXML + void buttonCollectionDel(ActionEvent event) { + if (listViewCollection.getSelectionModel().getSelectedIndex() < 0 || listViewCollection.getSelectionModel().getSelectedItem() == null) { + showError("Коллекция не выбрана"); + return; + } + + Alert confirmDialog = new Alert(Alert.AlertType.CONFIRMATION); + confirmDialog.setTitle("Удаление"); + confirmDialog.setHeaderText(null); + confirmDialog.setContentText("Удалить коллекцию?"); + if (confirmDialog.showAndWait().orElse(ButtonType.CANCEL) != ButtonType.OK) { + return; + } + + storageCollection.delCollection(listViewCollection.getSelectionModel().getSelectedItem()); + refreshListBoxItems(); + } + + private void refreshListBoxItems() { + listViewCollection.getItems().clear(); + for (String colName : storageCollection.Keys()) { + if (colName != null && !colName.isEmpty()) { + listViewCollection.getItems().add(colName); + } + } + } + + @FXML + void buttonCreateCompany(ActionEvent event) { + if (listViewCollection.getSelectionModel().getSelectedIndex() < 0 || listViewCollection.getSelectionModel().getSelectedItem() == null) { + showAlert("Коллекция не выбрана"); + return; + } + + ICollectionGenericObjects collection = storageCollection.get(listViewCollection.getSelectionModel().getSelectedItem()); + if (collection == null) { + showAlert("Коллекция не проинициализирована"); + return; + } + + String selectedCompany = comboBox.getValue(); + + if (comboBox.getValue() == null) { + showError("Не выбрано хранилище"); + return; + } + + switch (selectedCompany) { + case "Хранилище": + company = new Hangar((int) canvasWarPlane.getWidth(), (int) canvasWarPlane.getHeight(), collection); + break; + } + + splitPane.getItems().get(1).setDisable(false); + refreshListBoxItems(); + } + private void showAlert(String message) { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Сообщение"); @@ -204,6 +315,15 @@ public class FormWarPlaneCollection extends Application { alert.showAndWait(); } + private void showError(String message) { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("Ошибка"); + alert.setHeaderText(null); + alert.setContentText(message); + alert.showAndWait(); + } + + // Создание объекта @FXML private void createAiFighter(ActionEvent event) { @@ -225,7 +345,7 @@ public class FormWarPlaneCollection extends Application { canvasWarPlane = (Canvas) loader.getNamespace().get("canvasWarPlane"); comboBox = (ComboBox) loader.getNamespace().get("comboBox"); comboBox.getItems().addAll("Хранилище"); - Scene scene = new Scene(root, 1250, 700); + Scene scene = new Scene(root, 1292, 765); primaryStage.setTitle("Истребитель"); primaryStage.setScene(scene); primaryStage.show(); diff --git a/ProjectAirFighter/src/main/java/com/projectairfighter/collectiongenericobjects/CollectionType.java b/ProjectAirFighter/src/main/java/com/projectairfighter/collectiongenericobjects/CollectionType.java new file mode 100644 index 0000000..8ed669d --- /dev/null +++ b/ProjectAirFighter/src/main/java/com/projectairfighter/collectiongenericobjects/CollectionType.java @@ -0,0 +1,19 @@ +package com.projectairfighter.collectiongenericobjects; + +public enum CollectionType { + + /** + Неопределно + */ + None, + + /** + Массив + */ + Massive, + + /** + Список + */ + List +} diff --git a/ProjectAirFighter/src/main/java/com/projectairfighter/collectiongenericobjects/ListGenericObjects.java b/ProjectAirFighter/src/main/java/com/projectairfighter/collectiongenericobjects/ListGenericObjects.java new file mode 100644 index 0000000..aa73a2a --- /dev/null +++ b/ProjectAirFighter/src/main/java/com/projectairfighter/collectiongenericobjects/ListGenericObjects.java @@ -0,0 +1,63 @@ +package com.projectairfighter.collectiongenericobjects; + +import java.util.ArrayList; +import java.util.List; + +public class ListGenericObjects implements ICollectionGenericObjects { + private final List collection; + private int maxCount; + + @Override + public int getCount() { + return collection.size(); + } + + @Override + public void setMaxCount(int value) { + if (value > 0) { + maxCount = value; + } + } + + public ListGenericObjects() { + collection = new ArrayList<>(); + } + + @Override + public T get(int position) { + if (position >= 0 && position < getCount()) { + return collection.get(position); + } else { + return null; + } + } + + @Override + public int insert(T obj) { + if (getCount() == maxCount) { + return -1; + } + collection.add(obj); + return getCount(); + } + + @Override + public int insert(T obj, int position) { + if (position < 0 || position >= getCount() || getCount() == maxCount) { + return -1; + } + collection.add(position, obj); + return position; + } + + @Override + public T remove(int position) { + if (position >= getCount() || position < 0) { + return null; + } + T obj = collection.get(position); + collection.remove(position); + return obj; + } +} + diff --git a/ProjectAirFighter/src/main/java/com/projectairfighter/collectiongenericobjects/StorageCollection.java b/ProjectAirFighter/src/main/java/com/projectairfighter/collectiongenericobjects/StorageCollection.java new file mode 100644 index 0000000..9b0d277 --- /dev/null +++ b/ProjectAirFighter/src/main/java/com/projectairfighter/collectiongenericobjects/StorageCollection.java @@ -0,0 +1,89 @@ +package com.projectairfighter.collectiongenericobjects; + +import java.util.*; + +/** + * Класс-хранилище коллекций. + * + * @param Тип элементов коллекций. + */ +public class StorageCollection { + /** + * Словарь (хранилище) с коллекциями. + */ + private final Map> storages; + + /** + * Возвращение списка названий коллекций. + * + * @return Список названий коллекций. + */ + public List Keys() { + return new ArrayList<>(storages.keySet()); + } + + /** + * Конструктор. + */ + public StorageCollection() { + storages = new HashMap<>(); + } + + /** + * Добавление коллекции в хранилище. + * + * @param name Название коллекции. + * @param collectionType Тип коллекции. + */ + public void addCollection(String name, CollectionType collectionType) { + if (name == null || storages.containsKey(name)) { + return; + } + switch (collectionType) { + case None: + return; + case Massive: + storages.put(name, new MassiveGenericObjects<>()); + return; + case List: + storages.put(name, new ListGenericObjects<>()); + return; + } + } + + /** + * Удаление коллекции из хранилища. + * + * @param name Название коллекции. + */ + public void delCollection(String name) { + if (name == null || !storages.containsKey(name)) { + return; + } + storages.remove(name); + } + + /** + * Доступ к коллекции по её названию. + * + * @param name Название коллекции. + * @return Коллекция или null, если коллекция с данным названием отсутствует. + */ + public ICollectionGenericObjects get(String name) { + if (name == null || !storages.containsKey(name)) { + return null; + } + return storages.get(name); + } + + /** + * Доступ к элементу коллекции по названию коллекции и индексу элемента. + * + * @param name Название коллекции. + * @param index Индекс элемента в коллекции. + * @return Элемент коллекции или null, если коллекция с данным названием отсутствует или индекс некорректен. + */ + public T getElement(String name, int index) { + return this.get(name).get(index); + } +} diff --git a/ProjectAirFighter/src/main/resources/com/projectairfighter/FormWarPlaneCollection.fxml b/ProjectAirFighter/src/main/resources/com/projectairfighter/FormWarPlaneCollection.fxml index 347c2f3..70f32b6 100644 --- a/ProjectAirFighter/src/main/resources/com/projectairfighter/FormWarPlaneCollection.fxml +++ b/ProjectAirFighter/src/main/resources/com/projectairfighter/FormWarPlaneCollection.fxml @@ -5,19 +5,71 @@ - + - + - -