2023-10-20 16:22:20 +04:00
|
|
|
|
package ProjectStormtrooper;
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
2023-10-24 14:17:48 +04:00
|
|
|
|
import javax.swing.event.ListSelectionEvent;
|
2023-12-17 19:08:22 +04:00
|
|
|
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
2023-10-20 16:22:20 +04:00
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
2023-12-17 19:08:22 +04:00
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
import java.io.File;
|
2023-10-24 14:17:48 +04:00
|
|
|
|
import java.util.Objects;
|
2023-11-07 12:57:17 +04:00
|
|
|
|
import java.util.Stack;
|
2023-10-20 16:22:20 +04:00
|
|
|
|
|
|
|
|
|
public class FormPlaneCollection {
|
2023-10-24 14:17:48 +04:00
|
|
|
|
final PlanesGenericStorage _storage;
|
2023-10-22 13:01:12 +04:00
|
|
|
|
FrameDoubleParametrized _frameDoubleParametrized;
|
2023-11-07 12:57:17 +04:00
|
|
|
|
FrameStormtrooper _frameRemovedPlanes;
|
2023-10-20 16:22:20 +04:00
|
|
|
|
private JPanel PanelWrapper;
|
|
|
|
|
private JPanel GroupBoxInstruments;
|
|
|
|
|
private JPanel PictureBoxCollection;
|
|
|
|
|
private JButton buttonAddPlane;
|
|
|
|
|
private JTextField textFieldNumber;
|
|
|
|
|
private JButton buttonRemovePlane;
|
|
|
|
|
private JButton buttonRefreshCollection;
|
2023-10-22 13:01:12 +04:00
|
|
|
|
private JButton buttonOpenGenerateWindow;
|
2023-10-24 14:17:48 +04:00
|
|
|
|
private JTextField textFieldStorageName;
|
|
|
|
|
private JButton buttonAddStorage;
|
|
|
|
|
private JList listBoxStorages;
|
|
|
|
|
private JButton buttonRemoveStorage;
|
|
|
|
|
private JPanel storagesPanel;
|
2023-11-07 12:57:17 +04:00
|
|
|
|
private JButton buttonShowRemovedPlanes;
|
2023-10-20 22:43:07 +04:00
|
|
|
|
public DrawingPlane SelectedPlane;
|
2023-11-07 12:57:17 +04:00
|
|
|
|
Stack<DrawingPlane> _removedPlanes;
|
2023-10-20 22:43:07 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public JPanel getPanelWrapper() {
|
|
|
|
|
return PanelWrapper;
|
|
|
|
|
}
|
2023-10-20 16:22:20 +04:00
|
|
|
|
|
|
|
|
|
public FormPlaneCollection() {
|
2023-12-19 13:32:53 +04:00
|
|
|
|
PictureBoxCollection.setPreferredSize(new Dimension(800, 600));
|
|
|
|
|
_storage = new PlanesGenericStorage(800, 600);
|
2023-11-07 12:57:17 +04:00
|
|
|
|
_removedPlanes = new Stack<>();
|
2023-10-20 16:22:20 +04:00
|
|
|
|
buttonAddPlane.addActionListener(this::buttonAddPlaneClicked);
|
|
|
|
|
buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked);
|
|
|
|
|
buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked);
|
2023-10-22 13:01:12 +04:00
|
|
|
|
buttonOpenGenerateWindow.addActionListener(this::buttonOpenGenerateWindowClicked);
|
2023-10-24 14:17:48 +04:00
|
|
|
|
buttonAddStorage.addActionListener(this::buttonAddStorageClicked);
|
|
|
|
|
buttonRemoveStorage.addActionListener(this::buttonRemoveStorageClicked);
|
|
|
|
|
listBoxStorages.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged);
|
2023-11-07 12:57:17 +04:00
|
|
|
|
buttonShowRemovedPlanes.addActionListener(this::buttonShowRemovedPlanesClicked);
|
2023-10-24 14:17:48 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-17 19:08:22 +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 23:57:27 +04:00
|
|
|
|
try {
|
|
|
|
|
if (_storage.LoadData(selectedFile.getAbsolutePath())) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Не загрузилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(
|
|
|
|
|
null,
|
|
|
|
|
"Ошибка при загрузке объектов" + ex.getMessage(),
|
|
|
|
|
"Результат",
|
|
|
|
|
JOptionPane.ERROR_MESSAGE
|
|
|
|
|
);
|
2023-12-17 19:08:22 +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-18 23:57:27 +04:00
|
|
|
|
try {
|
|
|
|
|
if (_storage.SaveData(selectedFile.getAbsolutePath()))
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
else
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Не сохранилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(
|
|
|
|
|
null,
|
|
|
|
|
"Ошибка при сохранении объектов" + ex.getMessage(),
|
|
|
|
|
"Результат",
|
|
|
|
|
JOptionPane.ERROR_MESSAGE
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-12-17 19:08:22 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
2023-12-17 23:06:17 +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();
|
|
|
|
|
if (_storage.LoadDataSingle(selectedFile.getAbsolutePath())) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Не загрузилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ReloadObjects();
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
JMenuItem saveItemSingle = new JMenuItem("Сохранить коллекцию");
|
|
|
|
|
saveItemSingle.addActionListener(
|
|
|
|
|
e -> {
|
|
|
|
|
if (listBoxStorages.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();
|
|
|
|
|
if (_storage.SaveDataSingle(selectedFile.getAbsolutePath(), (String) listBoxStorages.getSelectedValue()))
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
else
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Не сохранилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
2023-12-17 19:08:22 +04:00
|
|
|
|
fileMenu.add(openItem);
|
|
|
|
|
fileMenu.add(saveItem);
|
2023-12-17 23:06:17 +04:00
|
|
|
|
fileMenu.add(openItemSingle);
|
|
|
|
|
fileMenu.add(saveItemSingle);
|
2023-12-17 19:08:22 +04:00
|
|
|
|
menuBar.add(fileMenu);
|
|
|
|
|
return menuBar;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 14:17:48 +04:00
|
|
|
|
private void ReloadObjects() {
|
|
|
|
|
int index = listBoxStorages.getSelectedIndex();
|
|
|
|
|
listBoxStorages.setListData(_storage.Keys().toArray());
|
|
|
|
|
if (listBoxStorages.getModel().getSize() > 0 && (index == -1 || index >= listBoxStorages.getModel().getSize())) {
|
|
|
|
|
listBoxStorages.setSelectedIndex(0);
|
|
|
|
|
} else if (listBoxStorages.getModel().getSize() > 0 && index > -1 && index < listBoxStorages.getModel().getSize()) {
|
|
|
|
|
listBoxStorages.setSelectedIndex(index);
|
|
|
|
|
}
|
|
|
|
|
listBoxStorages.invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonAddStorageClicked(ActionEvent e) {
|
|
|
|
|
String storageName = textFieldStorageName.getText();
|
|
|
|
|
if (Objects.equals(storageName, "")) {
|
|
|
|
|
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
|
|
|
|
"Введите название",
|
|
|
|
|
"Ошибка",
|
|
|
|
|
JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_storage.AddSet(storageName);
|
|
|
|
|
ReloadObjects();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void listBoxObjectsSelectedIndexChanged(ListSelectionEvent e) {
|
|
|
|
|
refreshPictureBox();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonRemoveStorageClicked(ActionEvent e) {
|
|
|
|
|
if (listBoxStorages.getSelectedIndex() == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Object[] options = {"Да", "Нет"};
|
|
|
|
|
int n = JOptionPane.showOptionDialog(this.getPanelWrapper(),
|
|
|
|
|
"Удалить объект?",
|
|
|
|
|
"Все серьезно",
|
|
|
|
|
JOptionPane.YES_NO_OPTION,
|
|
|
|
|
JOptionPane.QUESTION_MESSAGE,
|
|
|
|
|
null,
|
|
|
|
|
options,
|
|
|
|
|
options[0]
|
|
|
|
|
);
|
|
|
|
|
if (n == 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_storage.DelSet(listBoxStorages.getSelectedValue().toString());
|
|
|
|
|
ReloadObjects();
|
2023-10-20 16:22:20 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void buttonAddPlaneClicked(ActionEvent e) {
|
2023-12-05 11:32:33 +04:00
|
|
|
|
FrameStormtrooper frameConfig = new FrameStormtrooper();
|
|
|
|
|
FormPlaneConfig formConfig = new FormPlaneConfig();
|
|
|
|
|
frameConfig.setContentPane(formConfig.panelWrapper);
|
|
|
|
|
formConfig.buttonApply.addActionListener(ev -> {
|
|
|
|
|
AddPlaneListener(formConfig.selectedPlane);
|
|
|
|
|
frameConfig.dispose();
|
|
|
|
|
});
|
|
|
|
|
formConfig.buttonCancel.addActionListener(ev -> {
|
|
|
|
|
frameConfig.dispose();
|
|
|
|
|
});
|
|
|
|
|
frameConfig.setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddPlaneListener(DrawingPlane plane) {
|
2023-10-24 14:17:48 +04:00
|
|
|
|
if (listBoxStorages.getSelectedIndex() == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-05 11:32:33 +04:00
|
|
|
|
plane.SetDrawingBounds(PictureBoxCollection.getWidth(), PictureBoxCollection.getHeight());
|
2023-10-24 14:17:48 +04:00
|
|
|
|
var obj = _storage.Get(listBoxStorages.getSelectedValue().toString());
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-05 11:32:33 +04:00
|
|
|
|
SelectedPlane = plane;
|
|
|
|
|
if (SelectedPlane != null) {
|
|
|
|
|
if (obj.Add(SelectedPlane) > -1) {
|
|
|
|
|
refreshPictureBox();
|
|
|
|
|
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
|
|
|
|
"Объект добавлен",
|
|
|
|
|
"Успех",
|
|
|
|
|
JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
|
|
|
|
"Не удалось добавить объект",
|
|
|
|
|
"Ошибка",
|
|
|
|
|
JOptionPane.ERROR_MESSAGE);
|
2023-10-20 22:43:07 +04:00
|
|
|
|
}
|
2023-12-05 11:32:33 +04:00
|
|
|
|
}
|
2023-10-20 16:22:20 +04:00
|
|
|
|
}
|
2023-10-20 22:43:07 +04:00
|
|
|
|
|
2023-10-20 16:22:20 +04:00
|
|
|
|
public void buttonRemovePlaneClicked(ActionEvent e) {
|
2023-10-24 14:17:48 +04:00
|
|
|
|
if (listBoxStorages.getSelectedIndex() == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage.Get(listBoxStorages.getSelectedValue().toString());
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-10-22 13:01:12 +04:00
|
|
|
|
int pos;
|
|
|
|
|
try {
|
|
|
|
|
pos = Integer.parseInt(textFieldNumber.getText());
|
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
|
|
|
|
"Неверное значение",
|
|
|
|
|
"Ошибка",
|
|
|
|
|
JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-10-20 22:43:07 +04:00
|
|
|
|
Object[] options = {"Да", "Нет"};
|
|
|
|
|
int n = JOptionPane.showOptionDialog(this.getPanelWrapper(),
|
|
|
|
|
"Удалить объект?",
|
|
|
|
|
"Все серьезно",
|
|
|
|
|
JOptionPane.YES_NO_OPTION,
|
|
|
|
|
JOptionPane.QUESTION_MESSAGE,
|
|
|
|
|
null,
|
|
|
|
|
options,
|
|
|
|
|
options[0]
|
|
|
|
|
);
|
|
|
|
|
if (n == 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-11-07 12:57:17 +04:00
|
|
|
|
DrawingPlane removedPlane = obj.Sub(pos);
|
|
|
|
|
if (removedPlane != null) {
|
|
|
|
|
_removedPlanes.push(removedPlane);
|
2023-10-22 13:01:12 +04:00
|
|
|
|
refreshPictureBox();
|
2023-10-20 22:43:07 +04:00
|
|
|
|
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
2023-10-22 13:01:12 +04:00
|
|
|
|
"Объект удален",
|
|
|
|
|
"Успех",
|
|
|
|
|
JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
|
|
|
|
"Не удалось удалить объект",
|
2023-10-20 22:43:07 +04:00
|
|
|
|
"Ошибка",
|
|
|
|
|
JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
}
|
2023-10-20 16:22:20 +04:00
|
|
|
|
}
|
2023-10-20 22:43:07 +04:00
|
|
|
|
|
2023-10-20 16:22:20 +04:00
|
|
|
|
public void buttonRefreshCollectionClicked(ActionEvent e) {
|
2023-10-20 22:43:07 +04:00
|
|
|
|
refreshPictureBox();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-22 13:01:12 +04:00
|
|
|
|
public void buttonOpenGenerateWindowClicked(ActionEvent e) {
|
|
|
|
|
if (_frameDoubleParametrized != null) {
|
|
|
|
|
_frameDoubleParametrized.dispose();
|
|
|
|
|
}
|
|
|
|
|
_frameDoubleParametrized = new FrameDoubleParametrized();
|
|
|
|
|
_frameDoubleParametrized.setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 12:57:17 +04:00
|
|
|
|
public void buttonShowRemovedPlanesClicked(ActionEvent e) {
|
|
|
|
|
if (_removedPlanes.empty()) {
|
|
|
|
|
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
|
|
|
|
"Нет удаленных объектов",
|
|
|
|
|
"Инфо",
|
|
|
|
|
JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_frameRemovedPlanes = new FrameStormtrooper();
|
|
|
|
|
_frameRemovedPlanes._formStromtrooper._drawingPlane = _removedPlanes.pop();
|
|
|
|
|
_frameRemovedPlanes.setVisible(true);
|
|
|
|
|
_frameRemovedPlanes._formStromtrooper.Draw();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-20 22:43:07 +04:00
|
|
|
|
public void refreshPictureBox() {
|
2023-10-24 14:17:48 +04:00
|
|
|
|
if (listBoxStorages.getSelectedIndex() == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var obj = _storage.Get(listBoxStorages.getSelectedValue().toString());
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-10-20 16:22:20 +04:00
|
|
|
|
Graphics g = PictureBoxCollection.getGraphics();
|
2023-10-20 22:43:07 +04:00
|
|
|
|
PictureBoxCollection.paint(g);
|
2023-10-24 14:17:48 +04:00
|
|
|
|
obj.ShowPlanes(g);
|
2023-10-20 16:22:20 +04:00
|
|
|
|
}
|
|
|
|
|
}
|