package ProjectElectricLocomotive; import javax.swing.*; import javax.swing.event.ListSelectionEvent; import java.awt.*; import java.util.Stack; //готовая лаба 3 public class FormLocomotiveCollections { FormElectricLocomotive formElectricLocomotive; FormLocomotiveConfig formLocomotiveConfig; 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; private JButton Button_OpenFormRemovedLocomotives; public DrawingLocomotive loco; final LocomotivesGenericStorage _storage; FrameElectricLocomotive _frameRemovedLocomotives; private Stack _stackRemoveObjects; FrameDopClassParameters frameDopClassParameters; public JPanel getPictureBoxCollections() { return MainPanel; } public FormLocomotiveCollections() { formElectricLocomotive = new FormElectricLocomotive(); formLocomotiveConfig = new FormLocomotiveConfig(); _storage = new LocomotivesGenericStorage(400,300); _stackRemoveObjects = new Stack<>(); listBoxStorage.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged); 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; } FrameLocomotiveConfig frameLocomotiveConfig = new FrameLocomotiveConfig(); frameLocomotiveConfig.setVisible(true); frameLocomotiveConfig._formLocomotiveConfig.buttonOk.addActionListener(e2 ->{ frameLocomotiveConfig.dispose(); //loco = frameLocomotiveConfig._formLocomotiveConfig. }); // FrameElectricLocomotive frameElectricLocomotive = new FrameElectricLocomotive(); // frameElectricLocomotive.setVisible(true); // frameElectricLocomotive._formElectricLocomotive.ButtonSelectLocomotive.addActionListener(e2 -> { // loco = frameElectricLocomotive._formElectricLocomotive._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()); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Неверное значение", "Ошибка", JOptionPane.ERROR_MESSAGE); return; } Object[] options = {"Да", "Нет"}; int n = JOptionPane.showOptionDialog(this.getPictureBoxCollections(), "Удалить объект?", "Все серьезно", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0] ); if (n == 1) { return; } DrawingLocomotive removedPlane = obj.SubOverload(pos); if (removedPlane != null) { _stackRemoveObjects.push(removedPlane); Refresh(); JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Объект удален", "Успех", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Не удалось удалить объект", "Ошибка", JOptionPane.ERROR_MESSAGE); } }); ButtonRefreshCollection.addActionListener(e -> { Refresh(); }); Button_OpenFormRemovedLocomotives.addActionListener(e -> { if(_stackRemoveObjects.empty()) { JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Вы ничего не удалили, кажется...", "ой", JOptionPane.INFORMATION_MESSAGE); return; } _frameRemovedLocomotives = new FrameElectricLocomotive(); _frameRemovedLocomotives._formElectricLocomotive._drawingLocomotive = _stackRemoveObjects.pop(); _frameRemovedLocomotives.setVisible(true); _frameRemovedLocomotives._formElectricLocomotive.Draw(); }); } 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(); } private void listBoxObjectsSelectedIndexChanged(ListSelectionEvent e) { Refresh(); } 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); obj.ShowLocomotives(g); } }