PIbd-12_Karamushko_M.K._Air.../FormMapWithSetAircrafts.java
2022-12-02 10:17:40 +03:00

318 lines
12 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.util.*;
import java.util.List;
public class FormMapWithSetAircrafts implements Form {
private JPanel MainPane;
private JButton buttonAddAircraft;
private JComboBox comboBoxSelectorMap;
private JTextField textBoxPosition;
private JButton buttonRemoveAircraft;
private JButton buttonShowStorage;
private JButton buttonShowOnMap;
private JButton upButton;
private JButton leftButton;
private JButton rightButton;
private JButton downButton;
private JPanel drawPanel;
private JTextField textBoxNewMapName;
private JList listBoxMaps;
private JButton buttonAddMap;
private JButton buttonDeleteMap;
private JButton buttonShowDeleted;
private JMenuBar menu;
private MapsCollection _mapsCollection;
private HashMap<String, AbstractMap> _mapsDict = new HashMap<>(){{
put( "Простая карта", new SimpleMap() );
put( "Моя карта", new MyMap() );
}};
private Canvas canv = new Canvas(this);
private Queue<IDrawingObject> deletedAircrafts = new ArrayDeque<>();
JFrame jFrame = getFrame();
Image img;
private JFrame getFrame() {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setBounds(300, 50, 1000, 750);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
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);
}
}
public void run() {
jFrame.add(MainPane);
drawPanel.add(canv);
jFrame.revalidate();
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, "Не загрузилось");
});
_mapsCollection = new MapsCollection(canv.getSize().width, canv.getSize().height);
comboBoxSelectorMap.removeAllItems();
_mapsDict.keySet().forEach(elem -> {
comboBoxSelectorMap.addItem(elem);
});
comboBoxSelectorMap.setSelectedIndex(-1);
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()))
{
JOptionPane.showMessageDialog(jFrame, "Нет такой карты");
return;
}
_mapsCollection.AddMap(textBoxNewMapName.getText(),
_mapsDict.get(comboBoxSelectorMap.getSelectedItem().toString()));
ReloadMaps();
});
buttonDeleteMap.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1)
{
return;
}
String mapName = listBoxMaps.getSelectedValue().toString();
if (JOptionPane.showConfirmDialog(jFrame, "Удалить карту " + mapName,
"Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
{
_mapsCollection.DelMap(mapName);
ReloadMaps();
}
});
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, "Не удалось добавить объект");
}
});
});
buttonRemoveAircraft.addActionListener(e -> {
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);
String mapName = listBoxMaps.getSelectedValue().toString();
IDrawingObject deleted = _mapsCollection.getMap(mapName).removeAircraft(pos);
if(deleted != null) {
JOptionPane.showMessageDialog(jFrame, "Объект удален");
img = _mapsCollection.getMap(mapName).ShowSet();
deletedAircrafts.add(deleted);
canv.repaint();
} else {
JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект");
}
});
buttonShowStorage.addActionListener(e -> {
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowSet();
canv.repaint();
});
buttonShowOnMap.addActionListener(e -> {
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowOnMap();
canv.repaint();
});
buttonShowDeleted.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1)
{
return;
}
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);
});
leftButton.addActionListener(e -> {
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left);
canv.repaint();
});
rightButton.addActionListener(e -> {
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right);
canv.repaint();
});
upButton.addActionListener(e -> {
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up);
canv.repaint();
});
downButton.addActionListener(e -> {
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down);
canv.repaint();
});
}
@Override
public void Draw(Graphics2D g) {
if(img == null) return;
g.drawImage(img, 0, 0, null);
}
}