238 lines
8.3 KiB
Java
238 lines
8.3 KiB
Java
import javax.swing.*;
|
||
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 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();
|
||
|
||
_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);
|
||
}
|
||
}
|