PIbd-12_Karamushko_M.K._Air.../FormMapWithSetAircrafts.java

318 lines
12 KiB
Java
Raw Normal View History

2022-11-15 18:20:34 +04:00
import javax.swing.*;
2022-12-02 11:17:40 +04:00
import javax.swing.filechooser.FileNameExtensionFilter;
2022-11-15 18:20:34 +04:00
import java.awt.*;
2022-11-25 13:49:46 +04:00
import java.util.*;
import java.util.List;
2022-11-15 18:20:34 +04:00
public class FormMapWithSetAircrafts implements Form {
private JPanel MainPane;
2022-11-25 13:49:46 +04:00
private JButton buttonAddAircraft;
2022-11-15 18:20:34 +04:00
private JComboBox comboBoxSelectorMap;
private JTextField textBoxPosition;
2022-11-25 13:49:46 +04:00
private JButton buttonRemoveAircraft;
2022-11-15 18:20:34 +04:00
private JButton buttonShowStorage;
private JButton buttonShowOnMap;
private JButton upButton;
private JButton leftButton;
private JButton rightButton;
private JButton downButton;
private JPanel drawPanel;
2022-11-25 13:49:46 +04:00
private JTextField textBoxNewMapName;
private JList listBoxMaps;
private JButton buttonAddMap;
private JButton buttonDeleteMap;
private JButton buttonShowDeleted;
2022-12-02 11:17:40 +04:00
private JMenuBar menu;
2022-11-25 13:49:46 +04:00
private MapsCollection _mapsCollection;
private HashMap<String, AbstractMap> _mapsDict = new HashMap<>(){{
put( "Простая карта", new SimpleMap() );
put( "Моя карта", new MyMap() );
}};
2022-11-15 18:20:34 +04:00
private Canvas canv = new Canvas(this);
2022-11-25 13:49:46 +04:00
private Queue<IDrawingObject> deletedAircrafts = new ArrayDeque<>();
2022-11-15 18:20:34 +04:00
JFrame jFrame = getFrame();
Image img;
private JFrame getFrame() {
JFrame frame = new JFrame();
frame.setVisible(true);
2022-11-25 13:49:46 +04:00
frame.setBounds(300, 50, 1000, 750);
2022-11-15 18:20:34 +04:00
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
2022-11-25 13:49:46 +04:00
private void ReloadMaps()
{
int index = listBoxMaps.getSelectedIndex();
List<String> items = _mapsCollection.getKeys();
listBoxMaps.setListData(items.toArray());
if (items.size() > 0 && (index == -1 || index >= items.size()))
{
listBoxMaps.setSelectedIndex(0);
}
else if (items.size() > 0 && index > -1 && index < items.size())
{
listBoxMaps.setSelectedIndex(index);
}
}
2022-11-15 18:20:34 +04:00
public void run() {
jFrame.add(MainPane);
drawPanel.add(canv);
2022-11-25 13:49:46 +04:00
jFrame.revalidate();
2022-12-02 11:17:40 +04:00
menu = new JMenuBar();
JMenu fileActions = new JMenu("Файл");
menu.add(fileActions);
jFrame.setJMenuBar(menu);
JMenuItem saveItem = new JMenuItem("Сохранить");
JMenuItem loadItem = new JMenuItem("Загрузить");
JMenuItem saveMapItem = new JMenuItem("Сохранить карту");
JMenuItem loadMapItem = new JMenuItem("Загрузить карту");
fileActions.add(saveItem);
fileActions.add(loadItem);
fileActions.add(saveMapItem);
fileActions.add(loadMapItem);
saveItem.addActionListener(e -> {
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("TXT format", "txt"));
chooser.showSaveDialog(jFrame);
if(chooser.getSelectedFile() == null) return;
String path = chooser.getSelectedFile().getAbsolutePath();
if(_mapsCollection.SaveData(path)) {
JOptionPane.showMessageDialog(jFrame, "Сохранение прошло успешно");
} else JOptionPane.showMessageDialog(jFrame, "Не сохранилось");
});
loadItem.addActionListener(e -> {
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("TXT format", "txt"));
chooser.showOpenDialog(jFrame);
if(chooser.getSelectedFile() == null) return;
String path = chooser.getSelectedFile().getAbsolutePath();
if(_mapsCollection.LoadData(path)) {
JOptionPane.showMessageDialog(jFrame, "Загрузка прошла успешно");
ReloadMaps();
} else JOptionPane.showMessageDialog(jFrame, "Не загрузилось");
});
saveMapItem.addActionListener(e -> {
if(_mapsCollection == null) return;
if(listBoxMaps.getSelectedValue() == null) return;
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("TXT format", "txt"));
chooser.showSaveDialog(jFrame);
if(chooser.getSelectedFile() == null) return;
String path = chooser.getSelectedFile().getAbsolutePath();
if(_mapsCollection.SaveMap(path, listBoxMaps.getSelectedValue().toString())) {
JOptionPane.showMessageDialog(jFrame, "Сохранение прошло успешно");
} else JOptionPane.showMessageDialog(jFrame, "Не сохранилось");
});
loadMapItem.addActionListener(e -> {
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("TXT format", "txt"));
chooser.showOpenDialog(jFrame);
if(chooser.getSelectedFile() == null) return;
String path = chooser.getSelectedFile().getAbsolutePath();
if(_mapsCollection.LoadMap(path)) {
JOptionPane.showMessageDialog(jFrame, "Загрузка прошла успешно");
ReloadMaps();
} else JOptionPane.showMessageDialog(jFrame, "Не загрузилось");
});
2022-11-25 13:49:46 +04:00
_mapsCollection = new MapsCollection(canv.getSize().width, canv.getSize().height);
comboBoxSelectorMap.removeAllItems();
_mapsDict.keySet().forEach(elem -> {
comboBoxSelectorMap.addItem(elem);
});
2022-11-15 18:20:34 +04:00
comboBoxSelectorMap.setSelectedIndex(-1);
2022-11-25 13:49:46 +04:00
listBoxMaps.addListSelectionListener(e -> {
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowSet();
canv.repaint();
});
buttonAddMap.addActionListener(e -> {
if (comboBoxSelectorMap.getSelectedIndex() == -1 || textBoxNewMapName.getText() == "")
{
JOptionPane.showMessageDialog(jFrame, "Не все данные заполнены");
return;
}
if (!_mapsDict.containsKey(comboBoxSelectorMap.getSelectedItem().toString()))
2022-11-15 18:20:34 +04:00
{
2022-11-25 13:49:46 +04:00
JOptionPane.showMessageDialog(jFrame, "Нет такой карты");
return;
2022-11-15 18:20:34 +04:00
}
2022-11-25 13:49:46 +04:00
_mapsCollection.AddMap(textBoxNewMapName.getText(),
_mapsDict.get(comboBoxSelectorMap.getSelectedItem().toString()));
ReloadMaps();
});
buttonDeleteMap.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1)
2022-11-15 18:20:34 +04:00
{
2022-11-25 13:49:46 +04:00
return;
2022-11-15 18:20:34 +04:00
}
2022-11-25 13:49:46 +04:00
String mapName = listBoxMaps.getSelectedValue().toString();
if (JOptionPane.showConfirmDialog(jFrame, "Удалить карту " + mapName,
"Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
2022-11-15 18:20:34 +04:00
{
2022-11-25 13:49:46 +04:00
_mapsCollection.DelMap(mapName);
ReloadMaps();
2022-11-15 18:20:34 +04:00
}
});
2022-11-29 17:50:40 +04:00
buttonAddAircraft.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1)
{
return;
}
FormAircraftConfig formConfig = new FormAircraftConfig();
formConfig.run();
formConfig.addListener(drawingAircraft -> {
if(drawingAircraft == null) return;
DrawingObjectAircraft aircraft = new DrawingObjectAircraft(drawingAircraft);
if (_mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).addAircraft(aircraft) != -1)
{
JOptionPane.showMessageDialog(jFrame, "Объект добавлен");
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowSet();
canv.repaint();
}
else
{
JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект");
}
});
2022-11-15 18:20:34 +04:00
});
2022-11-25 13:49:46 +04:00
buttonRemoveAircraft.addActionListener(e -> {
2022-11-15 18:20:34 +04:00
String text = textBoxPosition.getText();
if(text.isEmpty()) return;
if(JOptionPane.showConfirmDialog(
jFrame,
"Вы действительно хотите удалить объект?",
"Удаление",
JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return;
int pos;
try {
pos = Integer.parseInt(text);
} catch (Exception err) {
return;
}
pos = Integer.parseInt(text);
2022-11-25 13:49:46 +04:00
String mapName = listBoxMaps.getSelectedValue().toString();
IDrawingObject deleted = _mapsCollection.getMap(mapName).removeAircraft(pos);
if(deleted != null) {
2022-11-15 18:20:34 +04:00
JOptionPane.showMessageDialog(jFrame, "Объект удален");
2022-11-25 13:49:46 +04:00
img = _mapsCollection.getMap(mapName).ShowSet();
deletedAircrafts.add(deleted);
2022-11-15 18:20:34 +04:00
canv.repaint();
} else {
JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект");
}
});
buttonShowStorage.addActionListener(e -> {
2022-11-25 13:49:46 +04:00
if(listBoxMaps.getSelectedValue() == null) return;
2022-11-15 18:20:34 +04:00
2022-11-25 13:49:46 +04:00
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowSet();
2022-11-15 18:20:34 +04:00
canv.repaint();
});
buttonShowOnMap.addActionListener(e -> {
2022-11-25 13:49:46 +04:00
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowOnMap();
canv.repaint();
});
buttonShowDeleted.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1)
2022-11-15 18:20:34 +04:00
{
return;
}
2022-11-25 13:49:46 +04:00
if(deletedAircrafts.size() == 0) {
JOptionPane.showMessageDialog(jFrame, "Очередь пуста");
return;
}
DrawingObjectAircraft deleted = (DrawingObjectAircraft) deletedAircrafts.peek();
FormAircraft dialog = new FormAircraft(deleted.getAircraft());
deletedAircrafts.remove();
dialog.run();
dialog.setSize(800, 500);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
2022-11-15 18:20:34 +04:00
});
leftButton.addActionListener(e -> {
2022-11-25 13:49:46 +04:00
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left);
2022-11-15 18:20:34 +04:00
canv.repaint();
});
rightButton.addActionListener(e -> {
2022-11-25 13:49:46 +04:00
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right);
2022-11-15 18:20:34 +04:00
canv.repaint();
});
upButton.addActionListener(e -> {
2022-11-25 13:49:46 +04:00
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up);
2022-11-15 18:20:34 +04:00
canv.repaint();
});
downButton.addActionListener(e -> {
2022-11-25 13:49:46 +04:00
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down);
2022-11-15 18:20:34 +04:00
canv.repaint();
});
}
@Override
public void Draw(Graphics2D g) {
if(img == null) return;
g.drawImage(img, 0, 0, null);
}
}