228 lines
9.2 KiB
Java
Raw Normal View History

package ProjectElectricLocomotive;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import java.awt.*;
import java.util.Stack;
2023-11-06 20:57:19 +04:00
//готовая лаба 3
2023-10-21 22:32:22 +04:00
public class FormLocomotiveCollections {
private JPanel MainPanel;
private JPanel pictureBoxCollections;
private JPanel Instruments;
private JButton ButtonAddLocomotive;
2023-10-21 22:32:22 +04:00
private JTextField textFieldNumber;
private JButton ButtonRefreshCollection;
private JButton ButtonRemoveLocomotive;
2023-11-06 20:57:19 +04:00
private JButton ButtonCreateRandomLoco;
2023-11-07 00:52:46 +04:00
private JTextField textBoxStorageName;
private JButton ButtonAddObject;
private JList listBoxStorage;
private JButton ButtonRemoveObject;
private JButton Button_OpenFormRemovedLocomotives;
public DrawingLocomotive loco;
final LocomotivesGenericStorage _storage;
FrameElectricLocomotive _frameRemovedLocomotives;
2023-10-21 22:32:22 +04:00
private Stack<DrawingLocomotive> stackRemoveObjects;
FrameDopClassParameters frameDopClassParameters;
// LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> _locomotives;
public JPanel getPictureBoxCollections() {
return MainPanel;
}
2023-10-21 22:32:22 +04:00
2023-10-21 22:32:22 +04:00
public FormLocomotiveCollections() {
//_locomotives = new LocomotiveGenericCollection<>(400, 300);
_storage = new LocomotivesGenericStorage(/*pictureBoxCollections.getWidth(), pictureBoxCollections.getHeight()*/400,300);
stackRemoveObjects = new Stack<>();
listBoxStorage.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged);
2023-10-21 22:32:22 +04:00
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();
});
2023-10-21 22:32:22 +04:00
ButtonAddLocomotive.addActionListener(e -> {
if (listBoxStorage.getSelectedIndex() == -1)
return;
var obj = _storage.get(listBoxStorage.getSelectedValue().toString()); // ТУТ ЕЩЕ РАЗ - ОБРАТИ ВНИМАНИЕ НА СТРИНГ
if (obj == null) {
return;
}
2023-10-21 22:32:22 +04:00
FrameElectricLocomotive frameElectricLocomotive = new FrameElectricLocomotive();
frameElectricLocomotive.setVisible(true);
frameElectricLocomotive._formElectricLocomotive.ButtonSelectLocomotive.addActionListener(e2 -> {
loco = frameElectricLocomotive._formElectricLocomotive._drawingLocomotive;
2023-10-21 22:32:22 +04:00
frameElectricLocomotive.dispose();
if (loco != null) {
//проверяем, удалось ли нам загрузить объект
if (obj.AddOverload(loco)!= -1) {
2023-10-21 22:32:22 +04:00
JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен");
Refresh();
} else {
JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект");
}
}
});
});
ButtonCreateRandomLoco.addActionListener(e -> {
if (frameDopClassParameters != null) frameDopClassParameters.dispose();
2023-11-06 20:57:19 +04:00
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 = 0;
try {
pos = Integer.parseInt(textFieldNumber.getText());
}
catch (NumberFormatException ex)
{
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
"Неверное значение",
"Ошибка",
JOptionPane.ERROR_MESSAGE);
}
DrawingLocomotive deletedLoco = obj.SubOverload(pos);
if (deletedLoco != null) {
// logic for push deleted loco in stack
stackRemoveObjects.push(deletedLoco);
Refresh();
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
"Объект удален",
"Успех",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
"Не удалось удалить объект",
"Ошибка",
JOptionPane.ERROR_MESSAGE);
}*/
if (listBoxStorage.getSelectedIndex() == -1) {
return;
}
var obj = _storage.get(listBoxStorage.getSelectedValue().toString());
if (obj == null) {
return;
}
int pos;
2023-10-21 22:32:22 +04:00
try {
pos = Integer.parseInt(textFieldNumber.getText());
} catch (NumberFormatException ex) {
2023-10-21 22:32:22 +04:00
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);
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
"Объект удален",
"Успех",
JOptionPane.INFORMATION_MESSAGE);Refresh();
} else {
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
"Не удалось удалить объект",
"Ошибка",
JOptionPane.ERROR_MESSAGE);
}
});
2023-11-06 20:57:19 +04:00
ButtonRefreshCollection.addActionListener(e -> {
2023-10-21 22:32:22 +04:00
Refresh();
});
2023-11-06 20:57:19 +04:00
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();
});
2023-10-21 22:32:22 +04:00
}
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();
}
2023-10-21 22:32:22 +04:00
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);
}
}