2023-10-21 19:25:05 +04:00
|
|
|
|
package ProjectElectricLocomotive;
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
2023-11-07 20:36:52 +04:00
|
|
|
|
import javax.swing.event.ListSelectionEvent;
|
2023-10-21 19:25:05 +04:00
|
|
|
|
import java.awt.*;
|
2023-11-08 00:17:28 +04:00
|
|
|
|
import java.util.Stack;
|
2023-10-21 19:25:05 +04:00
|
|
|
|
|
2023-11-06 20:57:19 +04:00
|
|
|
|
//готовая лаба 3
|
|
|
|
|
|
2023-10-21 22:32:22 +04:00
|
|
|
|
public class FormLocomotiveCollections {
|
2023-11-18 23:58:18 +04:00
|
|
|
|
FormElectricLocomotive formElectricLocomotive;
|
2023-10-21 19:25:05 +04:00
|
|
|
|
private JPanel MainPanel;
|
|
|
|
|
private JPanel pictureBoxCollections;
|
|
|
|
|
private JPanel Instruments;
|
|
|
|
|
private JButton ButtonAddLocomotive;
|
2023-10-21 22:32:22 +04:00
|
|
|
|
private JTextField textFieldNumber;
|
2023-10-21 19:25:05 +04:00
|
|
|
|
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;
|
2023-11-07 20:36:52 +04:00
|
|
|
|
private JButton ButtonRemoveObject;
|
2023-11-08 00:17:28 +04:00
|
|
|
|
private JButton Button_OpenFormRemovedLocomotives;
|
2023-10-21 19:25:05 +04:00
|
|
|
|
public DrawingLocomotive loco;
|
2023-11-07 20:36:52 +04:00
|
|
|
|
final LocomotivesGenericStorage _storage;
|
2023-11-08 00:17:28 +04:00
|
|
|
|
FrameElectricLocomotive _frameRemovedLocomotives;
|
2023-11-18 23:58:18 +04:00
|
|
|
|
private Stack<DrawingLocomotive> _stackRemoveObjects;
|
2023-11-08 00:17:28 +04:00
|
|
|
|
FrameDopClassParameters frameDopClassParameters;
|
2023-10-21 19:25:05 +04:00
|
|
|
|
public JPanel getPictureBoxCollections() {
|
|
|
|
|
return MainPanel;
|
|
|
|
|
}
|
2023-10-21 22:32:22 +04:00
|
|
|
|
|
|
|
|
|
public FormLocomotiveCollections() {
|
2023-11-18 23:58:18 +04:00
|
|
|
|
formElectricLocomotive = new FormElectricLocomotive();
|
|
|
|
|
_storage = new LocomotivesGenericStorage(400,300);
|
|
|
|
|
_stackRemoveObjects = new Stack<>();
|
2023-11-08 00:17:28 +04:00
|
|
|
|
listBoxStorage.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged);
|
2023-10-21 22:32:22 +04:00
|
|
|
|
|
2023-11-07 20:36:52 +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 -> {
|
2023-11-07 20:36:52 +04:00
|
|
|
|
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);
|
2023-11-08 00:17:28 +04:00
|
|
|
|
frameElectricLocomotive._formElectricLocomotive.ButtonSelectLocomotive.addActionListener(e2 -> {
|
|
|
|
|
loco = frameElectricLocomotive._formElectricLocomotive._drawingLocomotive;
|
2023-10-21 22:32:22 +04:00
|
|
|
|
frameElectricLocomotive.dispose();
|
|
|
|
|
if (loco != null) {
|
|
|
|
|
//проверяем, удалось ли нам загрузить объект
|
2023-11-07 20:36:52 +04:00
|
|
|
|
if (obj.AddOverload(loco)!= -1) {
|
2023-10-21 22:32:22 +04:00
|
|
|
|
JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен");
|
|
|
|
|
Refresh();
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-07 20:36:52 +04:00
|
|
|
|
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 -> {
|
2023-11-07 20:36:52 +04:00
|
|
|
|
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 {
|
2023-11-07 20:36:52 +04:00
|
|
|
|
pos = Integer.parseInt(textFieldNumber.getText());
|
2023-11-08 00:17:28 +04:00
|
|
|
|
} catch (NumberFormatException ex) {
|
2023-10-21 22:32:22 +04:00
|
|
|
|
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
|
|
|
|
|
"Неверное значение",
|
|
|
|
|
"Ошибка",
|
|
|
|
|
JOptionPane.ERROR_MESSAGE);
|
2023-11-08 00:17:28 +04:00
|
|
|
|
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) {
|
2023-11-18 23:58:18 +04:00
|
|
|
|
_stackRemoveObjects.push(removedPlane);
|
|
|
|
|
Refresh();
|
2023-11-08 00:17:28 +04:00
|
|
|
|
|
|
|
|
|
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
|
|
|
|
|
"Объект удален",
|
|
|
|
|
"Успех",
|
2023-11-18 23:58:18 +04:00
|
|
|
|
JOptionPane.INFORMATION_MESSAGE);
|
2023-11-08 00:17:28 +04:00
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
|
|
|
|
|
"Не удалось удалить объект",
|
|
|
|
|
"Ошибка",
|
|
|
|
|
JOptionPane.ERROR_MESSAGE);
|
2023-10-21 19:25:05 +04:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
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
|
|
|
|
|
2023-11-08 00:17:28 +04:00
|
|
|
|
Button_OpenFormRemovedLocomotives.addActionListener(e -> {
|
2023-11-18 23:58:18 +04:00
|
|
|
|
if(_stackRemoveObjects.empty()) {
|
2023-11-08 00:17:28 +04:00
|
|
|
|
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
|
|
|
|
|
"Вы ничего не удалили, кажется...",
|
|
|
|
|
"ой",
|
|
|
|
|
JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_frameRemovedLocomotives = new FrameElectricLocomotive();
|
2023-11-18 23:58:18 +04:00
|
|
|
|
_frameRemovedLocomotives._formElectricLocomotive._drawingLocomotive = _stackRemoveObjects.pop();
|
2023-11-08 00:17:28 +04:00
|
|
|
|
_frameRemovedLocomotives.setVisible(true);
|
|
|
|
|
_frameRemovedLocomotives._formElectricLocomotive.Draw();
|
|
|
|
|
});
|
|
|
|
|
|
2023-10-21 22:32:22 +04:00
|
|
|
|
}
|
2023-11-07 20:36:52 +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();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 00:17:28 +04:00
|
|
|
|
private void listBoxObjectsSelectedIndexChanged(ListSelectionEvent e) {
|
|
|
|
|
Refresh();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 22:32:22 +04:00
|
|
|
|
public void Refresh() {
|
2023-11-07 20:36:52 +04:00
|
|
|
|
if (listBoxStorage.getSelectedIndex() == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage.get(listBoxStorage.getSelectedValue().toString());
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-10-21 19:25:05 +04:00
|
|
|
|
Graphics g = pictureBoxCollections.getGraphics();
|
|
|
|
|
pictureBoxCollections.paint(g);
|
2023-11-08 00:17:28 +04:00
|
|
|
|
obj.ShowLocomotives(g);
|
2023-10-21 19:25:05 +04:00
|
|
|
|
}
|
|
|
|
|
}
|