package ProjectElectricLocomotive; import javax.swing.*; import javax.swing.event.ListSelectionEvent; import java.awt.*; //готовая лаба 3 public class FormLocomotiveCollections { FrameDopClassParameters frameDopClassParameters; private JPanel MainPanel; private JPanel pictureBoxCollections; private JPanel Instruments; private JButton ButtonAddLocomotive; private JTextField textFieldNumber; private JButton ButtonRefreshCollection; private JButton ButtonRemoveLocomotive; private JButton ButtonCreateRandomLoco; private JTextField textBoxStorageName; private JButton ButtonAddObject; private JList listBoxStorage; 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 -> { loco = frameElectricLocomotive._formLocomotiveCollection._drawingLocomotive; frameElectricLocomotive.dispose(); if (loco != null) { //проверяем, удалось ли нам загрузить объект if (obj.AddOverload(loco)!= -1) { JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен"); Refresh(); } else { JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект"); } } }); }); 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 { 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(), "Объект удален", "Успех", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Не удалось удалить объект", "Ошибка", JOptionPane.ERROR_MESSAGE); } } catch (Exception ex) { JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Неверное значение", "Ошибка", JOptionPane.ERROR_MESSAGE); } }); ButtonRefreshCollection.addActionListener(e -> { Refresh(); }); } 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); } }