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-12-18 00:49:42 +04:00
|
|
|
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
2023-10-21 19:25:05 +04:00
|
|
|
|
import java.awt.*;
|
2023-12-18 00:49:42 +04:00
|
|
|
|
import java.io.File;
|
2023-12-18 20:30:21 +04:00
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.IOException;
|
2023-11-08 00:17:28 +04:00
|
|
|
|
import java.util.Stack;
|
2023-12-19 00:19:35 +04:00
|
|
|
|
import org.apache.logging.log4j.*;
|
2023-11-06 20:57:19 +04:00
|
|
|
|
|
2023-10-21 22:32:22 +04:00
|
|
|
|
public class FormLocomotiveCollections {
|
2023-12-19 00:19:35 +04:00
|
|
|
|
private final Logger _logger;
|
2023-11-18 23:58:18 +04:00
|
|
|
|
FormElectricLocomotive formElectricLocomotive;
|
2023-11-20 16:33:32 +04:00
|
|
|
|
FormLocomotiveConfig formLocomotiveConfig;
|
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-12-18 20:30:21 +04:00
|
|
|
|
|
2023-10-21 19:25:05 +04:00
|
|
|
|
public JPanel getPictureBoxCollections() {
|
|
|
|
|
return MainPanel;
|
|
|
|
|
}
|
2023-10-21 22:32:22 +04:00
|
|
|
|
|
2023-12-19 11:33:23 +04:00
|
|
|
|
public FormLocomotiveCollections() {
|
2023-12-19 00:19:35 +04:00
|
|
|
|
|
2023-12-19 03:41:22 +04:00
|
|
|
|
_logger = LogManager.getLogger("logger");
|
2023-12-19 00:19:35 +04:00
|
|
|
|
|
2023-11-18 23:58:18 +04:00
|
|
|
|
formElectricLocomotive = new FormElectricLocomotive();
|
2023-11-21 12:30:54 +04:00
|
|
|
|
formLocomotiveConfig = new FormLocomotiveConfig();
|
2023-12-18 20:30:21 +04:00
|
|
|
|
_storage = new LocomotivesGenericStorage(400, 300);
|
2023-11-18 23:58:18 +04:00
|
|
|
|
_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();
|
2023-12-19 02:22:26 +04:00
|
|
|
|
_logger.info("Добавлен набор: " + NameStorage);
|
2023-11-07 20:36:52 +04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ButtonRemoveObject.addActionListener(e ->
|
|
|
|
|
{
|
|
|
|
|
if (listBoxStorage.getSelectedIndex() == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Коллекция удалена", "Удаление", JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
_storage.DelSet((String) listBoxStorage.getSelectedValue()); //ТУТ СТРИНГ ОБРАТИ ВНИМАНИЕ КАК-НИБУДЬ
|
|
|
|
|
ReloadObjects();
|
2023-12-19 03:41:22 +04:00
|
|
|
|
_logger.info("Удален набор: " + listBoxStorage.getSelectedValue());
|
2023-11-07 20:36:52 +04:00
|
|
|
|
});
|
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-11-20 16:33:32 +04:00
|
|
|
|
FrameLocomotiveConfig frameLocomotiveConfig = new FrameLocomotiveConfig();
|
|
|
|
|
frameLocomotiveConfig.setVisible(true);
|
2023-12-18 20:30:21 +04:00
|
|
|
|
frameLocomotiveConfig._formLocomotiveConfig.buttonOk.addActionListener(e2 -> {
|
2023-11-21 12:30:54 +04:00
|
|
|
|
frameLocomotiveConfig.dispose();
|
2023-12-04 13:28:12 +04:00
|
|
|
|
loco = frameLocomotiveConfig._formLocomotiveConfig._loco;
|
2023-12-18 20:30:21 +04:00
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
if (loco != null) {
|
|
|
|
|
if (obj.AddOverload(loco) != -1) {
|
|
|
|
|
JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен");
|
|
|
|
|
Refresh();
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект");
|
|
|
|
|
}
|
2023-12-04 13:28:12 +04:00
|
|
|
|
}
|
2023-12-18 20:30:21 +04:00
|
|
|
|
frameLocomotiveConfig._formLocomotiveConfig.buttonClose.addActionListener(e3 -> {
|
|
|
|
|
frameLocomotiveConfig.dispose();
|
|
|
|
|
});
|
|
|
|
|
} catch (LocoStorageOverflowException ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, ex.getMessage());
|
2023-12-19 03:41:22 +04:00
|
|
|
|
_logger.warn("Превышено допустимое количество объектов в коллекции");
|
2023-12-04 13:28:12 +04:00
|
|
|
|
}
|
2023-10-21 22:32:22 +04:00
|
|
|
|
});
|
2023-11-20 16:33:32 +04:00
|
|
|
|
|
2023-10-21 22:32:22 +04:00
|
|
|
|
});
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2023-12-18 20:30:21 +04:00
|
|
|
|
int pos = Integer.parseInt(textFieldNumber.getText());
|
2023-10-21 22:32:22 +04:00
|
|
|
|
try {
|
2023-12-18 20:30:21 +04:00
|
|
|
|
/*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();
|
2023-11-08 00:17:28 +04:00
|
|
|
|
|
2023-12-18 20:30:21 +04:00
|
|
|
|
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
|
|
|
|
|
"Объект удален",
|
|
|
|
|
"Успех",
|
|
|
|
|
JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
|
|
|
|
|
"Не удалось удалить объект",
|
|
|
|
|
"Ошибка",
|
|
|
|
|
JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
}
|
2023-12-19 00:19:35 +04:00
|
|
|
|
} catch (LocoNotFoundException ex) {
|
2023-12-18 20:30:21 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, ex.getMessage());
|
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-12-18 20:30:21 +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
|
|
|
|
|
2023-12-18 00:49:42 +04:00
|
|
|
|
public JMenuBar getMenuBar() {
|
|
|
|
|
JMenuBar menuBar = new JMenuBar();
|
|
|
|
|
JMenu fileMenu = new JMenu("Файл");
|
|
|
|
|
JMenuItem openItem = new JMenuItem("Загрузить");
|
|
|
|
|
openItem.addActionListener(
|
|
|
|
|
e -> {
|
|
|
|
|
JFileChooser fileChooser = new JFileChooser();
|
|
|
|
|
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
|
|
|
|
fileChooser.setDialogTitle("Выберите файл для загрузки данных");
|
|
|
|
|
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
|
|
|
|
File selectedFile = fileChooser.getSelectedFile();
|
2023-12-18 20:30:21 +04:00
|
|
|
|
try {
|
|
|
|
|
if (_storage.LoadData(selectedFile.getAbsolutePath())) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Не загрузилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
} catch (FileNotFoundException ex) {
|
2023-12-19 00:19:35 +04:00
|
|
|
|
// logger.error("\n" + "Ошибка при загрузке всех объектов из файла: " + ex.getMessage());
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Ошибка при загрузке всех объектов " + ex.getMessage(), "Результат", JOptionPane.ERROR_MESSAGE);
|
2023-12-18 00:49:42 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ReloadObjects();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
JMenuItem saveItem = new JMenuItem("Сохранить");
|
|
|
|
|
saveItem.addActionListener(
|
|
|
|
|
e -> {
|
|
|
|
|
JFileChooser fileChooser = new JFileChooser();
|
|
|
|
|
fileChooser.setDialogTitle("Выберите файл для сохранения данных");
|
|
|
|
|
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
|
|
|
|
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
|
|
|
|
|
File selectedFile = fileChooser.getSelectedFile();
|
2023-12-19 00:19:35 +04:00
|
|
|
|
try {
|
|
|
|
|
_storage.SaveData(selectedFile.getAbsolutePath());
|
2023-12-18 00:49:42 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
2023-12-19 00:19:35 +04:00
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Ошибка при сохранении всех объектов " + ex.getMessage(), "Результат", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-18 00:49:42 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
JMenuItem openItemSingle = new JMenuItem("Загрузить коллекцию");
|
|
|
|
|
openItemSingle.addActionListener(
|
|
|
|
|
e -> {
|
|
|
|
|
JFileChooser fileChooser = new JFileChooser();
|
|
|
|
|
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
|
|
|
|
fileChooser.setDialogTitle("Выберите файл для загрузки данных");
|
|
|
|
|
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
|
|
|
|
File selectedFile = fileChooser.getSelectedFile();
|
2023-12-18 20:30:21 +04:00
|
|
|
|
try {
|
|
|
|
|
if (_storage.LoadDataSingle(selectedFile.getAbsolutePath())) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Не загрузилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException ex) {
|
2023-12-19 00:19:35 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, "Ошибка при загрузке коллекции " + ex.getMessage(), "Результат", JOptionPane.ERROR_MESSAGE);
|
2023-12-18 00:49:42 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ReloadObjects();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
JMenuItem saveItemSingle = new JMenuItem("Сохранить коллекцию");
|
|
|
|
|
saveItemSingle.addActionListener(
|
|
|
|
|
e -> {
|
|
|
|
|
if (listBoxStorage.getSelectedValue() == null) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Коллекция не выбрана", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
JFileChooser fileChooser = new JFileChooser();
|
|
|
|
|
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
|
|
|
|
fileChooser.setDialogTitle("Выберите файл для сохранения данных");
|
|
|
|
|
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
|
|
|
|
|
File selectedFile = fileChooser.getSelectedFile();
|
2023-12-19 00:19:35 +04:00
|
|
|
|
try {
|
|
|
|
|
_storage.SaveDataSingle(selectedFile.getAbsolutePath(), (String) listBoxStorage.getSelectedValue());
|
2023-12-18 00:49:42 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
2023-12-19 00:19:35 +04:00
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Ошибка при сохранении коллекции " + ex.getMessage(), "Результат", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-18 00:49:42 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
fileMenu.add(openItem);
|
|
|
|
|
fileMenu.add(saveItem);
|
|
|
|
|
fileMenu.add(openItemSingle);
|
|
|
|
|
fileMenu.add(saveItemSingle);
|
|
|
|
|
menuBar.add(fileMenu);
|
|
|
|
|
return menuBar;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|