226 lines
8.3 KiB
Java
226 lines
8.3 KiB
Java
package ProjectStormtrooper;
|
||
|
||
import javax.swing.*;
|
||
import javax.swing.event.ListSelectionEvent;
|
||
import java.awt.*;
|
||
import java.awt.event.ActionEvent;
|
||
import java.util.Objects;
|
||
import java.util.Stack;
|
||
|
||
public class FormPlaneCollection {
|
||
final PlanesGenericStorage _storage;
|
||
FrameDoubleParametrized _frameDoubleParametrized;
|
||
FrameStormtrooper _frameRemovedPlanes;
|
||
private JPanel PanelWrapper;
|
||
private JPanel GroupBoxInstruments;
|
||
private JPanel PictureBoxCollection;
|
||
private JButton buttonAddPlane;
|
||
private JTextField textFieldNumber;
|
||
private JButton buttonRemovePlane;
|
||
private JButton buttonRefreshCollection;
|
||
private JButton buttonOpenGenerateWindow;
|
||
private JTextField textFieldStorageName;
|
||
private JButton buttonAddStorage;
|
||
private JList listBoxStorages;
|
||
private JButton buttonRemoveStorage;
|
||
private JPanel storagesPanel;
|
||
private JButton buttonShowRemovedPlanes;
|
||
public DrawingPlane SelectedPlane;
|
||
Stack<DrawingPlane> _removedPlanes;
|
||
|
||
|
||
public JPanel getPanelWrapper() {
|
||
return PanelWrapper;
|
||
}
|
||
|
||
public FormPlaneCollection() {
|
||
PictureBoxCollection.setPreferredSize(new Dimension(600, 500));
|
||
_storage = new PlanesGenericStorage(600, 500);
|
||
_removedPlanes = new Stack<>();
|
||
buttonAddPlane.addActionListener(this::buttonAddPlaneClicked);
|
||
buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked);
|
||
buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked);
|
||
buttonOpenGenerateWindow.addActionListener(this::buttonOpenGenerateWindowClicked);
|
||
buttonAddStorage.addActionListener(this::buttonAddStorageClicked);
|
||
buttonRemoveStorage.addActionListener(this::buttonRemoveStorageClicked);
|
||
listBoxStorages.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged);
|
||
buttonShowRemovedPlanes.addActionListener(this::buttonShowRemovedPlanesClicked);
|
||
}
|
||
|
||
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();
|
||
}
|
||
|
||
public void buttonAddPlaneClicked(ActionEvent e) {
|
||
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) {
|
||
if (listBoxStorages.getSelectedIndex() == -1) {
|
||
return;
|
||
}
|
||
plane.SetDrawingBounds(PictureBoxCollection.getWidth(), PictureBoxCollection.getHeight());
|
||
var obj = _storage.Get(listBoxStorages.getSelectedValue().toString());
|
||
if (obj == null) {
|
||
return;
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
|
||
public void buttonRemovePlaneClicked(ActionEvent e) {
|
||
if (listBoxStorages.getSelectedIndex() == -1) {
|
||
return;
|
||
}
|
||
var obj = _storage.Get(listBoxStorages.getSelectedValue().toString());
|
||
if (obj == null) {
|
||
return;
|
||
}
|
||
int pos;
|
||
try {
|
||
pos = Integer.parseInt(textFieldNumber.getText());
|
||
} catch (NumberFormatException ex) {
|
||
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
||
"Неверное значение",
|
||
"Ошибка",
|
||
JOptionPane.ERROR_MESSAGE);
|
||
return;
|
||
}
|
||
Object[] options = {"Да", "Нет"};
|
||
int n = JOptionPane.showOptionDialog(this.getPanelWrapper(),
|
||
"Удалить объект?",
|
||
"Все серьезно",
|
||
JOptionPane.YES_NO_OPTION,
|
||
JOptionPane.QUESTION_MESSAGE,
|
||
null,
|
||
options,
|
||
options[0]
|
||
);
|
||
if (n == 1) {
|
||
return;
|
||
}
|
||
DrawingPlane removedPlane = obj.Sub(pos);
|
||
if (removedPlane != null) {
|
||
_removedPlanes.push(removedPlane);
|
||
refreshPictureBox();
|
||
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
||
"Объект удален",
|
||
"Успех",
|
||
JOptionPane.INFORMATION_MESSAGE);
|
||
} else {
|
||
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
||
"Не удалось удалить объект",
|
||
"Ошибка",
|
||
JOptionPane.ERROR_MESSAGE);
|
||
}
|
||
}
|
||
|
||
public void buttonRefreshCollectionClicked(ActionEvent e) {
|
||
refreshPictureBox();
|
||
}
|
||
|
||
public void buttonOpenGenerateWindowClicked(ActionEvent e) {
|
||
if (_frameDoubleParametrized != null) {
|
||
_frameDoubleParametrized.dispose();
|
||
}
|
||
_frameDoubleParametrized = new FrameDoubleParametrized();
|
||
_frameDoubleParametrized.setVisible(true);
|
||
}
|
||
|
||
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();
|
||
}
|
||
|
||
public void refreshPictureBox() {
|
||
if (listBoxStorages.getSelectedIndex() == -1) {
|
||
return;
|
||
}
|
||
var obj = _storage.Get(listBoxStorages.getSelectedValue().toString());
|
||
if (obj == null) {
|
||
return;
|
||
}
|
||
Graphics g = PictureBoxCollection.getGraphics();
|
||
PictureBoxCollection.paint(g);
|
||
obj.ShowPlanes(g);
|
||
}
|
||
}
|