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-10-20 16:22:20 +04:00
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
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-10-20 22:43:07 +04:00
|
|
|
|
PictureBoxCollection.setPreferredSize(new Dimension(600, 500));
|
2023-10-24 14:17:48 +04:00
|
|
|
|
_storage = new PlanesGenericStorage(600, 500);
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-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 22:43:07 +04:00
|
|
|
|
FrameStormtrooper frameStormtrooper = new FrameStormtrooper();
|
|
|
|
|
frameStormtrooper.setVisible(true);
|
2023-11-07 12:57:17 +04:00
|
|
|
|
frameStormtrooper._formStromtrooper.buttonSelectPlane.addActionListener(ev -> {
|
|
|
|
|
SelectedPlane = frameStormtrooper._formStromtrooper._drawingPlane;
|
2023-10-20 22:43:07 +04:00
|
|
|
|
frameStormtrooper.dispose();
|
|
|
|
|
if (SelectedPlane != null) {
|
2023-10-24 14:17:48 +04:00
|
|
|
|
if (obj.Add(SelectedPlane) > -1) {
|
2023-10-20 22:43:07 +04:00
|
|
|
|
refreshPictureBox();
|
|
|
|
|
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
|
|
|
|
"Объект добавлен",
|
|
|
|
|
"Успех",
|
|
|
|
|
JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
|
|
|
|
"Не удалось добавить объект",
|
|
|
|
|
"Ошибка",
|
|
|
|
|
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 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
|
|
|
|
}
|
|
|
|
|
}
|