diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java index 3dc9d8f..a811492 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java @@ -143,7 +143,7 @@ public class ControllerBus { DrawingObjectBus bus = new DrawingObjectBus(SelectedBus); - if (_mapBusesCollectionGeneric.add(bus) != -1) { + if (ControllerMapWithSetBus.AddNewBus(bus) != -1) { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("SelectBus"); alert.setContentText("Вы создали объект"); diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java index 9562343..05fed9f 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java @@ -1,4 +1,6 @@ package com.example.doubledeckerbus; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; @@ -12,13 +14,27 @@ import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; import java.io.IOException; +import java.util.HashMap; import java.util.Objects; import java.util.Optional; public class ControllerMapWithSetBus { static MapWithSetBusesGeneric _mapBusesCollectionGeneric; + private HashMap _mapsDict = new HashMap<>(); + { + _mapsDict.put("Простая карта", new SimpleMap()); + _mapsDict.put("Водная карта", new WaterMap()); + } AbstractMap map = new SimpleMap(); + + public static int AddNewBus (DrawingObjectBus bus) { + return _mapsCollection.GetId(selected).add(bus); + } + static public MapsCollection _mapsCollection; + + static String selected; + static String map_name = "Простая карта"; protected ObservableList countOfMap = FXCollections.observableArrayList("Простая карта", "Водная карта"); @@ -58,18 +74,32 @@ public class ControllerMapWithSetBus { @FXML private TextField textBoxPosition; + @FXML + private ListView listViewMaps; + + @FXML + private TextField TextFieldMap; + @FXML private void initialize(){ +// canvasBus.setWidth(pictureBoxBus.getWidth()); +// canvasBus.setWidth(pictureBoxBus.getHeight()); + if (_mapsCollection == null) + _mapsCollection = new MapsCollection((int) canvasBus.getWidth(), (int) canvasBus.getHeight()); comboBoxSelectorMap.setItems(countOfMap); comboBoxSelectorMap.setValue(map_name); + listViewMaps.getSelectionModel().selectedItemProperty() + .addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observableValue, String s, String t1) { + selected = t1; + } + }); + listViewMaps.setItems(_mapsCollection.toObserveList()); } GraphicsContext gc; private void FirstIncome() { - if (_mapBusesCollectionGeneric == null) { - _mapBusesCollectionGeneric = new MapWithSetBusesGeneric( - (int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight(), map); - } gc = canvasBus.getGraphicsContext2D(); if (comboBoxSelectorMap.getValue() != map_name) { map_name = comboBoxSelectorMap.getValue(); @@ -77,9 +107,31 @@ public class ControllerMapWithSetBus { case "Простая карта" -> map = new SimpleMap(); case "Водная карта" -> map = new WaterMap(); } - _mapBusesCollectionGeneric.changeMap(map); } } + + private void ReloadMaps() + { + int index = listViewMaps.getSelectionModel().getSelectedIndex(); + + ObservableList listMaps = FXCollections.observableArrayList(); + + for (int i = 0; i < _mapsCollection.Keys().size(); i++) + { + listMaps.add(_mapsCollection.Keys().get(i)); + } + listViewMaps.setItems(listMaps); + + if (listMaps.size() > 0 && (index == -1 || index >= listMaps.size())) + { + listViewMaps.getSelectionModel().select(0); + } + else if (listMaps.size() > 0 && index > -1 && index < listMaps.size()) + { + listViewMaps.getSelectionModel().select(index); + } + } + @FXML private void ButtonAddBus_Click(ActionEvent event) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormBus.fxml")); @@ -90,6 +142,39 @@ public class ControllerMapWithSetBus { FirstIncome(); } + @FXML + private void ButtonAddMap_Click(ActionEvent event) + { + if ((Objects.equals(TextFieldMap.getText(), ""))) + { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("SelectMap"); + alert.setContentText("Не все данные заполнены"); + Optional option = alert.showAndWait(); + return; + } + if (!_mapsDict.containsKey(comboBoxSelectorMap.getValue())) + { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("SelectMap"); + alert.setContentText("Нет такой карты"); + Optional option = alert.showAndWait(); + return; + } + _mapsCollection.AddMap(TextFieldMap.getText(), _mapsDict.get(comboBoxSelectorMap.getValue())); + ReloadMaps(); + } + @FXML + private void ButtonDeleteMap_Click(ActionEvent event) + { + if (listViewMaps.getSelectionModel().selectedItemProperty().isNull().get()) + { + return; + } + _mapsCollection.DelMap(listViewMaps.getSelectionModel().getSelectedItem()); + ReloadMaps(); + + } @FXML private void ButtonRemoveBus_Click(ActionEvent event) @@ -116,7 +201,7 @@ public class ControllerMapWithSetBus { return; } - if (_mapBusesCollectionGeneric.remove(pos) != null) + if (_mapsCollection.GetId(selected).remove(pos) != null) { alert = new Alert(Alert.AlertType.WARNING); alert.setTitle("RemoveBus"); @@ -136,30 +221,30 @@ public class ControllerMapWithSetBus { private void ButtonShowStorage_Click(ActionEvent event) { FirstIncome(); - if (_mapBusesCollectionGeneric == null) + if (selected == null) { return; } gc.setFill(Color.WHITE); gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight()); - _mapBusesCollectionGeneric.ShowSet(gc); + _mapsCollection.GetId(selected).ShowSet(gc); } @FXML private void ButtonShowOnMap_Click(ActionEvent event) { FirstIncome(); - if (_mapBusesCollectionGeneric == null) { + if (selected == null) { return; } gc.setFill(Color.WHITE); - gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight()); - _mapBusesCollectionGeneric.ShowOnMap(gc); + gc.fillRect(0, 0, canvasBus.getWidth(), canvasBus.getHeight()); + _mapsCollection.GetId(selected).ShowOnMap(gc); } @FXML private void ButtonMove_Click(ActionEvent event) { - if (_mapBusesCollectionGeneric == null) + if (selected == null) { return; } @@ -172,8 +257,11 @@ public class ControllerMapWithSetBus { case "buttonLeft" -> dir = Direction.Left; case "buttonRight" -> dir = Direction.Right; } - _mapBusesCollectionGeneric.MoveObject(dir); + _mapsCollection.GetId(selected).MoveObject(dir); } + + + } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java index 7372e67..eb0b4d1 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java @@ -12,7 +12,7 @@ public class Form extends Application { @Override public void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormPolymorph.fxml")); + FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormMapWithSetBus.fxml")); Scene scene = new Scene(fxmlLoader.load()); myStage = stage; myStage.setTitle("DoubleDeckerBus"); diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java index a94d632..3215a9d 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java @@ -46,9 +46,8 @@ public class MapWithSetBusesGeneric 0) currentWidth--; diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapsCollection.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapsCollection.java new file mode 100644 index 0000000..b7cf218 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapsCollection.java @@ -0,0 +1,49 @@ +package com.example.doubledeckerbus; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; + +import java.util.HashMap; +import java.util.List; + +public class MapsCollection { + HashMap> _mapStorages; + + public List Keys() { + return _mapStorages.keySet().stream().toList(); + } + + private int _pictureWidth; + + private int _pictureHeight; + + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new HashMap<>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddMap(String name, AbstractMap map) + { + if (Keys().contains(name)) return; + _mapStorages.put(name, new MapWithSetBusesGeneric(_pictureWidth, _pictureHeight, map)); + } + + public void DelMap(String name) + { + _mapStorages.remove(name); + } + + public MapWithSetBusesGeneric GetId(String id) + { + return _mapStorages.get(id); + } + + public ObservableList toObserveList() { + ObservableList result = FXCollections.observableArrayList(); + result.addAll(_mapStorages.keySet()); + return result; + } + +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java index c6e3c6c..b8f1ccb 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java @@ -1,15 +1,20 @@ package com.example.doubledeckerbus; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Objects; class SetBusesGeneric { - private Object[] _places; + private ArrayList _places; private int BusyPlaces = 0; + private int _maxCount; + public SetBusesGeneric(int count) { - _places = new Object[count]; + _maxCount = count; + _places = new ArrayList<>(); } public int Insert(T bus) @@ -19,37 +24,42 @@ class SetBusesGeneric { public int Insert(T bus, int position) { - if (position < 0 || position >= _places.length|| BusyPlaces == _places.length) return -1; + if (position < 0 || position >= _maxCount || BusyPlaces == _maxCount) return -1; BusyPlaces++; - while (_places[position] != null) { - for (int i = _places.length - 1; i > 0; --i) { - if (_places[i] == null && _places[i - 1] != null) { - _places[i] = _places[i - 1]; - _places[i - 1] = null; - } - } - } - _places[position] = bus; + _places.add(position, bus); return position; } public T Remove(int position) { - if (position < 0 || position >= _places.length) return null; - T savedBus = (T)_places[position]; - _places[position] = null; + if (position < 0 || position >= _maxCount) return null; + T savedBus = _places.get(position - 1); + _places.set(position - 1, null); return savedBus; } public T Get(int position) { - if (position < 0 || position >= _places.length) return null; - return (T)_places[position]; + if (position < 0 || position >= _maxCount) return null; + return _places.get(position); } + + public ArrayList GetBuses() { + ArrayList result = new ArrayList<>(); + for (var bus: _places) { + if (bus != null){ + result.add(bus); + } + else { + break; + } + } + return result; + } public int Count() { - return _places.length; + return _places.size(); } } diff --git a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml index d14d90e..8a028c7 100644 --- a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml +++ b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormMapWithSetBus.fxml @@ -4,6 +4,7 @@ + @@ -12,7 +13,7 @@ - + @@ -23,7 +24,7 @@ - + @@ -34,6 +35,8 @@ + + @@ -43,12 +46,12 @@ - - -