diff --git a/src/RoadTrain/FormTruckCollection.java b/src/RoadTrain/FormTruckCollection.java index b6b520b..0ea0df7 100644 --- a/src/RoadTrain/FormTruckCollection.java +++ b/src/RoadTrain/FormTruckCollection.java @@ -5,19 +5,30 @@ import RoadTrain.Generics.*; import RoadTrain.DrawingObjects.*; import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.*; public class FormTruckCollection { private class Canvas extends JComponent { - public TrucksGenericCollection _truck; public Canvas() { } public void paintComponent (Graphics g) { super.paintComponent(g); - if (_truck.ShowTrucks() != null) { - g.drawImage(_truck.ShowTrucks(), 0, 10, this); + if (jListStorage.getSelectedIndex() == -1) + { + return; + } + var obj = _storage.get(jListStorage.getSelectedValue()); + if (obj == null) + { + return; + } + if (obj.ShowTrucks() != null) { + g.drawImage(obj.ShowTrucks(), 0, 0, this); } super.repaint(); } @@ -25,26 +36,55 @@ public class FormTruckCollection { Canvas canv; static int pictureBoxWidth = 700; static int pictureBoxHeight = 480; - private TrucksGenericCollection _truck; + private TrucksGenericStorage _storage; public void Draw(){ canv.repaint(); } + private Stack StackRemoved; + private JList jListStorage; + private DefaultListModel listModel; + private void ReloadObjects() + { + int index = jListStorage.getSelectedIndex(); + listModel.clear(); + for (String key : _storage.Keys()) { + listModel.addElement(key); + } + if (listModel.size() > 0 && (index == -1 || index >= listModel.size())) + { + jListStorage.setSelectedIndex(0); + } + else if (listModel.size() > 0 && index > -1 && index < listModel.size()) + { + jListStorage.setSelectedIndex(index); + } + } FormTruckCollection() { + listModel = new DefaultListModel(); + jListStorage = new JList(listModel); canv = new Canvas(); - JFrame Frame = new JFrame ("TrucksCollecltion"); - _truck = new TrucksGenericCollection(pictureBoxWidth, pictureBoxHeight); - canv._truck = _truck; + JFrame Frame = new JFrame ("TrucksCollection"); + _storage = new TrucksGenericStorage(pictureBoxWidth, pictureBoxHeight); JButton ButtonAddTruck = new JButton("Добавить технику"); ButtonAddTruck.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { + if (jListStorage.getSelectedIndex() == -1) + { + return; + } + var obj = _storage.get(jListStorage.getSelectedValue()); + if (obj == null) + { + return; + } RoadTrainForm form = new RoadTrainForm(); form.buttonSelectTruck.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { - if (_truck.Add(form._drawingTruck) != -1) { + if (obj.Add(form._drawingTruck) != -1) { JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); Draw(); } else { @@ -57,12 +97,22 @@ public class FormTruckCollection { } } ); + StackRemoved = new Stack(); JTextField TextBoxNumber = new JTextField(); JButton ButtonRemoveTruck = new JButton("Удалить технику"); ButtonRemoveTruck.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { + if (jListStorage.getSelectedIndex() == -1) + { + return; + } + var obj = _storage.get(jListStorage.getSelectedValue()); + if (obj == null) + { + return; + } if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { return; } @@ -75,9 +125,11 @@ public class FormTruckCollection { JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); return; } - int pos = Integer.parseInt(TextBoxNumber.getText()); - if (_truck.remove(pos) != null) { + var removed = obj.remove(pos); + if (removed != null) + { + StackRemoved.push(removed); JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); Draw(); } else { @@ -87,6 +139,20 @@ public class FormTruckCollection { } ); + JButton buttonGetRemoved = new JButton("Вернуть удаленные"); + buttonGetRemoved.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (StackRemoved.size()==0){ + JOptionPane.showMessageDialog(null, "Нет удалённых", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + RoadTrainForm form = new RoadTrainForm(); + form._drawingTruck = StackRemoved.pop(); + form.Draw(); + } + } + ); JButton ButtonRefreshCollection = new JButton("Обновить коллекцию"); ButtonRefreshCollection.addActionListener( new ActionListener() { @@ -104,22 +170,76 @@ public class FormTruckCollection { } } ); + JTextField textBoxSetName = new JTextField(); + JButton buttonAddSet = new JButton("Добавить набор"); + buttonAddSet.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (textBoxSetName.getText().length() == 0) + { + JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Не все данные заполнены"); + return; + } + _storage.AddSet(textBoxSetName.getText()); + ReloadObjects(); + } + } + ); + jListStorage.addListSelectionListener( + new ListSelectionListener() { + public void valueChanged(ListSelectionEvent e){ + Draw(); + } + } + ); + + JButton buttonRemoveSet = new JButton("Убрать набор"); + buttonRemoveSet.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (jListStorage.getSelectedIndex() == -1) + { + return; + } + if (JOptionPane.showConfirmDialog(null, "Удалить объект " + jListStorage.getSelectedValue() + "?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) + { + return; + } + _storage.DelSet(jListStorage.getSelectedValue()); + ReloadObjects(); + } + } + ); Frame.setSize (880, 520); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Frame.setLayout(null); canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); - ButtonAddTruck.setBounds(pictureBoxWidth - 50, 10, 170, 30); - TextBoxNumber.setBounds(pictureBoxWidth - 50, 50, 170, 20); - ButtonRemoveTruck.setBounds(pictureBoxWidth - 50, 80, 170, 30); - ButtonRefreshCollection.setBounds(pictureBoxWidth - 50, 120, 170, 30); - FormTruckGenerate.setBounds(pictureBoxWidth - 50, 160, 170, 30); + ButtonAddTruck.setBounds(pictureBoxWidth, 0, 160, 20); + TextBoxNumber.setBounds(pictureBoxWidth, 30, 160, 20); + ButtonRemoveTruck.setBounds(pictureBoxWidth, 60, 160, 20); + ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 160, 20); + FormTruckGenerate.setBounds(pictureBoxWidth, 120, 160, 20); + + buttonAddSet.setBounds(pictureBoxWidth, 150, 160, 20); + textBoxSetName.setBounds(pictureBoxWidth, 180, 160, 20); + jListStorage.setBounds(pictureBoxWidth, 210, 160, 80); + buttonRemoveSet.setBounds(pictureBoxWidth, 300, 160, 20); + + buttonGetRemoved.setBounds(pictureBoxWidth, 330, 160, 20); Frame.add(canv); Frame.add(ButtonAddTruck); Frame.add(ButtonRemoveTruck); Frame.add(ButtonRefreshCollection); Frame.add(TextBoxNumber); Frame.add(FormTruckGenerate); + Frame.add(buttonAddSet); + Frame.add(textBoxSetName); + Frame.add(jListStorage); + Frame.add(buttonRemoveSet); + + Frame.add(buttonGetRemoved); Frame.setVisible(true); } } diff --git a/src/RoadTrain/Generics/SetGeneric.java b/src/RoadTrain/Generics/SetGeneric.java index 0e18662..62ca44f 100644 --- a/src/RoadTrain/Generics/SetGeneric.java +++ b/src/RoadTrain/Generics/SetGeneric.java @@ -1,63 +1,77 @@ package RoadTrain.Generics; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.NoSuchElementException; public class SetGeneric { - // Массив объектов, которые храним - private Object[] _places; - - // Количество объектов в массиве - public int Count; - - // Конструктор + private ArrayList _places; + public int Count() { + return _places.size(); + } + private int _maxCount; public SetGeneric(int count) { - _places = new Object[count]; - Count = _places.length; + _maxCount = count; + _places = new ArrayList(count); } - - // Добавление объекта в набор - public int Insert(T truck) { - int i = 0; - for (; i < _places.length; i++) { - if (_places[i] == null) - break; - } - if (i == _places.length) + public int Insert(T train) { + if (_places.size() >= _maxCount) return -1; - for (; i > 0; i--) { - _places[i] = _places[i - 1]; - } - _places[i] = truck; - return i; + _places.add(0, train); + return 0; } + public boolean Insert(T train, int position) { + if (_places.size() >= _maxCount) + return false; - // Добавление объекта в набор на конкретную позицию - public boolean Insert(T truck, int position) { - if (position < 0 || position >= _places.length) + if (position < 0 || position > _places.size()) return false; - for (; position < _places.length; position++) { - if (_places[position] == null) - break; - } - if (position == _places.length) - return false; - for (; position > 0; position--) { - _places[position] = _places[position - 1]; - } - _places[position] = truck; + + if (position == _places.size()) + _places.add(train); + else + _places.add(position, train); + return true; } - - // Удаление объекта из набора с конкретной позиции public boolean Remove(int position) { - if (position < 0 || position >= _places.length) + if (position < 0 || position >= _places.size()) return false; - _places[position] = null; + _places.remove(position); return true; } - - // Получение объекта из набора по позиции public T Get(int position) { - if (position < 0 || position >= _places.length) + if (position < 0 || position >= _places.size()) return null; - return (T) _places[position]; + return _places.get(position); + } + public Iterable GetTrucks(final Integer maxTrains) { + return new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + private int currentIndex = 0; + private int count = 0; + + @Override + public boolean hasNext() { + return currentIndex < _places.size() && (maxTrains == null || count < maxTrains); + } + + @Override + public T next() { + if (hasNext()) { + count++; + return _places.get(currentIndex++); + } + throw new NoSuchElementException(); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + }; } } diff --git a/src/RoadTrain/Generics/TrucksGenericCollection.java b/src/RoadTrain/Generics/TrucksGenericCollection.java index 86776fd..b2f9c49 100644 --- a/src/RoadTrain/Generics/TrucksGenericCollection.java +++ b/src/RoadTrain/Generics/TrucksGenericCollection.java @@ -71,8 +71,9 @@ public class TrucksGenericCollection> _truckStorages; + public List Keys(){return _truckStorages.keySet().stream().collect(Collectors.toList());} + private int _pictureWidth; + private int _pictureHeight; + public TrucksGenericStorage(int pictureWidth, int pictureHeight) + { + _truckStorages = new HashMap>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddSet(String name) + { + if (_truckStorages.containsKey(name)) + return; + _truckStorages.put(name, new TrucksGenericCollection(_pictureWidth, _pictureHeight)); + } + public void DelSet(String name) + { + if (!_truckStorages.containsKey(name)) + return; + _truckStorages.remove(name); + } + public TrucksGenericCollection get(String ind) + { + if (_truckStorages.containsKey(ind)) + return _truckStorages.get(ind); + return null; + } + + public DrawingObjectTruck get(String ind1, int ind2){ + if (!_truckStorages.containsKey(ind1)) + return null; + return _truckStorages.get(ind1).GetU(ind2); + } +} diff --git a/src/RoadTrain/RoadTrainForm.java b/src/RoadTrain/RoadTrainForm.java index 270830c..7ad40eb 100644 --- a/src/RoadTrain/RoadTrainForm.java +++ b/src/RoadTrain/RoadTrainForm.java @@ -70,7 +70,6 @@ public class RoadTrainForm{ random.nextInt(2000) + 1000, color, this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3)); _drawingTruck.SetPosition(random.nextInt(100), random.nextInt(90)); - canv._drawingTruck = _drawingTruck; Draw(); } ); @@ -94,7 +93,6 @@ public class RoadTrainForm{ color, color2, random.nextBoolean(), random.nextBoolean(), this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3)); _drawingTruck.SetPosition(random.nextInt(100), random.nextInt(90)); - canv._drawingTruck = _drawingTruck; Draw(); } ); @@ -152,7 +150,6 @@ public class RoadTrainForm{ } }); frame.setSize(910, 500); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(null); canv = new Canvas(); canv.setBounds(0, 0, 900, 500); @@ -178,7 +175,6 @@ public class RoadTrainForm{ frame.setVisible(true); } class Canvas extends JComponent{ - public DrawingTruck _drawingTruck; public Canvas(){} public void paintComponent(Graphics g){