From 93d391fad32c51b330fe7c48a394b2a211b3c7ee Mon Sep 17 00:00:00 2001 From: ekallin Date: Tue, 7 Nov 2023 20:36:52 +0400 Subject: [PATCH] some modifications , but locos doesn't show --- .../FormLocomotiveCollections.form | 6 +- .../FormLocomotiveCollections.java | 74 +++++++++++++++++-- .../LocomotiveGenericCollection.java | 17 +++-- .../LocomotivesGenericStorage.java | 71 ++++++++++++++++++ ProjectElectricLocomotive/SetGeneric.java | 28 +++++-- 5 files changed, 174 insertions(+), 22 deletions(-) create mode 100644 ProjectElectricLocomotive/LocomotivesGenericStorage.java diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.form b/ProjectElectricLocomotive/FormLocomotiveCollections.form index ac1e08b..70fb649 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.form +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.form @@ -66,7 +66,9 @@ - + + + @@ -110,7 +112,7 @@ - + diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java index ebabad0..5b0e831 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.java +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -1,6 +1,7 @@ package ProjectElectricLocomotive; import javax.swing.*; +import javax.swing.event.ListSelectionEvent; import java.awt.*; //готовая лаба 3 @@ -18,19 +19,53 @@ public class FormLocomotiveCollections { private JTextField textBoxStorageName; private JButton ButtonAddObject; private JList listBoxStorage; - private JButton ButtomRemoveObject; + private JButton ButtonRemoveObject; public DrawingLocomotive loco; + private DefaultListModel listModel; + final LocomotivesGenericStorage _storage; LocomotiveGenericCollection _locomotives; public JPanel getPictureBoxCollections() { return MainPanel; } + public FormLocomotiveCollections() { _locomotives = new LocomotiveGenericCollection<>(400, 300); + _storage = new LocomotivesGenericStorage(pictureBoxCollections.getWidth(), pictureBoxCollections.getHeight()); + ButtonAddObject.addActionListener(e -> + { + String NameStorage = textBoxStorageName.getText(); + if (NameStorage.equals("")) { + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Где данные? Напиши хоть что-нибудь", + "Ошибка", + JOptionPane.INFORMATION_MESSAGE); + return; + } + _storage.AddSet(NameStorage); + ReloadObjects(); + Refresh(); + }); + + ButtonRemoveObject.addActionListener(e -> + { + if (listBoxStorage.getSelectedIndex() == -1) { + return; + } + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Коллекция удалена", "Удаление", JOptionPane.INFORMATION_MESSAGE); + _storage.DelSet((String) listBoxStorage.getSelectedValue()); //ТУТ СТРИНГ ОБРАТИ ВНИМАНИЕ КАК-НИБУДЬ + ReloadObjects(); + }); ButtonAddLocomotive.addActionListener(e -> { + if (listBoxStorage.getSelectedIndex() == -1) + return; + var obj = _storage.get(listBoxStorage.getSelectedValue().toString()); // ТУТ ЕЩЕ РАЗ - ОБРАТИ ВНИМАНИЕ НА СТРИНГ + if (obj == null) { + return; + } FrameElectricLocomotive frameElectricLocomotive = new FrameElectricLocomotive(); frameElectricLocomotive.setVisible(true); frameElectricLocomotive._formLocomotiveCollection.ButtonSelectLocomotive.addActionListener(e2 -> { @@ -38,7 +73,7 @@ public class FormLocomotiveCollections { frameElectricLocomotive.dispose(); if (loco != null) { //проверяем, удалось ли нам загрузить объект - if (_locomotives.AddOverload(loco) != -1) { + if (obj.AddOverload(loco)!= -1) { JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен"); Refresh(); } else { @@ -48,16 +83,26 @@ public class FormLocomotiveCollections { }); }); - ButtonCreateRandomLoco.addActionListener(e->{ - if(frameDopClassParameters!=null) frameDopClassParameters.dispose(); + ButtonCreateRandomLoco.addActionListener(e -> { + if (frameDopClassParameters != null) frameDopClassParameters.dispose(); frameDopClassParameters = new FrameDopClassParameters(); frameDopClassParameters.setVisible(true); }); ButtonRemoveLocomotive.addActionListener(e -> { + if (listBoxStorage.getSelectedIndex() == -1) { + return; + } + var obj = _storage.get(listBoxStorage.getSelectedValue().toString()); + if (obj == null) { + return; + } + int pos; try { - int pos = Integer.parseInt(textFieldNumber.getText()); + pos = Integer.parseInt(textFieldNumber.getText()); + DrawingLocomotive deletedLoco = obj.SubOverload(pos); if (_locomotives.SubOverload(pos) != null) { + // logic for push deleted loco in stack Refresh(); JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Объект удален", @@ -82,7 +127,26 @@ public class FormLocomotiveCollections { }); } + + private void ReloadObjects() { + int index = listBoxStorage.getSelectedIndex(); + listBoxStorage.setListData(_storage.Keys().toArray()); + if (listBoxStorage.getModel().getSize() > 0 && (index == -1 || index >= listBoxStorage.getModel().getSize())) { + listBoxStorage.setSelectedIndex(0); + } else if (listBoxStorage.getModel().getSize() > 0 && index > -1 && index < listBoxStorage.getModel().getSize()) { + listBoxStorage.setSelectedIndex(index); + } + listBoxStorage.invalidate(); + } + public void Refresh() { + if (listBoxStorage.getSelectedIndex() == -1) { + return; + } + var obj = _storage.get(listBoxStorage.getSelectedValue().toString()); + if (obj == null) { + return; + } Graphics g = pictureBoxCollections.getGraphics(); pictureBoxCollections.paint(g); _locomotives.ShowLocomotives(g); diff --git a/ProjectElectricLocomotive/LocomotiveGenericCollection.java b/ProjectElectricLocomotive/LocomotiveGenericCollection.java index 0aeddea..7b3b154 100644 --- a/ProjectElectricLocomotive/LocomotiveGenericCollection.java +++ b/ProjectElectricLocomotive/LocomotiveGenericCollection.java @@ -26,11 +26,11 @@ public class LocomotiveGenericCollection> _locomotiveStorage; + public List Keys() + { + return _locomotiveStorage.keySet().stream().toList(); + } + private final int _pictureWidth; + private final int _pictureHeight; + public LocomotivesGenericStorage(int pictureWidth, int pictureHeight) { + _locomotiveStorage = new HashMap<>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(String name) + { + if (!(_locomotiveStorage.containsKey(name))) + { + _locomotiveStorage.put(name, new LocomotiveGenericCollection(_pictureWidth, _pictureHeight)); + } + } + + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(String name) + { + if (_locomotiveStorage.keySet().contains(name)) + { + _locomotiveStorage.remove(name); + } + } + + + /// + /// Доступ к набору + /// + /// + /// + public LocomotiveGenericCollection get(String ind) + { + // TODO Продумать логику получения набора + if (_locomotiveStorage.keySet().contains(ind)) + { + return _locomotiveStorage.get(ind); + } + return null; + } + + public DrawingObjectLocomotive get(String name, int ind) + { + if (_locomotiveStorage.keySet().contains(ind)) + { + return _locomotiveStorage.get(name).GetU(ind); + } + return null; + } +} diff --git a/ProjectElectricLocomotive/SetGeneric.java b/ProjectElectricLocomotive/SetGeneric.java index ba12886..53b4476 100644 --- a/ProjectElectricLocomotive/SetGeneric.java +++ b/ProjectElectricLocomotive/SetGeneric.java @@ -6,12 +6,14 @@ import java.util.NoSuchElementException; public class SetGeneric{ private ArrayList _places; + public int Count() { return _places.size(); } public int maxCount; + public SetGeneric(int count) { maxCount = count; @@ -25,11 +27,8 @@ public class SetGeneric{ public int Insert(T loco, int position) { - int NoEmpty = 0, temp = 0; if(position < 0 || position > Count()) return -1; - if(Count() > maxCount) - return -1; else { _places.add(position, loco); @@ -43,9 +42,11 @@ public class SetGeneric{ return null; else { + T plane = _places.get(position); _places.remove(position); + _places.set(position, null); + return plane; } - return _places.get(position); } public T Get(int position) @@ -54,7 +55,21 @@ public class SetGeneric{ return _places.get(position); } - public Iterable GetLocomotives(final Integer maxLocomotives) { + public void Set(int position, T loco) { + // Проверка позиции + // Проверка свободных мест в списке + if (position < 0 || position >= maxCount || Count() == maxCount) { + return; + } + // Вставка в список по позиции + _places.set(position, loco); + } + + public ArrayList GetEnumerator() { + return _places; + } + +/* public Iterable GetLocomotives(final Integer maxLocomotives) { return new Iterable() { @Override public Iterator iterator() { @@ -84,6 +99,5 @@ public class SetGeneric{ } }; } - }; - } + };*/ } \ No newline at end of file